TabLayout 选项卡选择 [英] TabLayout tab selection

查看:36
本文介绍了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();

即使您在没有 ViewPager 的情况下单独使用 TabLayout(这是非典型的,可能是不好的做法,但我已经看到它完成),这种技术也有效.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆