Android, TabLayout icon color doesn't change when dragging

早过忘川 提交于 2019-12-22 18:23:16

问题


So, I have code like this:

tabLayout.setOnTabSelectedListener(
        new TabLayout.ViewPagerOnTabSelectedListener(tabViewPager) {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                super.onTabSelected(tab);

                //set selected icon color
                Drawable icon = tab.getIcon();
                icon = DrawableCompat.wrap(icon);
                DrawableCompat.setTint(icon, ContextCompat.getColor(MainActivity.this,
                        R.color.colorAccent));
                tab.setIcon(icon);
            }

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

                //set unselected icon color
                Drawable icon = tab.getIcon();
                icon = DrawableCompat.wrap(icon);
                DrawableCompat.setTint(icon, ContextCompat.getColor(MainActivity.this,
                        R.color.white));
                tab.setIcon(icon);
            }

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

When I click on TabLayout icon to change page, everything is ok, but when I drag viewPager, the color of icon doesn't change and it looks like this:


回答1:


That is not how it should be done. TabLayout can choose the icon based on state_selected from StateListDrawable

There are two ways for creating StateListDrawable:

1. API 21+ only solution

Create your tabs like that:

mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
for (int i = 0; i < 4; ++i) {
    TabLayout.Tab tab = mTabLayout.newTab();
    tab.setIcon(R.drawable.icon);
    mTabLayout.addTab(tab, i);
}

where R.drawable.icon is a state drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/drawable_unselected" android:state_selected="false"/>
    <item android:drawable="@drawable/drawable_selected" android:state_selected="true"/>
</selector>

R.drawable.drawable_unselected is you image and R.drawable.drawable_selected looks like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:src="@drawable/drawable_unselected"
            android:tint="@color/answer_color"/>
    </item>
</layer-list>

2. API 4+ Compatible solution

Unfortunately, android:tint inside bitmap tag in drawable xml has been introduced in API 21. On lower API's, programmatic solution is needed.

Here modify the previous code:

mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
for (int i = 0; i < 4; ++i) {
    TabLayout.Tab tab = mTabLayout.newTab();
    StateListDrawable stateDrawable = new StateListDrawable();
    Drawable unSelectedDrawable = ContextCompat.getDrawable(this, R.drawable.drawable_unselected);
    Drawable selectedDrawable = createdSelectedDrawable(this, R.drawable.drawable_unselected);
    stateDrawable.addState(new int[]{-android.R.attr.state_selected}, unSelectedDrawable);
    stateDrawable.addState(new int[]{android.R.attr.state_selected}, selectedDrawable);
    tab.setIcon(stateDrawable);
    mTabLayout.addTab(tab, i);
}

The function for creating state_selected:

private Drawable createdSelectedDrawable(Context context, int iconResource) {
    Bitmap one = BitmapFactory.decodeResource(getResources(), iconResource);
    Bitmap oneCopy = Bitmap.createBitmap(one.getWidth(), one.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(oneCopy);
    Paint p = new Paint();
    p.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(context, android.R.color.holo_red_dark), PorterDuff.Mode.SRC_ATOP));
    c.drawBitmap(one, 0, 0, p);
    return new BitmapDrawable(getResources(), oneCopy);
}


来源:https://stackoverflow.com/questions/39475790/android-tablayout-icon-color-doesnt-change-when-dragging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!