Disable TabLayout

前端 未结 11 1878
时光说笑
时光说笑 2020-11-29 02:49

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 disa

11条回答
  •  旧时难觅i
    2020-11-29 03:16

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

提交回复
热议问题