Tablayout with icons only

后端 未结 12 1391
暗喜
暗喜 2020-12-04 11:32

I am using design support to create tabs. I am also using ViewPager for swipable tabs.

Now, I don\'t know how to use only icons instead of texts in tabs

12条回答
  •  旧巷少年郎
    2020-12-04 11:59

    None of these methods are elegant when using TabLayout as the ViewPager "decor" scenario. TabLayout Documentation Here is a simple extension of TabLayout and PagerAdapter that provides a simple drop in replacement for TabLayout that should be able to be used in either scenario without manually adding icons outside of the TabLayout class and following the usage of PagerAdapter to get the tab information.

    /**
     * Created by JDL on 1/18/2017.
     */
    public class TabLayoutExt extends TabLayout {
    
        protected ViewPager mViewPager;
    
        public abstract static class TabLayoutViewPagerAdapter extends PagerAdapter {
            public TabLayoutViewPagerAdapter() {
            }
    
            /**
             * This method may be called by the TabLayout to obtain an icon drawable
             * to for the specified tab. This method may return null
             * indicating no tab icon for this page. The default implementation returns
             * null.
             *
             * @param position The position of the title requested
             * @return A drawable icon for the requested page
             */
            public Drawable getPageIcon(Context context, int position) {
                return null;
            }
        }
    
        public TabLayoutExt(Context context) {
            super(context);
        }
    
        public TabLayoutExt(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public TabLayoutExt(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onAttachedToWindow() {
            //Cover the implicit setup in TabLayout
            if (mViewPager == null) {
                final ViewParent vp = getParent();
                if (vp instanceof ViewPager) {
                    mViewPager = (ViewPager)vp;
                }
    
            }
            super.onAttachedToWindow();
        }
    
        public void addTab(@NonNull Tab tab, int position, boolean setSelected) {
            if (mViewPager != null && mViewPager.getAdapter() instanceof TabLayoutViewPagerAdapter) {
                Drawable icon = ((TabLayoutViewPagerAdapter) mViewPager.getAdapter()).getPageIcon(getContext(),position);
                tab.setIcon(icon);
            }
            super.addTab(tab,position,setSelected);
    
        }
    
        @Override
        public void setupWithViewPager(@Nullable ViewPager viewPager, boolean autoRefresh) {
            mViewPager = viewPager;
            super.setupWithViewPager(viewPager, autoRefresh);
        }
    }
    

    So all that needs be done is extend TabLayoutViewPagerAdapter instead of PageAdapter and implement getPageIcon(Context,int) instead of or in addition to title. The drop in TabLayoutExt in your XML file, instead of the normal TabLayout. This could be extended for further modifying the tab, either with a custom view instead or something else.

提交回复
热议问题