Android 如何在TabLayout中禁用单击TabItem?

Android 如何在TabLayout中禁用单击TabItem?,android,android-tablayout,Android,Android Tablayout,我在TabLayout中有选项卡,有时我需要禁用一些选项卡,例如tab1(我不需要禁用所有选项卡,只需要禁用其中的一些)。禁用意味着不可能点击它。我该怎么做 <com.google.android.material.tabs.TabLayout android:id="@+id/switchLayout" android:layout_width="match_parent"> <

我在TabLayout中有选项卡,有时我需要禁用一些选项卡,例如tab1(我不需要禁用所有选项卡,只需要禁用其中的一些)。禁用意味着不可能点击它。我该怎么做

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/switchLayout"
        android:layout_width="match_parent">

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/custom" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/custom" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tab3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/custom" />

    </com.google.android.material.tabs.TabLayout>

如果可行,请尝试以下方法:

touchableList = tabLayout?.touchables
touchableList?.forEach { it.isEnabled = false }
或者您可以参考以下内容:

如果可行,请尝试以下方法:

touchableList = tabLayout?.touchables
touchableList?.forEach { it.isEnabled = false }
或者您可以参考以下内容:


感谢您为我指明方向。使用触摸屏似乎不是一个好主意,因为我必须说我想在哪个位置禁用它,并且禁用触摸屏会弄乱位置。因此,我尝试使用setOnTouchListener,它很有效,谢谢。很高兴知道它的帮助谢谢你为我指明方向。使用触摸屏似乎不是一个好主意,因为我必须说我想在哪个位置禁用它,并且禁用触摸屏会弄乱位置。因此,我尝试使用setOnTouchListener,它很有效,谢谢。很高兴知道它有帮助