android view selected,Android:ViewPager - PagerSlidingTabStrip, Custom Tab Background on state_selec...

问题

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)

android:drawable="@drawable/gradient_tab" />

android:drawable="@drawable/tab_unselected" />

And this is the activity_main.xml:

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: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.

回答1:

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:

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" />

回答2:

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

回答3:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值