简体   繁体   中英

PagerSlidingTabStrip-Changing icon image of tab and bottomline colour when it is selected

I am using PagerSlidingTabStrip .I want to change the icon image of tab when it is selected and also show a bottom line colour on the selected tab.I tried this but the icon image seems to change only when i press the tab and then return to previous image when i unpress the tab.Also the bottom line colour of tab is same as the rest of the tab.I failed to understand where am i going wrong?I have added attributes name in 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"/>

Code-

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


    }

After reading your question and source codes repeatedly, I want to tell you that no matter whether you press tabs or scroll ViewPager , the icons you added can't be changed. After tabs initialized, icons retain unchanged. You may find it from updateTabStyles() if you ever try to read source codes of PagerSlidingTabStrip , which only change attributes of TextView but not ImageButton which is your icons.
I tried PagerSlidingTabStrip some time ago. And I ever forked this project in Github and extended it. Here is my PagerSlidingTabStrip address . I ever added attributes like pstsTextSelectedColor , pstsTextSize pstsDividerWidth . If you are interested in mine, you can take a look at it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM