Add / Delete pages to ViewPager dynamically

前端 未结 9 1108
囚心锁ツ
囚心锁ツ 2021-01-29 23:17

I would like to add or delete pages from my view pager dynamically. Is that possible?

9条回答
  •  失恋的感觉
    2021-01-30 00:13

    you have to set the adapter to viewpager again, then it will refresh the content.

    removeView(int pos) in my PagerAdaper

    public void removeView(int index) {
           imageFileNames.remove(index);
           notifyDataSetChanged();
    }
    

    wherever I am removing the file I have to do like this

    imagePagerAdapter.removeView(currentPosition);
    viewPager.setAdapter(imagePagerAdapter);
    

    EDIT:

    This below method is effective, you can apply the below one.

    public void updateView(int pos){
          viewPager.setAdapter(null);
          imagePagerAdapter =new ImagePagerAdapter(YOUR_CONTEXT,YOUR_CONTENT);
          viewPager.setAdapter(imagePagerAdapter);
          viewPager.setCurrentItem(pos);
    }
    

    replace YOUR_CONTEXT with your context and your content with your content name i.e. updated list or something.

提交回复
热议问题