安卓 TabLayout 选项卡
- TabLayout简介
- Fragment+ViewPager+TabLayout组合使用
TabLayout简介
Tablayout继承自HorizontalScrollView用于页面切换指示器
TabLayout 是属于 com.android.support:design 包的控件,所以需要依赖该包
implementation 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.2@aar'
Fragment+ViewPager+TabLayout组合使用
activity的xml文件
<?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=".mvp.view.activity.MainActivity"><androidx.viewpager.widget.ViewPagerandroid:id="@+id/vp"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="8"></androidx.viewpager.widget.ViewPager><com.flyco.tablayout.CommonTabLayoutapp:tl_textSelectColor="#F10A0A"app:tl_textUnselectColor="#0E0E0E"android:id="@+id/tab"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></com.flyco.tablayout.CommonTabLayout></LinearLayout>
activity的java文件
public class MainActivity extends AppCompatActivity {private ViewPager vp;private CommonTabLayout tab;private List<Fragment> flist = new ArrayList<>();private List<CustomTabEntity> tlist = new ArrayList<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//主页面切换vp = (ViewPager) findViewById(R.id.vp);tab = (CommonTabLayout) findViewById(R.id.tab);//添加数据tlist.add(new TabBean("首页",R.drawable.homeyes,R.drawable.homeno));tlist.add(new TabBean("分类",R.drawable.soyes,R.drawable.sono));tlist.add(new TabBean("购物车",R.drawable.shoppyes,R.drawable.shoppno));tlist.add(new TabBean("我的",R.drawable.meyes,R.drawable.meno));tab.setTabData((ArrayList<CustomTabEntity>) tlist);//添加数据flist.add(new Fragment1());flist.add(new Fragment2());flist.add(new Fragment2());flist.add(new Fragment2());//内部适配器vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {@NonNull@Overridepublic Fragment getItem(int position) {return flist.get(position);}@Overridepublic int getCount() {return flist.size();}});//tab标题关联viewpagertab.setOnTabSelectListener(new OnTabSelectListener() {@Overridepublic void onTabSelect(int position) {vp.setCurrentItem(position);}@Overridepublic void onTabReselect(int position) {}});//viewpager关联tab标题vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {tab.setCurrentTab(position);}@Overridepublic void onPageSelected(int position) {}@Overridepublic void onPageScrollStateChanged(int state) {}});}
}
创建一个tab的bean类
public class TabBean implements CustomTabEntity {private String string;private int yes;private int no;public TabBean(String string, int yes, int no) {this.string = string;this.yes = yes;this.no = no;}@Overridepublic String getTabTitle() {return string;}@Overridepublic int getTabSelectedIcon() {return yes;}@Overridepublic int getTabUnselectedIcon() {return no;}
}
内容fragment创建出来就行内容不需要改动
效果