viewPager.setCurrentItem后不突出TabLayout标签的文本() [英] TabLayout tab text not highlighted after viewPager.setCurrentItem()

查看:1166
本文介绍了viewPager.setCurrentItem后不突出TabLayout标签的文本()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有附加我ViewPager的TabLayout的问题。再现步骤:


  1. 启动的第一个标签。

  2. 选择第二个选项卡。

  3. preSS后退按钮 - 我的code看到用户是第二个标签并呼吁 viewPager.setCurrentItem(0)返回用户在第一个选项卡。

  4. 然而,正如图片所示,仍然选中第二个选项卡中的文本,而第一个标签的文本是灰色的。 (虽然粉色吧追溯到第一片像它应该。)

在这里输入的形象描述

我在想什么?

  tabLayout =(TabLayout)rootView.findViewById(R.id.tab_layout_main);
tabLayout.addTab(tabLayout.newTab()的setText(getActivity()的getString(R.string.main_tab_grades))。);
tabLayout.addTab(tabLayout.newTab()的setText(getActivity()的getString(R.string.main_tab_schedule))。);viewPager =(NonSwipeableViewPager)rootView.findViewById(R.id.pager_main);
pagerAdapter =新PagerAdapterMain(getActivity(),getChildFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);viewPager.addOnPageChangeListener(新TabLayout.TabLayoutOnPageChangeListener(tabLayout));tabLayout.setOnTabSelectedListener(新TabLayout.OnTabSelectedListener(){
    @覆盖
    公共无效onTabSelected(TabLayout.Tab标签){
        viewPager.setCurrentItem(tab.getPosition());
    }
    @覆盖
    公共无效onTabUnselected(TabLayout.Tab标签){
        返回;
    }    @覆盖
    公共无效onTabReselected(TabLayout.Tab标签){
        返回;
    }
});


解决方案

您可以尝试通过tablayout而不是viewPager选择该选项卡。

  tabLayout.getTabAt(0)。选择();

I'm having an issue with the TabLayout attached to my ViewPager. Repro steps:

  1. Start on the first tab.
  2. Select the 2nd tab.
  3. Press the back button--my code sees that the user is on the second tab and calls viewPager.setCurrentItem(0) to return the user to the first tab.
  4. However, as shown in the picture, the 2nd tab text is still selected while the 1st tab text is grayed out. (Although the pink bar goes back to the 1st tab like it should.)

What am I missing?

tabLayout = (TabLayout) rootView.findViewById(R.id.tab_layout_main);
tabLayout.addTab(tabLayout.newTab().setText(getActivity().getString(R.string.main_tab_grades)));
tabLayout.addTab(tabLayout.newTab().setText(getActivity().getString(R.string.main_tab_schedule)));

viewPager = (NonSwipeableViewPager) rootView.findViewById(R.id.pager_main);
pagerAdapter = new PagerAdapterMain(getActivity(), getChildFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }


    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        return;
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        return;
    }
});

解决方案

you could try to select the tab through the tablayout instead of the viewPager.

tabLayout.getTabAt(0).select();

这篇关于viewPager.setCurrentItem后不突出TabLayout标签的文本()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆