我应该如何在TabLayout中以编程方式选择选项卡?
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
我应该如何在TabLayout中以编程方式选择选项卡?
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
如果您知道要选择哪个选项卡的索引,可以这样做:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
TabLayout.Tab tab = tabLayout.getTabAt(someIndex);
tab.select();
即使您在不使用ViewPager的情况下仅使用TabLayout这种技术也可以工作(尽管这是不典型的且可能是不好的实践,但我看过它被使用)。
这是我解决问题的方法:
void selectPage(int pageIndex){
tabLayout.setScrollPosition(pageIndex,0f,true);
viewPager.setCurrentItem(pageIndex);
}
使用这个:
tabs.getTabAt(index).select();
请记住,如果currentTabIndex和index相同,则会将您的流程发送到onTabReselected而不是onTabSelected。Objects.requireNonNull(tabs.getTabAt(position)).select();
的使用是安全的。 - Williaan Lopes使用这个:
<android.support.design.widget.TabLayout
android:id="@+id/patienthomescreen_tabs"
android:layout_width="match_parent"
android:layout_height="72sp"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabIndicatorColor="@android:color/white"
app:tabSelectedTextColor="@color/green"/>
在OnClickListener之后:
TabLayout tabLayout = (TabLayout) findViewById(R.id.patienthomescreen_tabs);
TabLayout.Tab tab = tabLayout.getTabAt(someIndex);
tab.select();
请记住,如果 currentTabIndex 和 index 相同,这将使您的流程发送到 onTabReselected 而不是 onTabSelected。
tabLayoutSensor.getTabAt(0)?.select()
。 - joninx这可能不是最终解决方案,需要您使用TabLayout
与ViewPager
一起使用,但这就是我解决它的方式:
void selectPage(int pageIndex)
{
viewPager.setCurrentItem(pageIndex);
tabLayout.setupWithViewPager(viewPager);
}
我测试了使用这段代码的性能影响有多大,首先在运行该方法时观察Android Studio中的CPU和内存监视器,然后将其与我自己在页面之间导航时(使用滑动手势)对CPU和内存产生的负载进行比较,差异并不显著,所以至少它不是一个糟糕的解决方案...
希望这能帮助到某些人!
tabLayout.setScrollPosition(pageIndex,0f,true);
)时,当我点击右侧的标签时,它没有更新ViewPager的页面(我不知道为什么)。只有先点击左侧选项卡,然后再点击右侧选项卡才能最终更新页面以显示正确的选项卡页面,这非常不稳定。但是这个解决方案没有任何故障。 - Rock Lee只需设置viewPager.setCurrentItem(index)
,相关的TabLayout将选择相应的选项卡。
使用TabLayout
,这是由Material Components Library提供的。只需使用selectTab
方法即可:
TabLayout tabLayout = findViewById(R.id.tab_layout);
tabLayout.selectTab(tabLayout.getTabAt(index));
需要1.1.0版本。
如果您无法使用tab.select(),并且不想使用ViewPager,仍然可以以编程方式选择选项卡。如果您正在使用通过TabLayout.Tab setCustomView(android.view.View view)
自定义视图,那么这将更简单。以下是两种方法的实现。
// if you've set a custom view
void updateTabSelection(int position) {
// get the position of the currently selected tab and set selected to false
mTabLayout.getTabAt(mTabLayout.getSelectedTabPosition()).getCustomView().setSelected(false);
// set selected to true on the desired tab
mTabLayout.getTabAt(position).getCustomView().setSelected(true);
// move the selection indicator
mTabLayout.setScrollPosition(position, 0, true);
// ... your logic to swap out your fragments
}
如果您没有使用自定义视图,则可以按照以下方式进行操作
// if you are not using a custom view
void updateTabSelection(int position) {
// get a reference to the tabs container view
LinearLayout ll = (LinearLayout) mTabLayout.getChildAt(0);
// get the child view at the position of the currently selected tab and set selected to false
ll.getChildAt(mTabLayout.getSelectedTabPosition()).setSelected(false);
// get the child view at the new selected position and set selected to true
ll.getChildAt(position).setSelected(true);
// move the selection indicator
mTabLayout.setScrollPosition(position, 0, true);
// ... your logic to swap out your fragments
}
使用StateListDrawable来切换选择和未选择的可绘制对象,或使用类似的东西来实现您想要的颜色和/或可绘制对象。
有点晚了,但可能是一个有用的解决方案。
我直接在我的Fragment中使用TabLayout,并尝试在Fragment的生命周期很早的阶段选择一个标签。
对我有用的是等待TabLayout完成其子视图的绘制,使用android.view.View#post
方法。即:
int myPosition = 0;
myFilterTabLayout.post(() -> { filterTabLayout.getTabAt(myPosition).select(); });
new Handler().postDelayed(() -> {
tabLayout.post(() -> { tabLayout.getTabAt(myPosition).select(); });
}, 200);
- HishamHandler.postDelayed
这种解决方案,因为如果用户非常快地退出屏幕,它可能会导致NPE:在延迟200后,你的tabLayout
可能已经不存在了... - ahmed_khan_89TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
TabLayout.Tab tab = tabLayout.getTabAt(pos);
if (tab != null) {
tab.select();
}
TabLayout.Tab.select()
的文档中可以看到:"选择此选项卡。仅当选项卡已添加到操作栏时才有效。" - Daniel Kvist