implementation ‘com.flyco.tablayout:FlycoTabLayout_Lib:2.0.2@aar’
<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_weight="1"
android:id="@+id/vp"
android:layout_height="0dp">
</androidx.viewpager.widget.ViewPager>
<com.flyco.tablayout.CommonTabLayout
android:layout_width="match_parent"
android:id="@+id/ctab"
app:tl_textSelectColor="#DF0C0C"
app:tl_textUnselectColor="#000"
android:layout_height="wrap_content">
</com.flyco.tablayout.CommonTabLayout>
</LinearLayout>
package com.demo.myapplication;
import com.flyco.tablayout.listener.CustomTabEntity;
public class CustomTabEntityDemo implements CustomTabEntity {
private String tab;
private int SelectedIcon;
private int TabUnselectedIcon;
public CustomTabEntityDemo(String tab, int selectedIcon, int tabUnselectedIcon) {
this.tab = tab;
SelectedIcon = selectedIcon;
TabUnselectedIcon = tabUnselectedIcon;
}
@Override
public String getTabTitle() {
return tab;
}
@Override
public int getTabSelectedIcon() {
return SelectedIcon;
}
@Override
public int getTabUnselectedIcon() {
return TabUnselectedIcon;
}
}
package com.demo.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
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 List<Fragment> fragments = new ArrayList<>();
private ArrayList<CustomTabEntity> customTabEntities = new ArrayList<>();
private ViewPager mViewPager;
private CommonTabLayout mCommon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
//找控件
mViewPager = (ViewPager) findViewById(R.id.vp);
mCommon = (CommonTabLayout) findViewById(R.id.ctab);
//添加fragment
fragments.add(new Framgentone());
fragments.add(new Framgenttow());
//传入我们封装的3个参数
CustomTabEntityDemo demo1 = new CustomTabEntityDemo("第一页", R.mipmap.ic_launcher, R.mipmap.ic_launcher_round);
CustomTabEntityDemo demo2 = new CustomTabEntityDemo("第二页", R.mipmap.ic_launcher, R.mipmap.ic_launcher_round);
//存放到集合当中
customTabEntities.add(demo1);
customTabEntities.add(demo2);
//设置我们的tablayout页面
mCommon.setTabData(customTabEntities);
//设置viewpager适配器
mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
});
//设置CommonTabLayout 点击时间
mCommon.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelect(int position) {
mViewPager.setCurrentItem(position);
}
@Override
public void onTabReselect(int position) {
}
});
//设置viewpager滑动事件
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mCommon.setCurrentTab(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}