FlycoTabLayout - SegmentTabLayout的使用

一:导入依赖:

implementation 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'

二:布局文件:

布局文件:SegmentTabLayout+viewpager


  

    
    


    
    

三:代码:

1.设置viewpager适配器:

public class ViewPagerAdapter extends FragmentPagerAdapter {
    private List list;

    public ViewPagerAdapter(FragmentManager fm, List list) {
        super(fm);
        this.list = list;
    }

    @Override
    public Fragment getItem(int i) {
        return list.get(i);
    }

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

2.添加标题

    String[] mTitles = {"1","2","3","4","5"};
    tabLayout.setTabData(mTitles);

3.设置segmentTabLayout+viewpager+fragment联动:

    mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),list);

        vp.setAdapter(mViewPagerAdapter);

        //滑动、点击切换页面
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vp.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        //滑动、点击切换页面
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                tabLayout.setCurrentTab(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

4.设置segmentTabLayout属性(根据自己的需求):

    tabLayout.setIndicatorCornerRadius(10);//设置圆角
    tabLayout.setTextSelectColor(Color.WHITE);//选中文字颜色
    tabLayout.setTextUnselectColor(Color.parseColor("#007AFF"));//未选中文字颜色
    tabLayout.setIndicatorColor(Color.parseColor("#007AFF"));//选中区域颜色
    tabLayout.setDividerColor(Color.parseColor("#007AFF"));//设置分割线颜色
    tabLayout.setIndicatorMargin(1,1,1,1);//设置内间距

还可以设置更多属性,详情请看 这里

 

 

 

 

你可能感兴趣的:(FlycoTabLayout - SegmentTabLayout的使用)