Android tabLayout 最后选择的选项卡颜色未正确更新

Android tabLayout last selected tab color is not updating correctly

我有一个包含 4 个选项卡的表格布局。当我 select 一个选项卡时,该选项卡的文本和图标应该是红色的,而其他选项卡应该是灰色的。

为了更新图标,我创建了两个不同的版本,并在我覆盖的 OnTabSelectedListener 中更新了它,但是当我将它添加到 tablayout 时,文本颜色似乎 "lag behind"。最后一个 selected 选项卡文本仍然是红色的,并且在我按下另一个新选项卡之前不会更新。

当我删除 OnTabSelectedListener 时,颜色可以正常工作,但是我无法更新图标...我也无法直接在 tabLayout.tab 项目上更新文本颜色。

这是 Android 中的错误还是我遗漏了什么?


activity_tab.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.oivind.tabsexample.TabActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom">

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorHeight="0dp"
        app:tabTextColor="@android:color/darker_gray"
        app:tabSelectedTextColor="@android:color/holo_red_light"
        android:background="@android:color/white"
        style="@style/NASTabLayout">

    </android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

tabActivity.java onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    viewPager = (ViewPager) findViewById(R.id.container);
    viewPager.setAdapter(mSectionsPagerAdapter);

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

    tabLayout.getTabAt(0).setIcon(R.drawable.ic_icon_error);
    for(int i = 1; i < tabLayout.getTabCount(); i++) {
        tabLayout.getTabAt(i).setIcon(R.drawable.ic_icon_error_grey);
    }

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            tab.setIcon(R.drawable.ic_icon_error);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            tab.setIcon(R.drawable.ic_icon_error_grey);
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            tab.setIcon(R.drawable.ic_icon_error_grey);
        }
    });
}

这是一个快速修复,并没有解决原始问题

通过循环浏览选项卡并在每次点击时设置灰色图标来修复此问题:

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                for(int i = 0; i < tabLayout.getTabCount(); i++) {
                    tabLayout.getTabAt(i).setIcon(R.drawable.ic_icon_error_grey);
                }
                tab.setIcon(R.drawable.ic_icon_error);
            }

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

            }

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

            }
        });

如果有人知道如何正确地做到这一点,请告诉。 :)

我也有这个错误,我通过在 onTabSelected 的末尾添加 tabLayout.setScrollPosition(tab.getPosition(),0f,true); 来解决它,如下所示:

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.setIcon(navActiveIcons[tab.getPosition()]);
                tabLayout.setScrollPosition(tab.getPosition(),0f,true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setIcon(navIcons[tab.getPosition()]);
            }

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

            }
        });