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

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

当前回答

只需设置viewpage . setcurrentitem (index)和相关的TabLayout将选择各自的选项卡。

其他回答

这也有帮助

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int i, float v, int i1) {
    }

    @Override
    public void onPageSelected(int i) {
        tablayout.getTabAt(i).select();
    }

    @Override
    public void onPageScrollStateChanged(int i) {
    }
});

这可能不是最终的解决方案,它需要你使用TabLayout和一个ViewPager,但这是我解决它的方法:

void selectPage(int pageIndex)
{
    viewPager.setCurrentItem(pageIndex);
    tabLayout.setupWithViewPager(viewPager);
}

我测试了使用这段代码对性能的影响有多大,首先在Android Studio中运行该方法时查看CPU和内存监视器,然后将其与我自己在页面之间导航时(使用滑动手势)对CPU和内存的负载进行比较,结果发现差异并不大,所以至少这不是一个可怕的解决方案……

希望这能帮助到一些人!

如果你正在使用TabLayout和viewPager,那么这有助于你。你可以在addOnpagelistener中使用ViewPager设置TabLayout。

如果你想直接设置TabLayout的位置(不是点击Tab个人)试试下面的代码TabLayout . gettabat (position_you_want_to_set).select()

   /* will be invoked whenever the page changes or is incrementally scrolled*/
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            tabLayout.getTabAt(position).select();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

有点晚了,但可能是一个有用的解决方案。 我直接在我的片段中使用我的TabLayout,并试图在片段的生命周期中选择一个选项卡。 对我来说有用的是等待TabLayout使用android.view绘制完它的子视图。视图# post方法。即:

int myPosition = 0;
myFilterTabLayout.post(() -> { filterTabLayout.getTabAt(myPosition).select(); });

我使用TabLayout切换片段。它在大多数情况下都是有效的,除了当我试图以编程方式使用tab.select(),我的TabLayout选择一个选项卡时。OnTabSelectedListener将触发onTabSelected(TabLayout。Tab Tab),这会让我很伤心。我正在寻找一种不触发侦听器的方法来做程序化选择。

So I adapted @kenodoggy 's answer to my use. I was further facing a problem where some of the internal objects would return null (because they weren't created yet, because I was answering onActivityResult() from my fragment, which occurs before onCreate() in the case the activity is singleTask or singleInstance) so I wrote up a detailed if/else sequence which would report the error and fall through without the NullPointerException that would otherwise trigger. I use Timber for logging, if you're not using that substitute with Log.e().

void updateSelectedTabTo(int position) {
    if (tabLayout != null){
        int selected = tabLayout.getSelectedTabPosition();
        if (selected != -1){
            TabLayout.Tab oldTab = tabLayout.getTabAt(0);
            if (oldTab != null){
                View view = oldTab.getCustomView();
                if (view != null){
                    view.setSelected(false);
                }
                else {
                    Timber.e("oldTab customView is null");
                }
            }
            else {
                Timber.e("oldTab is null");
            }
        }
        else {
            Timber.e("selected is -1");
        }
        TabLayout.Tab newTab = tabLayout.getTabAt(position);
        if (newTab != null){
            View view = newTab.getCustomView();
            if (view != null){
                view.setSelected(false);
            }
            else {
                Timber.e("newTab customView is null");
            }
        }
        else {
            Timber.e("newTab is null");
        }
    }
    else {
        Timber.e("tablayout is null");
    }
}

这里,tabLayout是绑定到XML中的tabLayout对象的内存变量。我不使用滚动标签功能,所以我把它也删除了。