viewpager setCurrentItem不改变tablayout选择的颜色

时间:2016-02-02 10:21:51

标签: android android-viewpager android-tablayout

我正在使用viewpager和标签布局

     viewPager.setAdapter(Adapter);
   tablayout.setupWithViewPager(viewPager);

   viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        int cPage;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {


                cPage = position;

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

现在当我滑动选中的标签颜色正在改变但我打电话时

                viewPager.setCurrentItem(1, true);

它没有改变新的标签布局颜色。说  我有2个标签,当我是第一个它为第一个标签设置文本颜色绿色,当我滑动它给第二个标签颜色绿色..但当我打电话

            viewPager.setCurrentItem(1, true);

在我的第一个标签中是绿色而不是第二个标签

6 个答案:

答案 0 :(得分:6)

https://stackoverflow.com/a/35843370/4302046

  

// mViewPager.setCurrentItem(position);

     

mTab​​Layout.getTabAt(位置)。选择();

答案 1 :(得分:5)

试试这个对我有用......

tabLayout.setScrollPosition(position,0f,true);

答案 2 :(得分:1)

在您的活动中:

 // init TabLayout
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        if (mCallFrom.equalsIgnoreCase(ActivityConstantUtils.sCalledFromActPanel)) {
            // To add Memory
            tabLayout.addTab(tabLayout.newTab().setText(R.string.add_memory_caption));
            tabLayout.addTab(tabLayout.newTab().setText(R.string.add_memory_effects));
//            tabLayout.addTab(tabLayout.newTab().setText(R.string.add_memory_frames));
        } else {
            // To add Memory from Recipe & Blog
            tabLayout.addTab(tabLayout.newTab().setText(R.string.add_memory_caption));
            tabLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT);
        }
        changeTabsFont(tabLayout);
        showDivider(tabLayout);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        final ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter(this.getFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 1) {
                    Utils.hideSoftKeyboard(view, getActivity());
                }
                viewPager.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        });

//样式适用于tablayout

<style name="ActPanelTabLayout.Theme" parent="Widget.Design.TabLayout">
        <item name="tabIndicatorColor">@color/memory_color_80</item>\
        <item name="android:textColor">@color/act_tab_black</item>
        <item name="tabSelectedTextColor">@color/act_tab_selector_text_color</item>
        <item name="tabTextAppearance">@style/TextAppearance.Jacksonville.ActTab</item>
    </style>

答案 3 :(得分:0)

面临独特的问题。当我们设置setCurrentItem时。它不会更改tablayout的选项卡。然后你必须在viewpager上添加onOPageChangeListener,你必须手动为所选的viewpager的位置选择tablayout的选项卡。然后setupWithViewPager。

注意:只有在添加addOnPageChangeListener后才需要设置setupWithViewPager。天知道为什么。这是有效的。如果我以前setupWithViewPager,它不起作用。再一次,全能者只知道。

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

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

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

            /*
              NOTE: This is setup after addOnPageChangeListener. Don't know why but this is what works. Otherwise tabLayout.does not select.
            */
            tabLayout.setupWithViewPager(this.viewPager);

答案 4 :(得分:0)

发生相同的问题,在滑动ViewPager后,即在滑动ViewPager时,TabLayout中的选项卡未处于选中状态。 解决方案(科特琳):

viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

        override fun onPageSelected(position: Int) {
            viewPager.setCurrentItem(position, false)
            tabLayout.getTabAt(position)?.select()
        }

        override fun onPageScrollStateChanged(state: Int) {}
    })

答案 5 :(得分:-1)

这是设计支持库中的已知错误,即使在它存在的版本24.1.1中,我能够通过使用协调器布局作为父(根)布局来找到解决此问题的方法