astuetz / PagerSlidingTabStrip

An interactive indicator to navigate between the different pages of a ViewPager
139 stars 44 forks source link

StateListDrawable doesn't not work with pstsTabBackground #119

Open jianinz opened 10 years ago

jianinz commented 10 years ago

Hi guys,

I am having a trouble when setting different background for each tab with StateListDrawable, here is how my xml looks like

<com.astuetz.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:textColor="@color/iz_grey_dark"
            android:textSize="@dimen/font_size_medium"
            app:pstsShouldExpand="true"
            app:pstsTabBackground="@drawable/selector_tab"/>

/res/drawable/selector_tab.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/shape_tab_pressed"/>
    <item android:state_selected="true" android:drawable="@drawable/shape_tab_pressed"/>
    <item android:drawable="@drawable/shape_tab_normal"/>
</selector>

where pressed and normal state would have different color reference back to

/res/values/colors.xml

The normal color state will always be set, but not selected neither pressed state color will be set. Am i missing something?

jianinz commented 10 years ago

update: it works with android:state_pressed="true", but if i would like to customize the color of current active tab, android:state_selected="true" seems does not work, does anybody know what is wrong here?

mpost commented 10 years ago

@jianinz I think selected is used when navigating with a cursor (eg d-pad). Check the regular item background on how to set a selector: https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable/item_background.xml

jianinz commented 10 years ago

Thanks for pointing it out @mpost.

Redtear commented 9 years ago

@jianinz

I make some change in lib

...
private View preTab;
...
private void addTab(final int position, final View tab) {
        if(position == 0){
            tab.setSelected(true);
            preTab = tab;
        }
        tab.setFocusable(true);
        tab.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(position);
                tab.setSelected(true);
                if(preTab != null) preTab.setSelected(false);
                preTab = tab;
            }
        });

        tab.setPadding(tabPadding, 0, tabPadding, 0);
        tabsContainer.addView(tab, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams);
    }

and now i can add state selected

<item android:state_selected="true" android:drawable="@drawable/check_on"/>

sorry author of this lib? but i can w8 ;)