Add Icons to SlidingTabLayout instead of Text

后端 未结 11 1975
执笔经年
执笔经年 2020-12-02 23:57

I\'m implementing a SlidingTabLayout in my android application. What my query is that I\'d like to implement icons in my Sliding Tabs instead of texts for navigation. I sear

相关标签:
11条回答
  • 2020-12-03 00:10

    I know that this thread if fairly old, but i wanted to share my solution to this problem nevertheless, because it might be helpful for some. It works quite nice, even for different drawables depending on the selected state.

    First of i did define two array inside the SlidingTabLayout class (this could easily be outsourced to another class):

    private int[] imageResId = {
            R.drawable.multi_random,
            R.drawable.single_random,
            R.drawable.known_person,
    };
    
    private int[] imageResSelected = {
            R.drawable.multi_random_selected,
            R.drawable.single_random_selected,
            R.drawable.known_person_selected
    };
    
    private int mOldPosition = 0;
    

    Now we alter the populateTabStrip() method inside the SlidingTabLayout:

     for (int i = 0; i < adapter.getCount(); i++) {
                View tabView = null;
                ImageView tabImageView = null;
    
                if (mTabViewLayoutId != 0) { // HAS TO BE SET FOR THE MOMENT!
                    // If there is a custom tab view layout id set, try and inflate it
                    tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
                            false);
                    tabImageView = (ImageView) tabView.findViewById(mTabViewTextViewId);
                }
    
                if (tabView == null) {
                    tabView = createDefaultTabView(getContext());
                }
    
                if (tabImageView == null && ImageView.class.isInstance(tabView)) {
                    tabImageView = (ImageView) tabView;
                }
    
                int resourceId;
                if (i == mViewPager.getCurrentItem()) {
                    resourceId = imageResSelected[i];
                    mOldPosition = i;
                } else {
                    resourceId = imageResId[i];
                }
                tabImageView.setImageResource(resourceId);
                tabView.setOnClickListener(tabClickListener);
    
                mTabStrip.addView(tabView);
            }
    

    To update the image according to which tab is selected we add some lines to the onPageSelected method

    // This Method is called once the transition is finished
    // Change the drawable of the old one..
    ImageView view = (ImageView) mTabStrip.getChildAt(mOldPosition);
    view.setImageResource(imageResId[mOldPosition]);
    
    // And now change it of the current one
    view = (ImageView) mTabStrip.getChildAt(position);
    view.setImageResource(imageResSelected[position]);
    
    mOldPosition = position;
    

    Finally we need to add a custom Tab View:

    mSlidingTabLayout.setCustomTabView(R.layout.custom_tab, 0);
    

    Like this one

    <?xml version="1.0" encoding="utf-8"?>
    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:padding="16dp"
              android:layout_weight="1"
              android:src="@drawable/multi_random"
            />
    

    This solution is far from perfect, but works for my needs. Maybe i could help someone with this.

    0 讨论(0)
  • 2020-12-03 00:12

    Your Adapter should implements PagerSlidingTabStrip.IconTabProvider.

    Then in getPageIconResIdyou can return id of image resource:

    public class ViewPagerAdapter extends FragmentPagerAdapter implements 
    PagerSlidingTabStrip.IconTabProvider {
    
       private static final int[] icons = {R.drawable.image1, R.drawable.image2, R.drawable.image3};
    
        @Override
        public int getPageIconResId(int i) {
            return icons[i];
        }
    }
    

    Keep in mind that only image will be shown, text won't.

    0 讨论(0)
  • 2020-12-03 00:13

    I solved same problem, but also with changing drawable on selected tab.

    Create your drawables for tabs, with two states. first_tab_drawable.xml, second_tab_drawable.xml, third_tab_drawable.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/ic_selected" android:state_selected="true"/>
        <item android:drawable="@drawable/ic_normal"/>
    </selector>
    

    Create your own pager adapter, extends from PagerAdapter:

    public class MyPagerAdapter extends PagerAdapter {
    
        private int[] drawablesIds = {
            R.drawable.first_tab_drawable,
            R.drawable.second_tab_drawable,
            R.drawable.third_tab_drawable
        };
    
        //Constructor and other standard funcs...
    
        public int getDrawableId(int position){
            //Here is only example for getting tab drawables
            return drawablesIds[position];
        }
        //...
    }
    

    Change code of SlidingTabLayout:

    private void populateTabStrip() {
        //Here is no more standard PagerAdapter!
        final MyPagerAdapter adapter = (MyPagerAdapter) mViewPager.getAdapter();
    
        final View.OnClickListener tabClickListener = new TabClickListener();
    
        for (int i = 0; i < adapter.getCount(); i++) {
            View tabView = null;
            TextView tabTitleView = null;
    
            if (mTabViewLayoutId != 0) {
                // If there is a custom tab view layout id set, try and inflate it
                tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
                        false);
                tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
            }
    
            if (tabView == null) {
                tabView = createDefaultTabView(getContext());
            }
    
            if (tabTitleView == null && TextView.class.isInstance(tabView)) {
                tabTitleView = (TextView) tabView;
            }
    
            //Set icon, that can be changeable instead setting text.
            //I think the text also can setting here from getPageTitle func.
            //But we interesting only in icon
            tabTitleView.setCompoundDrawablesWithIntrinsicBounds(adapter.getDrawableId(i), 0, 0, 0);
            //Select tab if it is current
            if (mViewPager.getCurrentItem() == i){
                tabView.setSelected(true);
            }
            tabView.setOnClickListener(tabClickListener);
    
            mTabStrip.addView(tabView);
        }
    }
    

    And make really selected TextView title also in SlidingTabLayout in InternalViewPagerListener:

        @Override
        public void onPageSelected(int position) {
    
            //Clear old selection and make new
            for(int i = 0; i < mTabStrip.getChildCount(); i ++){
                mTabStrip.getChildAt(i).setSelected(false);
            }
            mTabStrip.getChildAt(position).setSelected(true);
    
            if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
                mTabStrip.onViewPagerPageChanged(position, 0f);
                scrollToTab(position, 0);
            }
    
            if (mViewPagerPageChangeListener != null) {
                mViewPagerPageChangeListener.onPageSelected(position);
            }
        }
    

    Hope it will be helpful for somebody.

    0 讨论(0)
  • 2020-12-03 00:17

    Extend your main activity from FragmentActivity. Also implement this class from ActionBar.TabListener as we are adding Tabs

    // For Adding Tabs

    for (String tab_name : tabs) {
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.drawable_id)
    .setTabListener(this));
    }
    

    Check here.

    0 讨论(0)
  • 2020-12-03 00:21

    Just returning null from the getPageTitle() method did it for me:

    @Override
     public CharSequence getPageTitle(int position) {
        return null;
    }
    
    0 讨论(0)
  • 2020-12-03 00:22

    I followed Jeremy's answer and its working fine. AFAIK there is no new way of doing this. Most apps are now removing the toolbar altogether and adding a custom slidingtabstrip recommended by google.

    If you have any specific questions let me know. iv got my app looking a lot like the screenshot you have posted. I feel this material design update was focusing more on enabling custom animations and effects.

    0 讨论(0)
提交回复
热议问题