TabLayout选项卡选择问题

时间:2017-07-13 15:33:27

标签: android tabs android-tablayout

我制作了TabLayout

当我选择Tab ..文本颜色变化 但是Previous Selected Tab文本颜色没有改变

here

这是我的Java代码 这是片段

set

这里是我的XML

lst = [(u'RHSA-2017:1270', u'Red Hat Enterprise Linux 6'), 
       (u'RHSA-2017:1271', u'Red Hat Enterprise Linux 6'), 
       (u'RHSA-2017:1270', u'Red Hat Enterprise Linux 7')]
seen = set()
result = []
for rhsa, name in lst:
    if name not in seen:
        result.append((rhsa, name))
        seen.add(name)
print(result)
# [('RHSA-2017:1270', 'Red Hat Enterprise Linux 6'), ('RHSA-2017:1270', 'Red Hat Enterprise Linux 7')]

任何人都可以告诉我它的内容 为什么这种行为

2 个答案:

答案 0 :(得分:1)

可以充气自定义视图

    View view = LayoutInflater.from(this).inflate(R.layout.icon_tab, null);
    AppCompatTextView tv = (AppCompatTextView) view.findViewById(R.id.tab);
    ImageView imgTab1 = (ImageView) view.findViewById(R.id.imgTab);
    imgTab1.setImageResource(R.drawable.icon_tab_search);
    imgTab1.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    tv.setText(titles[0]);
    tabLayout.getTabAt(0).setCustomView(view);

布局icon_tab

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/custom_tab_layout_height"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="50dp">

            <ImageView
                android:id="@+id/imgTab"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:padding="12dp"
                android:scaleType="centerInside"
                android:src="@drawable/ic_notification"
                android:adjustViewBounds="true"/>

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/lblCount"
                android:layout_width="17dp"
                android:layout_height="17dp"
                android:layout_marginTop="5dp"
                android:layout_marginRight="10dp"
                android:layout_gravity="end"
                android:visibility="gone"
                android:background="@drawable/shape_tab"
                android:gravity="center"
                android:text="99"
                android:textSize="10sp"
                android:textColor="@color/white"/>

        </FrameLayout>

        <android.support.v7.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:gravity="center"
            android:id="@+id/tab"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="@string/app_name"
            android:drawablePadding="3dp"
            android:textColor="@color/item_tablayout"
            android:textSize="10sp"/>

    </LinearLayout>

</LinearLayout>

//文件夹res / color / item_tablayout.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="@color/white"/>
    <item android:state_focused="true" android:color="@color/white"/>
    <item android:state_selected="true" android:color="@color/white"/>
    <item android:color="@color/skyblue_button"/>

</selector>

答案 1 :(得分:1)

可以通过向您的TabLayout添加 TabSelectListener 来解决此问题,如下所示:

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.setIcon(navActiveIcons[tab.getPosition()]);
                tabLayout.setScrollPosition(tab.getPosition(),0f,true); // This is code that solves the problem
            }

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

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

            }
        });

实际上可以解决问题的代码在 onTabSelected 方法及其tabLayout.setScrollPosition(tab.getPosition(),0f,true);