1.依赖:
//flycoTabLayout依赖
implementation ‘com.flyco.tablayout:FlycoTabLayout_Lib:2.0.2@aar’
2.介绍:
1.FlycoTabLayout 是一个一个安卓的Tablayout库,其中主要包含了CommonTabLayout,SegmenTabLayout,SlidingTabLayout
2.CommonTabLayout:不同于SlidingTabLayout对ViewPager依赖,它是一个不依赖ViewPager可以与其他控件自由搭配使用的TabLayout.
3.注意我们用的是:CommonTabLayout
注意我们自己写的数据源Bean类要 implements CustomTabEntity
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".tablayout.QQTabActivity"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fragme_layout"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="0dp"></FrameLayout>
<com.flyco.tablayout.CommonTabLayout
app:tl_textSelectColor="#8BC34A"
app:tl_textUnselectColor="#000"
android:id="@+id/tab"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"></com.flyco.tablayout.CommonTabLayout>
</LinearLayout>
代码:
public class QQTabActivity extends AppCompatActivity{
CommonTabLayout commonTabLayout;
//注意数据源的类型
ArrayList<CustomTabEntity> tabEntitys=new ArrayList<>();
Fragment fragment1,fragment2,fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qqtab);
commonTabLayout=findViewById(R.id.tab);
fragment1=new Fragment1();
fragment2=new Fragment2();
fragment3=new Fragment3();
//TODO 1:设置tablayout的样式
tabEntitys.add(new MyTab("消息",R.mipmap.select1,R.mipmap.select2));
tabEntitys.add(new MyTab("我的",R.mipmap.select1,R.mipmap.select2));
tabEntitys.add(new MyTab("你的",R.mipmap.select1,R.mipmap.select2));
commonTabLayout.setTabData(tabEntitys);
//TODO 2:设置点击事件
commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelect(int position) {//选中
if(position==0){
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragme_layout,fragment1)
.commit();
}else if(position==1){
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragme_layout,fragment2)
.commit();
//消失
commonTabLayout.hideMsg(1);
}else if(position==2){
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragme_layout,fragment3)
.commit();
commonTabLayout.hideMsg(2);
}
}
@Override
public void onTabReselect(int position) {//再一次选中
}
});
//TODO 3:显示未读消息
commonTabLayout.showDot(1);//小红点
commonTabLayout.showMsg(2,400);//设置消息个数
}
}