I need to use a custom background for each tab when each one of them is in state_selected mode. But still nothing happens when I select a tab.
I used selector like this: (tab_selector.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/gradient_tab" />
<item android:state_selected="false"
android:drawable="@drawable/tab_unselected" />
<item android:drawable="@drawable/tab_unselected"/>
</selector>
And this is the activity_main.xml:
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/tab_back"
app1:pstsTabBackground="@drawable/tab_selector"
app1:pstsIndicatorColor="#00FFFFFF"
app1:pstsShouldExpand="true" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tabs"
tools:context=".MainActivity" />
As obvious I'm using PagerSlidingTabStrip by astuetz. And here is how I'm using ViewPager and PagerSlidingTabStrip:
final PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(mainPagerAdapter);
tabs.setTextColor(Color.WHITE);
tabs.setViewPager(viewPager);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
tabs.setViewPager(viewPager);
}
I tried mixing different states and re-positioning them, but still not working. I tried to put an specific tab on a selected mode but I could not figure it out.
I figured it out myself, but not in a standard way! Since I needed a gradient like background on my selected tab, I used indicator and underline like this:
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/tab_back"
app1:pstsUnderlineHeight="24dip"
app1:pstsIndicatorHeight="48dip"
app1:pstsIndicatorColor="#e7e7e7"
app1:pstsShouldExpand="true" />
Just for those who are looking for the answer without using the forked PageSliding tab Strip.
For The selector problem, you can set OnPageChangeListener on the tabStrip with a CustomOnPageChangeListener, and set selected_state of the button inside tabStrip to "true"
Here is my CustomOnPageChangeListener
private class CustomOnPageChangeListenner implements ViewPager.OnPageChangeListener{
private PagerSlidingTabStrip tabStrip;
private int previousPage=0;
//Constructor initiate with TapStrip
//
public CustomOnPageChangeListenner(PagerSlidingTabStrip tab){
tabStrip=tab;
//Set the first image button in tabStrip to selected,
((LinearLayout)tabStrip.getChildAt(0)).getChildAt(0).setSelected(true);
}
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int i) {
//set the previous selected page to state_selected = false
((LinearLayout)tabStrip.getChildAt(0)).getChildAt(previousPage).setSelected(false);
//set the selected page to state_selected = true
((LinearLayout)tabStrip.getChildAt(0)).getChildAt(i).setSelected(true);
//remember the current page
previousPage=i;
}
@Override
public void onPageScrollStateChanged(int i) {
}
}
And then you set this custom listener to your tab strip
PagerSlidingTabStrip tabStrip;
tabStrip.setOnPageChangeListener(new CustomOnPageChangeListenner(tabStrip));
I call PagerSlidingTabStrip.setOnPageChangeListener
and not viewPager.setOnPageChangeListener
It works for me.
来源:https://stackoverflow.com/questions/24470524/androidviewpager-pagerslidingtabstrip-custom-tab-background-on-state-selecte