首页 > 解决方案 > 如何 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 观察
如果您知道如何操作,如果您能分享它,我将非常感激。

标签: kotlinmvvm

解决方案


推荐阅读