导入依赖
implementation ‘com.flyco.tablayout:FlycoTabLayout_Lib:2.0.2@aar’//flycotablayout
1.xml布局中与ViewPager联合使用
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_weight="10"
android:layout_width="match_parent"
android:layout_height="0dp"></androidx.viewpager.widget.ViewPager>
<com.flyco.tablayout.CommonTabLayout
app:tl_textSelectColor="#FFEB3B"
app:tl_textUnselectColor="#000"
android:id="@+id/common"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"></com.flyco.tablayout.CommonTabLayout>
</LinearLayout>
在activity中代码
package com.example.day012;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.day012.adapter.FgAdapter;
import com.example.day012.fragment.FourFragment;
import com.example.day012.fragment.OneFragment;
import com.example.day012.fragment.ThreeFragment;
import com.example.day012.fragment.TwoFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private CommonTabLayout ctab;
private ArrayList<CustomTabEntity> list=new ArrayList<>();
private FgAdapter fgAdapter;
private List<Fragment> list2=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
list.add(new MyTab("首页",R.drawable.a,R.drawable.b));
list.add(new MyTab("消息",R.drawable.a,R.drawable.b));
list.add(new MyTab("朋友",R.drawable.a,R.drawable.b));
list.add(new MyTab("我的",R.drawable.a,R.drawable.b));
ctab.setTabData(list);
//显示未读消息
ctab.showDot(3);//显示小圆点
ctab.showMsg(2,888);
ctab.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelect(int position) {
Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_SHORT).show();
vp.setCurrentItem(position);
ctab.hideMsg(position);
}
@Override
public void onTabReselect(int position) {
}
});
list2.add(new OneFragment());
list2.add(new TwoFragment());
list2.add(new ThreeFragment());
list2.add(new FourFragment());
fgAdapter=new FgAdapter(getSupportFragmentManager(),this,list2);
vp.setAdapter(fgAdapter);
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
ctab.setCurrentTab(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void initView() {
vp = (ViewPager) findViewById(R.id.vp);
ctab = (CommonTabLayout) findViewById(R.id.ctab);
}
}
设置CustomTabEntity数组
package com.example.day012;
import com.flyco.tablayout.listener.CustomTabEntity;
public class MyTab implements CustomTabEntity {
private String title;
private int selectIcon;
private int unselectIcon;
public MyTab(String title, int selectIcon, int unselectIcon) {
this.title = title;
this.selectIcon = selectIcon;
this.unselectIcon = unselectIcon;
}
@Override
public String getTabTitle() {
return title;
}
@Override
public int getTabSelectedIcon() {
return selectIcon;
}
@Override
public int getTabUnselectedIcon() {
return unselectIcon;
}
}