我应该如何选择一个标签在TabLayout编程?

 TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
 tabLayout.setupWithViewPager(viewPager);

当前回答

如果你不能使用tab.select(),也不想使用ViewPager,你仍然可以通过编程方式选择一个选项卡。如果你通过TabLayout使用自定义视图。选项卡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在选中和未选中的可绘制对象之间切换,或者类似地处理颜色和/或可绘制对象。

其他回答

如果您在理解上有困难,这段代码可以帮助您

private void MyTabLayout(){
    TabLayout.Tab myTab = myTabLayout.newTab(); // create a new tab
    myTabLayout.addTab(myTab); // add my new tab to myTabLayout
    myTab.setText("new tab"); 
    myTab.select(); // select the new tab
}

你也可以在你的代码中添加这个:

myTabLayout.setTabTextColors(getColor(R.color.colorNormalTab),getColor(R.color.colorSelectedTab));

你可以试着用这个来解决:

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);

TabLayout.Tab tab = tabLayout.getTabAt(pos);
if (tab != null) {
    tab.select();
}

试试这个方法。

tabLayout.setTabTextColors(getResources().getColor(R.color.colorHintTextLight),
                           getResources().getColor(R.color.colorPrimaryTextLight));

我是这样解决的:

void selectPage(int pageIndex){
    tabLayout.setScrollPosition(pageIndex,0f,true);
    viewPager.setCurrentItem(pageIndex);
}

这将不工作的应用程序,有ViewPager2实现,为此,你需要使用

viewPager2.setCurrentItem(position);

当我们使用TabLayoutMediator时,在onConfigureTab中找到onConfigureTab

i.e

TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
                tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                switch (position){
                    case 0 : tab.setIcon(getResources().getDrawable(R.drawable.camera));
                            break;
                    case 1 : tab.setText("CHAT");
                            viewPager2.setCurrentItem(position); // when app starts this will be the selected tab
                            break;
                    case 2 : tab.setText("STATUS");
                            break;
                    case 3 : tab.setText("CALL");
                        break;
                }
            }
        }
        );
        tabLayoutMediator.attach();