kotlin - 如何 mvvm 数据绑定 tablayout 项目单击
问题描述
首先,我想做的是使用选项卡布局替换片段。无需使用viewpager。
我google了这个那个,还看了关于stackoverflow的文章,但最后还是没用,所以我要问一个问题
我的xml代码:
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/include"
app:tabGravity="fill"
app:tabMode="fixed">
<com.google.android.material.tabs.TabItem
android:id="@+id/program_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{()-> wishlistViewModel.moveWishListStep(WishlistStep.PROGRAM)}"
android:text="@string/program" />
<com.google.android.material.tabs.TabItem
android:id="@+id/magazine_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/magazine"
android:onClick="@{()-> wishlistViewModel.moveWishListStep(WishlistStep.MAGAZINE)}" />
</com.google.android.material.tabs.TabLayout>
我的视图模型代码:
fun moveWishListStep(view: View, newWishListStep: WishlistStep) {
_wishListStep.value = newWishListStep
Timber.d("MyPageStep moveStep: $newWishListStep")
}
但它不起作用
是不是标签布局中某个项目的点击不一样?目前我正在通过将片段分成步骤将其更改为 livedata 观察
如果您知道如何操作,如果您能分享它,我将非常感激。
解决方案
推荐阅读
- postgresql - 管理 Kubernetes 集群上的数据库迁移
- python-3.x - 将 Python 数据帧写入 AWS 红移时,DataError- 类型字符变化(256)的值太长
- vue.js - Vuejs 阻止在 contenteditable 中添加新行
- javascript - 没有收到控制台错误,但脚本不起作用
- javascript - 将购物车存储在本地存储中并在路由更改 vue 上持久化数据
- excel - VBA:如何在用户表单文本框中显示文件路径
- sql - Postgres/Redshift 按状态统计最后 5 行
- jenkins - 从版本 2.7.2 更新到 2.107.2 后,Jenkins 代理无法连接
- mysql - Laravel - 无法在查询中加入表
- r - 下一个“特定”观察结果向后进行(NOCB)