简体   繁体   English

tabbedActivity中的android disable / enable选项卡

[英]android disable / enable tab in tabbedActivity

I can not disable a tab in android tabbed activity. 我无法在android选项卡式活动中禁用选项卡。 The tabbed activity have 3 tabs and I want to disable the tab in the middle. 选项卡式活动有3个选项卡,我想在中间禁用选项卡。

I tried the following code in my Fragment but the variable middleTabView is always null! 我在Fragment中尝试了以下代码,但变量middleTabView始终为null!

TabLayout tabhostNew = (TabLayout) getActivity().findViewById(R.id.tabs);
TabLayout.Tab middleTabView  = tabhostNew.getTabAt(1).getCustomView();
middleTabView.setEnabled(false); //does not work, because middleTabView is null

The following code should work, but i am not able to get the variable tabwidget . 以下代码应该可以工作,但我无法获取变量tabwidget

tabHost.getTabWidget().getChildTabViewAt(your_index).setEnabled(false);

Can you please help me? 你能帮我么? Thank you in advance! 先感谢您!

The method you are trying to call getTabWidget() is implemented in the TabHost class and not in TabLayout (which you are using). 您尝试调用的方法getTabWidget()在实施TabHost类,而不是在TabLayout (您正在使用)。

Check out this answer : 看看这个答案

TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager); // if you are using a view pager

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM