【Android筆記】ViewPager巢狀fragment
阿新 • • 發佈:2021-12-20
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);
結語:後續會持續更新哦,喜歡的話記得點贊關注一下吧。