Android Studio 3.6
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中:
android {
viewBinding.enabled = true
dataBinding {
enabled = true
}
在我的活动中:
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.myproject.BuildConfig
import com.myproject.R
import com.myproject.adapter.CustomFragmentStateAdapter
import com.myproject.databinding.QrBluetoothSwipeActivityBinding
import com.myproject.ui.fragment.BluetoothPageFragment
import com.myproject.ui.fragment.QrPageFragment
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableInt
class QRBluetoothSwipeActivity : AppCompatActivity() {
private lateinit var viewBinding: QrBluetoothSwipeActivityBinding
var positionObservable = ObservableInt()
companion object {
private val TAG = QRBluetoothSwipeActivity::class.java.name
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// databinding init
val binding = DataBindingUtil.setContentView<QrBluetoothSwipeActivityBinding>(
this, R.layout.qr_bluetooth_swipe_activity
)
binding.setHandler(this)
// viewbinding init
viewBinding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
setContentView(viewBinding.root)
init()
}
private fun init() {
val customFragmentStateAdapter = CustomFragmentStateAdapter(this)
customFragmentStateAdapter.addFragment(QrPageFragment())
customFragmentStateAdapter.addFragment(BluetoothPageFragment())
viewBinding.viewPager2.adapter = customFragmentStateAdapter
viewBinding.viewPager2.registerOnPageChangeCallback(object :
ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "registerOnPageChangeCallback: position = $position")
}
positionObservable.set(position)
}
})
}
我的 qr_bluetooth_swipe_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
<variable
name="handler"
type="com.myproject.actviity.QRBluetoothSwipeActivity" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottonContainer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottonContainer"
android:layout_width="0dp"
android:layout_height="104dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/qrBottonMainContainer"
android:layout_width="0dp"
android:layout_height="104dp"
android:visibility="@{handler.positionObservable == 0 ? View.GONE: View.VISIBLE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
启动应用程序后,然后成功滑动 viewpager2 。 结果成功显示了下一条消息:
10-25 14:25:24.991 D/com.myproject.actviity.QRBluetoothSwipeActivity(23012): registerOnPageChangeCallback: position = 0
好。
但是qrBottonMainContainer
不会隐藏。为什么?
P.S。如果我删除了此内容:
viewBinding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
setContentView(viewBinding.root)
然后成功工作。
为什么?
答案 0 :(得分:3)
你不能用它们来打猎。
// prettier-ignore
是ViewBinding
的功能的子集,如果要替换DataBinding
,ButterKnife
或KAE(Kotlin Android扩展)之类的库,应使用
KotterKnife
不想投资于数据绑定重构。
如果您使用DataBinding
,则已经在binding
对象中拥有构成布局的视图的ID参考。像binding.myTextView
之类的东西。
请记住:
- 数据绑定库仅处理使用标签创建的数据绑定布局。
- 视图绑定不支持布局变量或布局表达式,因此不能用于将布局与XML数据绑定。
根据文档here
PS:在您的特定情况下,您不能将<layout>
标签与ViewBinding
一起使用
答案 1 :(得分:0)
这里没有 viewbinding
的解决方案android {
dataBinding {
enabled = true
}
活动中:
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.myproject.BuildConfig
import com.myproject.R
import com.myproject.adapter.CustomFragmentStateAdapter
import com.myproject.ui.fragment.BluetoothPageFragment
import com.myproject.ui.fragment.QrPageFragment
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableInt
import com.myproject.databinding.QrBluetoothSwipeActivityBinding
class QRBluetoothSwipeActivity : AppCompatActivity() {
private lateinit var dataBinding: QrBluetoothSwipeActivityBinding
var positionObservable = ObservableInt()
companion object {
private val TAG = QRBluetoothSwipeActivity::class.java.name
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
dataBinding = DataBindingUtil.setContentView<QrBluetoothSwipeActivityBinding>(
this, R.layout.qr_bluetooth_swipe_activity
)
dataBinding.setHandler(this)
init()
}
private fun init() {
val customFragmentStateAdapter = CustomFragmentStateAdapter(this)
customFragmentStateAdapter.addFragment(QrPageFragment())
customFragmentStateAdapter.addFragment(BluetoothPageFragment())
dataBinding.viewPager2.adapter = customFragmentStateAdapter
dataBinding.viewPager2.registerOnPageChangeCallback(object :
ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "registerOnPageChangeCallback: position = $position")
}
positionObservable.set(position)
}
})
}
}
现在,仅适用于数据绑定。
很好。
感谢@MatPag
答案 2 :(得分:0)
如果您想在单个布局中同时使用视图绑定和数据绑定 您只需要使用数据绑定,因为视图绑定是数据绑定的子集,数据绑定提供视图绑定的功能。
两者的区别在于,视图绑定仅用于视图引用,而不用于将 UI 与数据源绑定。
android {
buildFeatures {
dataBinding true
}
}
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.myapp.data.ViewModel" />
</data>
</layout>
val dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
dataBinding.executePendingBindings()
dataBinding.tvName.text="View Binding"