Android TabLayout加强版SlidingTabLayout

             Android TabLayout加强版SlidingTabLayout

众所周知TabLayout纯在很多问题和不足,因此各路神仙对其各种修改,在GitHub上比较被肯定三个TabLayout其中一个就是

SlidingTabLayout,接下来我简单介绍一下使用:

首先:依赖和XML配置

//FlycoTabLayout //进阶tablayout 
    compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/theme_color"
        android:orientation="vertical"
        android:paddingBottom="20px">

        <com.flyco.tablayout.SlidingTabLayout
            android:id="@+id/fragment_discover_tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/theme_color"
            android:paddingBottom="20px"
            android:paddingLeft="20px"
            android:paddingRight="20px"
            android:paddingTop="10px"

            app:tl_indicator_color="@color/white"
            app:tl_indicator_corner_radius="6px"
            app:tl_indicator_height="6px"
            app:tl_indicator_width_equal_title="true"
            app:tl_tab_padding="20px"
            app:tl_textSelectColor="@color/white"
            app:tl_textUnselectColor="@color/white"
            app:tl_textsize="17sp"


            />

    </LinearLayout>
    
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:id="@+id/fragment_discover_vp"/>

tablayout高度写死可以拉开横条和字的距离

这边注意了   一个1像素的ViewPager,  这是SlidingTabLayout必须要有的 GitHub上有说明.

我用不到ViewPager但是为了不让它报错,这边写了一个一像素高的ViewPager;

你如果问我,SlidingTabLayout这么麻烦,为什么还要使用它? !      是因为指示器!!! 弧度! 长短! 距离!   原生的能行吗?!!!

定义和配置:

private SlidingTabLayout mTabLayout;
private ViewPager viewPager;
mTabLayout = view.findViewById(R.id.fragment_discover_tablayout);
//1像素高的ViewPager 意图适配SlidingTabLayout
viewPager = view.findViewById(R.id.fragment_discover_vp); 
viewPager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {

                    @Override
                    public Fragment getItem(int position) {
                        return new mFragment_user();
                    }

                    @Override
                    public int getCount() {
                        return mTitles.size();
                    }

                    //ViewPager与TabLayout绑定后,这里获取到PageTitle就是Tab的Text
                    @Override
                    public CharSequence getPageTitle(int position) {
                        return mTitles.get(position).getCategory();
                    }

                });
                mTabLayout.setViewPager(viewPager);
                if (mTitles.size() > 1) {
                    mTabLayout.setCurrentTab(1);// todo  默认选中 //第一次加载设置默认
                }

这样就完事了,监听什么的就不说了.

源码地址:https://github.com/H07000223/FlycoTabLayout

发布了54 篇原创文章 · 获赞 212 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_39731011/article/details/87880634
今日推荐