TabLayout选项卡选择

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(这是非典型的,可能是不好的做法,但我已经看到它已经完成),这项技术仍然有效.

  • 此解决方案仅在选项卡已添加到操作栏时才有效,而这可能并非总是如此.从'TabLayout.Tab.select()`的文档中:*"选择此选项卡.仅在选项卡已添加到操作栏时才有效."* (10认同)
  • @DanielKvist,实际上文档本身是不正确的.源代码说明了一切.这个答案应该是公认的答案. (6认同)
  • 为什么在没有ViewPager的情况下使用TabLayout应被视为不良做法? (3认同)

小智 76

这就是我解决它的方式:

void selectPage(int pageIndex){
    tabLayout.setScrollPosition(pageIndex,0f,true);
    viewPager.setCurrentItem(pageIndex);
}
Run Code Online (Sandbox Code Playgroud)

  • 选定的答案对我没有用,但这个答案没有. (3认同)

Riy*_* PK 35

使用 __CODE__

  • 请记住,如果 currentTabIndex 和索引相同,那么这会将您的流程发送到 onTabReselected 而不是 onTabSelected。 (2认同)
  • @WilliaanLopes,这不会保护你免受 NPI 的影响,只会更快地抛出它 (2认同)

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执行所需的操作.


Pan*_*lva 11

只需设置viewPager.setCurrentItem(index),关联的TabLayout就会选择相应的选项卡.


Kil*_*uSs 8

最新的简单解决方案对我有用:

with(binding.tablayout) {
    selectTab(getTabAt(tabPosisiton))
}

Run Code Online (Sandbox Code Playgroud)

tabPosition从 0开始


CoC*_*Dev 7

您可以尝试用以下方法解决它:

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)


A.G*_*AYS 6

试试这个

    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)


Kis*_*nki 6

科特林用户:

Handler(Looper.getMainLooper()).postDelayed(
            { tabLayout.getTabAt(position).select() }, 100
        )
Run Code Online (Sandbox Code Playgroud)

如果需要滚动,这也会滚动您的选项卡布局。


ahm*_*_89 5

有点晚了,但可能是有用的解决方案。我直接在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(()-&gt;{ tabLayout.post(() -&gt; { tabLayout.getTabAt(myPosition).select(); }); } ,200);` (2认同)

Zon*_*Zon 5

不同答案的组合解决方案是:

new Handler().postDelayed(() -> {
  myViewPager.setCurrentItem(position, true);
  myTabLayout.setScrollPosition(position, 0f, true);
},
100);
Run Code Online (Sandbox Code Playgroud)