我用图标实现 PagerTabStrip 它会正常工作。但是当我更改选项卡时,我需要当前选项卡图标更改所选图像。
在我的代码下面
我的可绘制图标选择器代码
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_home_selected" android:state_selected="true" />
<item android:drawable="@drawable/ic_home_normal" android:state_selected="false" />
<item android:drawable="@drawable/ic_home_normal" />
</selector>
MainActivity.java
viewPager = (ViewPager) findViewById(R.id.pager);
tabs = (PagerTabStrip) findViewById(R.id.tabs);
fragments = new ArrayList<>();
fragments.add(R.drawable.menu_home_bg);
fragments.add(R.drawable.menu_likes_bg);
fragments.add(R.drawable.menu_matches_bg);
fragments.add(R.drawable.menu_home_bg);
pageAdapter = new TabPagerAdapter(getApplicationContext(), getSupportFragmentManager(), fragments);
viewPager.setAdapter(pageAdapter);
在我的适配器类中添加图标
Drawable image;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
image = context.getResources().getDrawable(tabIcon.get(position), context.getTheme());
} else
image = context.getResources().getDrawable(tabIcon.get(position));
if (image != null)
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
SpannableStringBuilder sb = new SpannableStringBuilder(" ");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
帮助我如何在选项卡更改时更改选择器图标