【问题标题】:How Kotlin find widget WITHOUT viewBinding.enabled = true?Kotlin 如何在没有 viewBinding.enabled = true 的情况下找到小部件?
【发布时间】:2019-10-24 08:35:34
【问题描述】:

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 上的活动 sn-p(方法#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
}

?

【问题讨论】:

  • 使用 Kotlin 综合属性kotlinlang.org/docs/tutorials/android-plugin.html。也是 Kotlin Extensions 提供的ViewBinding
  • 那么哪个更可取? Kotlin Android Extensions 插件或 viewBinding.enabled=true?或者它们是等价的吗?
  • @Phil 我更喜欢开箱即用的东西。所以 viewBinding 更适合我
  • 开箱即用,您的意思是您不必链接外部库,这应该会导致 APK 更小? Kotlin Android Extensions 的一个明显好处是更少的输入——您不必修改模块 build.gradle 文件。默认情况下包含 Kotlin Android 扩展。您在代码中所要做的就是输入视图 ID 来引用它。您不必明确启用任何内容。

标签: android android-viewbinding


【解决方案1】:

由于您应用的 build.gradle 文件中的 apply plugin: 'kotlin-android-extensions' 可以正常工作(可能它就在 apply plugin: 'kotlin-android' 行之后,因为您没有发布整个应用的 build.gradle 文件)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2021-01-14
    • 1970-01-01
    相关资源
    最近更新 更多