Kid*_*d24 212 android android-viewpager android-design-library android-tablayout
如何以编程方式在TabLayout中选择选项卡?
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
Run Code Online (Sandbox Code Playgroud)
Fir*_*fly 390
如果您知道要选择的选项卡的索引,则可以这样做:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
TabLayout.Tab tab = tabLayout.getTabAt(someIndex);
tab.select();
Run Code Online (Sandbox Code Playgroud)
即使您在没有ViewPager的情况下单独使用TabLayout(这是非典型的,可能是不好的做法,但我已经看到它已经完成),这项技术仍然有效.
小智 76
这就是我解决它的方式:
void selectPage(int pageIndex){
tabLayout.setScrollPosition(pageIndex,0f,true);
viewPager.setCurrentItem(pageIndex);
}
Run Code Online (Sandbox Code Playgroud)
Riy*_* PK 35
使用 __CODE__
Pra*_*har 33
用这个:
<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"/>
Run Code Online (Sandbox Code Playgroud)
在OnClickListener之后:
TabLayout tabLayout = (TabLayout) findViewById(R.id.patienthomescreen_tabs);
TabLayout.Tab tab = tabLayout.getTabAt(someIndex);
tab.select();
Run Code Online (Sandbox Code Playgroud)
Dan*_*ist 20
这可能不是最终的解决方案,它要求你TabLayout
和a一起使用ViewPager
,但这就是我解决它的方法:
void selectPage(int pageIndex)
{
viewPager.setCurrentItem(pageIndex);
tabLayout.setupWithViewPager(viewPager);
}
Run Code Online (Sandbox Code Playgroud)
我测试有多大使用此代码对性能的影响是首先寻找在Android Studio中的CPU和内存监控运行该方法时,则比较这是把CPU和内存上,当我在页面之间导航负载我自己(使用滑动手势),差异不是很大,所以至少它不是一个可怕的解决方案......
希望这有助于某人!
Gab*_*tti 14
使用Material Components LibraryTabLayout
提供的方法:selectTab
TabLayout tabLayout = findViewById(R.id.tab_layout);
tabLayout.selectTab(tabLayout.getTabAt(index));
Run Code Online (Sandbox Code Playgroud)
它需要 1.1.0 版。
ken*_*ggy 12
如果您不能使用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
}
Run Code Online (Sandbox Code Playgroud)
如果您没有使用自定义视图,那么您可以这样做
// 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
}
Run Code Online (Sandbox Code Playgroud)
使用StateListDrawable在选定和未选择的drawable之间切换或类似于使用颜色和/或drawable执行所需的操作.
最新的简单解决方案对我有用:
with(binding.tablayout) {
selectTab(getTabAt(tabPosisiton))
}
Run Code Online (Sandbox Code Playgroud)
tabPosition
从 0开始
您可以尝试用以下方法解决它:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
TabLayout.Tab tab = tabLayout.getTabAt(pos);
if (tab != null) {
tab.select();
}
Run Code Online (Sandbox Code Playgroud)
试试这个
new Handler().postDelayed(
new Runnable(){
@Override
public void run() {
if (i == 1){
tabLayout.getTabAt(0).select();
} else if (i == 2){
tabLayout.getTabAt(1).select();
}
}
}, 100);
Run Code Online (Sandbox Code Playgroud)
科特林用户:
Handler(Looper.getMainLooper()).postDelayed(
{ tabLayout.getTabAt(position).select() }, 100
)
Run Code Online (Sandbox Code Playgroud)
如果需要滚动,这也会滚动您的选项卡布局。
有点晚了,但可能是有用的解决方案。我直接在Fragment中使用TabLayout,并试图在Fragment的生命周期的早期选择一个选项卡。对我有用的是等到TabLayout使用android.view.View#post
方法完成绘制其子视图为止 。即:
int myPosition = 0;
myFilterTabLayout.post(() -> { filterTabLayout.getTabAt(myPosition).select(); });
Run Code Online (Sandbox Code Playgroud)
不同答案的组合解决方案是:
new Handler().postDelayed(() -> {
myViewPager.setCurrentItem(position, true);
myTabLayout.setScrollPosition(position, 0f, true);
},
100);
Run Code Online (Sandbox Code Playgroud)