PagerSlidingTabStrip - 更改选项卡和底线颜色的图标图像

时间:2015-04-22 09:30:08

标签: android android-viewpager pagerslidingtabstrip

我正在使用PagerSlidingTabStrip。我想在选中时更改标签的图标图像,并在所选标签上显示底线颜色。我试过这个但是图标图像似乎只有在我改变时才会改变当我取消选项卡时,按下选项卡然后返回上一个图像。选项卡的底线颜色与选项卡的其余部分相同。我无法理解我在哪里出错?我在attrs.xml中添加了属性名称

tabhome.xml

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

XML

<com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        app:pstsIndicatorColor="#5D349E"
        app:pstsUnderlineColor="#5D349E"
        app:pstsUnderlineHeight="10dip"
        app:pstsDividerColor="#8E58D4"
        app:pstsTabSwitch="true"
        app:pstsTabBackground="@color/tabbackground"/>

代码 -

    ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        tabs.setViewPager(pager);
   private class MyPagerAdapter extends FragmentPagerAdapter  implements PagerSlidingTabStrip.IconTabProvider {
        private String[] titles={"Home", "Photos","Audios", "Videos","Memes","Channels","Giphy","Flickr"};
        private final int[] ICONS = { R.drawable.tabhome,R.drawable.tabphoto,
                           R.drawable.tabaudio, R.drawable.tabvideo,R.drawable.tabmeme ,R.drawable.tabgif,R.drawable.tabgif,R.drawable.tabflickr};
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
        @Override
        public int getPageIconResId(int i) {
            return ICONS[i];
        }
        @Override
        public android.support.v4.app.Fragment getItem(int pos) {
            Bundle args = new Bundle();

            switch (pos) {

                                case 0:
                                    currentFragment = new HomeFragment();




                                    break;
                                case 1:

                                    currentFragment = new HomeFragment5();




                                    break;
                                case 2:

                                    currentFragment = new HomeFragment4();



                                    break;
                                case 3:

                                    currentFragment = new HomeFragment3();



                                    break;
                                case 4:

                                    currentFragment = new HomeFragment4();



                                    break;
                                case 5:
                                    currentFragment = new ChlHome();


                                    break;
                                case 6:
                                    currentFragment = new GiFragment();
                                    break;
                                case 7:
                                    currentFragment = new FlList();
                                    break;
                                default:
                            }
                            return currentFragment;
                        }
        @Override
        public int getCount() {
            return 8;
        }


    }

1 个答案:

答案 0 :(得分:0)

在反复阅读您的问题和源代码后,我想告诉您,无论您是按tabs还是滚动ViewPager,您添加的图标都无法更改。标签初始化后,图标保持不变。如果您尝试阅读updateTabStyles()的源代码,则可以从PagerSlidingTabStrip找到该代码,该代码仅更改TextView但不更改ImageButton的属性,这是您的图标。 我不久前试过PagerSlidingTabStrip。我曾经在Github上分配这个项目并扩展它。这是我的PagerSlidingTabStrip address。我添加了pstsTextSelectedColorpstsTextSize pstsDividerWidth等属性。如果你对我感兴趣,你可以看看它。