导入依赖
implementation 'com.android.support:design:28.0.0'
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<android.support.v4.view.ViewPager
android:id="@+id/vp_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
private ArrayList<String> mTitle = new ArrayList<>();
private List<Fragment> list1 = new ArrayList<>();
TabLayout tabLayout=v.findViewById(R.id.tabLayout);
ViewPager vp_1=v.findViewById(R.id.vp_1);
list1.add(new table_fragment01());
list1.add(new table_fragment02());
list1.add(new table_fragment03());
mTitle.add("热门影片");
mTitle.add("正在上映");
mTitle.add("即将上映");
vp_1.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public Fragment getItem(int i) {
if (i==0){
return new table_fragment01();
}else if (i==1){
return new table_fragment02();
}
return new table_fragment03();
}
@Override
public int getCount() {
return 3;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mTitle.get(position);
}
});
vp_1.setOffscreenPageLimit(list1.size());
tabLayout.setupWithViewPager(vp_1);
return v;
}