简体   繁体   中英

android disable / enable tab in tabbedActivity

I can not disable a tab in android tabbed activity. The tabbed activity have 3 tabs and I want to disable the tab in the middle.

I tried the following code in my Fragment but the variable middleTabView is always 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 .

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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