How to implement a ViewPager with different Fragments / Layouts

前端 未结 6 827
失恋的感觉
失恋的感觉 2020-11-22 00:32

When I start an activity which implements viewpager, the viewpager created various fragments. I want to use different layouts for each fragment, but the problem is that view

6条回答
  •  旧时难觅i
    2020-11-22 00:44

    Create new instances in your fragments and do like so in your Activity

     private class SlidePagerAdapter extends FragmentStatePagerAdapter {
        public SlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            switch(position){
                case 0:
                    return Fragment1.newInstance();
                case 1:
                    return Fragment2.newInstance();
                case 2:
                    return Fragment3.newInstance();
                case 3:
                    return Fragment4.newInstance();
    
    
                default: break;
    
            }
            return null;
        }
    

提交回复
热议问题