乐趣区

关于android: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 架构解析

退出移动版