TabLayout选项卡选择 [英] TabLayout tab selection
本文介绍了TabLayout选项卡选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以编程方式在TabLayout中选择一个标签?
How should I select a tab in TabLayout programmatically?
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
推荐答案
如果知道要选择的选项卡的索引,则可以这样做:
If you know the index of the tab you want to select, you can do it like so:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
TabLayout.Tab tab = tabLayout.getTabAt(someIndex);
tab.select();
即使您单独使用TabLayout而不使用ViewPager,此技术也可以工作(这是非典型的,可能是不好的做法,但我已经看到了).
This technique works even if you're using the TabLayout by itself without a ViewPager (which is atypical, and probably bad practice, but I've seen it done).
这篇关于TabLayout选项卡选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文