Android:ViewPager - PagerSlidingTabStrip, Custom Tab Background on state_selected

半城伤御伤魂 提交于 2019-12-04 16:36:40

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));
David Guo

I call PagerSlidingTabStrip.setOnPageChangeListener and not viewPager.setOnPageChangeListener

It works for me.

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