Disable TabLayout

不羁的心 提交于 2019-12-17 07:17:10

问题


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.


回答1:


I had the same problem and I solved it disabling touch event on tabs with the following code:

  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;
            }
        });
    }



回答2:


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);
}



回答3:


Very similar to the answer by pat8719 but just disabling the tabs is sufficient to prevent them from being selected.

TabLayout tabLayout = (TabLayout)  mParentView.findViewById(R.id.my_tabs);
TabLayoutUtils.enableTabs( tabLayout, false );

TabLayoutUtils class

public class TabLayoutUtils {

    public static void enableTabs(TabLayout tabLayout, Boolean enable){
        ViewGroup viewGroup = getTabViewGroup(tabLayout);
        if (viewGroup != null)
            for (int childIndex = 0; childIndex < viewGroup.getChildCount(); childIndex++)
            {
                View tabView = viewGroup.getChildAt(childIndex);
                if ( tabView != null)
                    tabView.setEnabled(enable);
            }
    }

    public static View getTabView(TabLayout tabLayout, int position){
        View tabView = null;
        ViewGroup viewGroup = getTabViewGroup(tabLayout);
        if (viewGroup != null && viewGroup.getChildCount() > position)
            tabView = viewGroup.getChildAt(position);

        return tabView;
    }

    private static ViewGroup getTabViewGroup(TabLayout tabLayout){
        ViewGroup viewGroup = null;

        if (tabLayout != null && tabLayout.getChildCount() > 0 ) {
            View view = tabLayout.getChildAt(0);
            if (view != null && view instanceof ViewGroup)
                viewGroup = (ViewGroup) view;
        }
        return viewGroup;
    }

}



回答4:


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 !




回答5:


For me the working approach is this:

bool condition = ...
foreach (var view in _tabLayout.Touchables)
    view.Clickable = condition;

This is 100% safe, as getTouchables() call is supported since API 1. No need to manually traverse layout or something. I consider it to be much simpler than that of the accepted answer, but only when all the tabs have to be marked not clickable.

P.S.: example is on C#, as I am working with Xamarin, but it is fairly easy to translate it back to Java. Enjoy! =)

Kotlin example:

tabLayout.touchables.forEach { it.isClickable = false }

Java example:

for (View v: tabLayout.getTouchables())
    v.setClickable(false);



回答6:


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.




回答7:


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.




回答8:


check my answer here Disable Tabs in TabLayout

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




回答9:


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
 }


来源:https://stackoverflow.com/questions/31702725/disable-tablayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!