如何在使用 TabLayout 时禁用或选择某些选项卡?

7

我正在使用android.support.design.widget.TabLayout控件。

我有4个选项卡 - 在每个选项卡上,我显示一些ViewPager - (使用fragment来显示不同的ViewPager)

我希望在用户添加第一个选项卡上存在的某些数据之前禁用所有选项卡。

我找不到任何禁用选项卡的方法。

代码:

 <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/detailsElementBackground"
        android:clickable="true"
        app:tabGravity="center"
        app:tabMode="scrollable"
        app:tabTextAppearance="@style/MineCustomTabText" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.AppBarLayout>


 ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());


    adapter.addFragment(new Fragment1(), "Fragment1");
    adapter.addFragment(new Fragment2(), "Fragment2");
    adapter.addFragment(new Fragment3(), "Fragment3");
    adapter.addFragment(new Fragment4(), "Fragment4");

//需要禁用Fragment2、Fragment3和Fragment4,直到用户添加了在Fragment1中存在的某些字符串


同样的问题。@Yanshof,请让我知道您是否已经找到解决方案。 - ZarNi Myo Sett Win
6个回答

3
这是我的解决方案。我使用Kotlin语言,Java也可以这样做。
private fun disableTab(tabLayout: TabLayout, index: Int) {
    (tabLayout.getChildAt(0) as ViewGroup).getChildAt(index).isEnabled = false
}

这对我很有帮助!


3

试试这个:

LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
    tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
}

参考:禁用TabLayout


2
听起来你想要做的是在输入数据之前禁用切换选项卡的功能,然后重新启用它。 这很容易,你只需要做两件事情: 一旦你实现了以上两点,你可以在你的onCreate方法中使用以下代码禁用选项卡切换功能:
tabs.isEnabled = false
viewpager.setSwipePagingEnabled(false)

当用户在第一个选项卡上输入数据后,您可以重新启用它。

tabs.setEnabled(true)
viewpager.setSwipePagingEnabled(true)

2
如果您想处理TabLayout的onTabSelected事件,可以这样做并检查是否允许显示该片段。
tab_layout.addOnTabSelectedListener( new TabLayout.OnTabSelectedListener() {
            override onTabReselected( TabLayout.Tab tab ) {}
            override onTabUnselected( TabLayout.Tab tab ) {}
            override onTabSelected( TabLayout.Tab tab ) {
                if( ... is not disabled )
                pager.currentItem = tab.position
            }

        })

2

ViewPager2 + tabLayout

TabLayoutMediator(Tabs,ViewPager) { tab,  pos->
   tab.view.isEnabled = false
}.attach()

1
如果你想禁用选项卡,你只需要使用自定义视图。
首先,创建你的自定义布局。

v_tabview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tabItemView"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:maxLines="1"
  android:textColor="@drawable/selector_tab" />

创建选择器,用于更改状态启用/禁用(更改颜色)。

selector_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="#9e9e9e" android:state_enabled="false" /> //gray
  <item android:color="#64b246" android:state_enabled="true" /> //green
</selector>

然后将其填充,设置名称并添加到tabLayout中。
arrayStringNames.forEach { name ->
    val textView: TextView = inflater.inflate(R.layout.v_tabview, tabLayout, false) as TextView
    textView.text = name
    val tab = tabLayout.newTab()
    tab.customView = textView
    tabLayout.addTab(tab)
}

在最后,一个魔术技巧!在那个样例代码中,我禁用了所有选项卡。如果你需要禁用第二和第三个选项卡,检查循环中的“index”,并在需要时禁用。
 for (index in 0 until tabLayout.tabCount) {
   ((tabLayout.getTabAt(index)?.customView) as? TextView)?.let { textView ->
     textView.isEnabled = enable //boolean
     (textView.parent as View).enable(enable)
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接