Kotlin 如何在没有 viewBinding.enabled = true 的情况下找到小部件?
How Kotlin find widget WITHOUT viewBinding.enabled = true?
Android Studio 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 的 activity 片段(approach#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 上更改我的 activity (approach#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
.
不错。
但问题是:
如何在 approach#2 中绑定工作 WITHOUT
android {
viewBinding.enabled = true
}
?
由于 apply plugin: 'kotlin-android-extensions'
在您应用的 build.gradle 文件中(可能它就在 apply plugin: 'kotlin-android'
行之后,因为您没有 post 您的整个应用build.gradle 文件)
Android Studio 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 的 activity 片段(approach#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 上更改我的 activity (approach#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
.
不错。
但问题是:
如何在 approach#2 中绑定工作 WITHOUT
android {
viewBinding.enabled = true
}
?
由于 apply plugin: 'kotlin-android-extensions'
在您应用的 build.gradle 文件中(可能它就在 apply plugin: 'kotlin-android'
行之后,因为您没有 post 您的整个应用build.gradle 文件)