【发布时间】:2020-07-31 21:15:02
【问题描述】:
我已尝试在 Internet 上使用教程在 androidx.viewpager.widget.ViewPager 上添加适配器以在当前选项卡上获得两种方式的数据绑定。
我到了那个适配器
// adapter file
@BindingAdapter("currentTab")
@JvmStatic
fun setTab(pager: ViewPager, itemLiveData: MutableLiveData<Int>) {
itemLiveData.value?.let {
if (pager.currentItem != it) {
pager.setCurrentItem(it, true)
}
}
}
@InverseBindingAdapter(attribute = "currentItem")
@JvmStatic
fun getTab(pager: ViewPager) = pager.currentItem
<!-- layout file -->
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="70dp"
app:adapter="@{viewModel.adapter}"
app:currentTab="@={viewModel.currentItem}"
app:layout_constraintBottom_toTopOf="@+id/tabLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
当我尝试构建应用程序时,我收到了这个错误(使用 --stacktrace 选项):
Could not find event 'currentItemAttrChanged' on View type 'androidx.viewpager.widget.ViewPager'
我找不到任何教程/答案,所以我在这里询问是否有人对此有答案。 谢谢
编辑:
我尝试使用@InverseBindingAdapter(attribute = "currentTab"),但它给了我同样的错误:Could not find event 'currentTabAttrChanged' on View type 'androidx.viewpager.widget.ViewPager'。
问题是currentTab更改时我不知道事件名称调用。
【问题讨论】:
标签: android kotlin android-viewpager android-databinding 2-way-object-databinding