How do I make all tabs in a TabHost unselected

2019-08-27 21:43发布

I need to show tab content on occasion, otherwise the area must be filled with "non-tabhost" data.However, tabs should be visible and when user clicks any of those tabs "non-tabhost" must be hidden and appropriate tab content must become visible.

It's something connected to a fake tab creation ?

Give an example of creating TabHost with tabs unselected. Thanks.

2条回答
虎瘦雄心在
2楼-- · 2019-08-27 22:16

1.copy the code where you want to tabs make unselected

 tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
                    tabLayout.setTabTextColors(Color.BLACK, Color.BLACK);

2.Override on Tabselected Listener and paste the following code

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override`enter code here
            public void onTabSelected(TabLayout.Tab tab) {
                 tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#EB1C23"));
                tabLayout.setTabTextColors(Color.BLACK, Color.RED);
              viewPager.setCurrentItem(position);
}


            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
               tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#EB1C23"));
                tabLayout.setTabTextColors(Color.BLACK, Color.RED);
              viewPager.setCurrentItem(position);
            }
        });
查看更多
ゆ 、 Hurt°
3楼-- · 2019-08-27 22:35

What I usually do is, add an extra Tab and use setVisibility(View.GONE) to hide it. THis will just hide the tab button from the user, and the Tab will still be there, in the "background" and you can programmatically select it, by using tabHost.setCurrentTab(0). I also usually keep this tab as the first one.

查看更多
登录 后发表回答