ViewPager2+自定义TabLayout

布局文件

app:tabIndicatorFullWidth="false"

app:tabPaddingStart="0dp"

app:tabPaddingEnd="0dp"

app:tabUnboundedRipple="@null"

这些属性比较重要

接下来是代码片段

自定义tabLayout代码片段

ColorStateList colorStateList = tabs.getTabTextColors();

TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabs, viewPager, (tab, position) -> { 

    View view = LayoutInflater.from(getContext()).inflate(R.layout.item_tab,tab.parent,false);     TextView month = view.findViewById(R.id.tv_month); 

    TextView day = view.findViewById(R.id.tv_day);

    String str1 = dateList.get(position).getName().substring(0,3); 

    month.setText(str1); 

    String str2 = dateList.get(position).getName().substring(3);     if(toDay.equals(dateList.get(position).getType())){ 

        day.setText("今天");

     }else { 

        day.setText(str2);

     } 

    month.setTextColor(colorStateList); 

    day.setTextColor(colorStateList);

     tab.setCustomView(view);

    }

);

//这句话很重要

tabLayoutMediator.attach();

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容