安卓工作室 3.6
Android Studio 3.6中的一项新功能是
viewBinding.enabled = true
方法#1
在 build.gradle 中:
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.0-beta01'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
在 app/build.gradle 中:
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.31.1'
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
android {
viewBinding.enabled = true
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation('com.crashlytics.sdk.android:crashlytics:2.10.1@aar') { transitive = true; }
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0-beta01'
implementation 'org.altbeacon:android-beacon-library:2.16.3'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta05'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation "androidx.core:core-ktx:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
我在 Kotlin 上的活动片段(方法#1)
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.myproject.android.BuildConfig
import com.myproject.android.R
import com.myproject.android.adapter.CustomFragmentStateAdapter
import com.myproject.android.databinding.QrBluetoothSwipeActivityBinding
import com.myproject.android.ui.fragment.BluetoothPageFragment
import com.myproject.android.ui.fragment.QrPageFragment
class QRBluetoothSwipeActivity : AppCompatActivity() {
private lateinit var binding: QrBluetoothSwipeActivityBinding
private lateinit var myAdapter: CustomFragmentStateAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
init()
}
private fun init() {
myAdapter = CustomFragmentStateAdapter(this)
myAdapter.addFragment(QrPageFragment())
myAdapter.addFragment(BluetoothPageFragment())
binding.viewPager2.adapter = myAdapter
}
}
这是我的qr_bluetooth_swipe_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.actviity.SplashDelayActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
结果它工作正常。正如你所看到的,我使用这个:
binding.viewPager2.adapter = myAdapter
没有使用方法findViewById
。
好的。
但我可以用另一种方法解决这个问题:
方法#2:
从app/build.gradle
块中删除viewBinding.enabled = true
像这样更改我在 Kotlin 上的活动(方法#2):
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.qr_bluetooth_swipe_activity.*
import com.myproject.android.BuildConfig
import com.myproject.android.R
import com.myproject.android.adapter.CustomFragmentStateAdapter
import com.myproject.android.ui.fragment.BluetoothPageFragment
import com.myproject.android.ui.fragment.QrPageFragment
class QRBluetoothSwipeActivity2 : AppCompatActivity() {
private lateinit var myAdapter: CustomFragmentStateAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.qr_bluetooth_swipe_activity)
init()
}
private fun init() {
myAdapter = CustomFragmentStateAdapter(this)
myAdapter.addFragment(QrPageFragment())
myAdapter.addFragment(BluetoothPageFragment())
viewPager2.adapter = myAdapter
}
}
结果它工作正常。正如你所看到的,我使用这个:
viewPager2.adapter = myAdapter
没有使用方法findViewById
。
好的。
但问题是:
在方法#2中,绑定如何在没有的情况下工作
android {
viewBinding.enabled = true
}
?