Android: Viewpager and FragmentStatePageAdapter

后端 未结 5 1188
春和景丽
春和景丽 2021-02-04 15:05

I\'m designing an app that allows users to flip between multiple pages in a ViewPager. I\'ve been struggling trying to figure out how it is possible to remove a Fragment instan

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-04 15:58

    Listing of main.xml

    
    
        
        
    
    

    Setting up the ViewPager

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
    MyPagerAdapter adapter = new MyPagerAdapter(this);
    viewPager.setAdapter(adapter);
    

    The PagerAdapter

    @Override
    public void destroyItem(View view, int arg1, Object object) {
             ((ViewPager) view).removeView((View)object);
    }
    @Override
    public int getCount() {
              return views.size();
    }
    @Override
    public Object instantiateItem(View view, int position) {
               View view = views.get(position);
               ((ViewPager) view).addView(view);
               return view;
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
               return view == object;
    }
    

    look here for more details view pager example

提交回复
热议问题