Disable TabLayout

2020-01-27 03:04发布

I'm using the new class provided by the design library : TabLayout. And I want in specific cases that the one I'm using can't change tab anymore.

I manage to disable swipe on its viewpager but I can't figure out how to disable the change of page by clicking on tabs.

Thank's in advance.

9条回答
手持菜刀,她持情操
2楼-- · 2020-01-27 03:30

A good trick you can use :

create a frame layout that cover the view(s) you want to protect from a click like the following :

<FrameLayout
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

This code will create and empty/transparent view on top of your view. The android:clickable="true" will intercept the click and prevent the click to go through the view !

This hack can probably be optimized but its few lines of code to protect multiple view at the same time !

查看更多
forever°为你锁心
3楼-- · 2020-01-27 03:35

It's also possible to avoid child clicks by extending TabLayout class and intercepting all the touch events.

class NonTouchableTabLayout : TabLayout {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        return true
    }
}

If you want to enable clicks again just return false on onInterceptTouchEvent method.

查看更多
霸刀☆藐视天下
4楼-- · 2020-01-27 03:39

If you are using a custom view for the Tab, you can use View#getParent() to get a reference to the Tab's View if you don't want to look through the ViewGroups.

Note: Using the custom view itself instead of the parent may not work because it can have margin, allowing the user to click in the empty space and change the tab still.

View tabView = (View) tab.getCustomView().getParent();

tabView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});

//or

tabView.setEnabled(false);

The OnTouchListener way and the setEnabled() way do different things, but have the same effect. I prefer the one-liner.

Again, this only works if you use a custom view, otherwise the getParent() call will cause a NullPointerException.

查看更多
冷血范
5楼-- · 2020-01-27 03:40

I found a similar answer that is a little more simple and also allows you to re-enable the tabs later if you wanted to without having to deal with overriding the onTouch event.

TabLayout tabLayout = (TabLayout)  mParentView.findViewById(R.id.my_tabs);

LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
tabStrip.setEnabled(false);
for(int i = 0; i < tabStrip.getChildCount(); i++) {
    tabStrip.getChildAt(i).setClickable(false);
}

And if you want to re-enable the tabs just set tabStrip.setEnabled and setClickable for the child elements to true

LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
tabStrip.setEnabled(true);
for(int i = 0; i < tabStrip.getChildCount(); i++) {
    tabStrip.getChildAt(i).setClickable(true);
}
查看更多
forever°为你锁心
6楼-- · 2020-01-27 03:40

Based on this excellent solution , if someone needs it in Kotlin :

  val tabStrip = mTabLayout.getChildAt(0) as LinearLayout
    for (i in 0..tabStrip.childCount) {
        tabStrip.getChildAt(i).setOnTouchListener(object : View.OnTouchListener{
            override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
                return true
            }
        })
    }

Or a more elegant solution with lambdas

mTabLayout.setOnTouchListener {v: View, m: MotionEvent ->
        true
 }
查看更多
叼着烟拽天下
7楼-- · 2020-01-27 03:44

check my answer here Disable Tabs in TabLayout

You can for loop tab and use this function to disable all tab

查看更多
登录 后发表回答