页签就是下面这种东西了:
页签制作
1.在布局文件中声明:
<com.google.android.material.tabs.TabLayout
android:id="@+id/test_home_tablayout"
android:layout_width="match_parent"
android:layout_height="40dp"
app:tabSelectedTextColor="@color/black"
app:tabTextColor="@color/gray" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/test_home_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
2.定义Fragment数组:
private UserFragment userFragment=new UserFragment();
private InfoFragment infoFragment=new InfoFragment();
private CareFragment careFragment=new CareFragment();
private Fragment[] fragments=new Fragment[]{userFragment,infoFragment,careFragment};
3.Java中获得TabLayout和ViewPager:
tablayout = (TabLayout) view.findViewById(R.id.test_home_tablayout);
viewPager = (ViewPager) view.findViewById(R.id.test_home_viewpager);
4.自定义适配器
class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments[position];
}
@Override
public int getCount() {
return tabTitles.length;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
5.使用适配器:
MyPagerAdapter pagerAdapter = new MyPagerAdapter(this.getChildFragmentManager());
Log.d(TAG, "onCreateView: "+"getChildFragmentManager is "+this.getChildFragmentManager());
viewPager.setAdapter(pagerAdapter);
tablayout.setupWithViewPager(viewPager);
tablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
|