【Android笔记】ViewPager嵌套fragment
作者:互联网
ViewPager嵌套Fragment
新建4个Fragment;
新建ViewPagerAdapter继承FragmentPagerAdapter;
public class ViewpagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> arrayList;
public ViewpagerAdapter(@NonNull FragmentManager fm,ArrayList<Fragment> arrayList) {
super(fm);
this.arrayList=arrayList;
}
@NonNull
@Override
public Fragment getItem(int position) {
return arrayList.get(position);
}
@Override
public int getCount() {
return arrayList.size();
}
}
在MainActivity中为ViewPager绑定适配器;
arrayList=new ArrayList<>();
arrayList.add(new Fragment1());
arrayList.add(new Fragment2());
arrayList.add(new Fragment3());
arrayList.add(new Fragment4());
viewpagerAdapter= new ViewpagerAdapter(getSupportFragmentManager(),arrayList);
viewpager.setAdapter(viewpagerAdapter);
结语:后续会持续更新哦,喜欢的话记得点赞关注一下吧。
相关视频
【Android进阶】ViewPager嵌套fragment架构解析
标签:fragment,ViewPager,arrayList,Fragment,add,new,Android,public 来源: https://www.cnblogs.com/sdsdw/p/15710618.html