TabLayout + ViewPager 的滑动页面

时间:2023-01-10 05:25:35

1、activity中代码

@Override
public void initData() {
super.initData();
tab=new ArrayList<>();
vp=new ArrayList<>();
tab.add("修理厂人脉");
tab.add("供应商人脉");
tab.add("技师人脉");
tab.add("车主人脉");
vp.add(new Fragment_XiuLiChang());
vp.add(new Fragment_GongYingShang());
vp.add(new Fragment_JiShi());
vp.add(new Fragment_CheZhu());
}

@Override
public void initEvent() {
super.initEvent();
people_quan_ViewPager.setAdapter(new Fragment_ViewPage(getSupportFragmentManager(),vp,tab));
people_quan_tabLayout.setupWithViewPager(people_quan_ViewPager);
}

2、Fragment_ViewPage中代码:

public class Fragment_ViewPage extends FragmentPagerAdapter {
private List<Fragment> fragments;
private List<String> fragment_title;
public Fragment_ViewPage(FragmentManager fm,List<Fragment> fragments,List<String> fragment_title) {
super(fm);
this.fragments=fragments;
this.fragment_title=fragment_title;
}

@Override
public Fragment getItem(int position) {
return fragments.get(position);
}

@Override
public int getCount() {
return fragments.size();
}
//配置标题的方法
@Override
public CharSequence getPageTitle(int position) {
return fragment_title.get(position);
}
}

3、效果图

TabLayout + ViewPager 的滑动页面