tatePagerAdapter从ViewPager动态删除项目

tatePagerAdapter从ViewPager动态删除项目

本文介绍了使用FragmentStatePagerAdapter从ViewPager动态删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

围绕该主题有很多讨论

  • ViewPager PagerAdapter not updating the View
  • Update ViewPager dynamically?
  • Removing fragments from FragmentStatePagerAdapter

我尝试了各种解决方案(包括使用POSITION_NONE无效).但是我仍然不知道如何正确删除项目.

I have tried various solutions (including the invalidation with POSITION_NONE). But I still donT know how to remove an item properly.

会发生什么事

  • 要么我得到一张空白页(这意味着该片段已被破坏,但instantiateItem没有被要求替换)
  • 或者整个崩溃可能是因为Android管理片段实例与我将它们保留在我的方式中不匹配arraylist/sparsearray
  • either I get a blank page (meaning the fragment is destroyed, but theinstantiateItem was not called for a replacement)
  • or the whole thing crashes probably because the way the Android manages thefragment instances do not match how I keep them in myarraylist/sparsearray

这是我的适配器

private class DatePickerPagerAdapter extends FragmentStatePagerAdapter {

    ArrayList<Fragment> registeredFragments = new ArrayList<Fragment>();

    public DatePickerPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return CreateWishFormDatePaginationFragment.newInstance(position);
    }

    @Override
    public int getItemPosition(Object object){ //doesnt change much..
        return PagerAdapter.POSITION_NONE;
    }

    @Override
    public int getCount() {
        return iPageCount;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.add(position, fragment);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, registeredFragments.get(position));
    }

    public void removePage(ViewGroup pager, int position) {
        destroyItem(pager, position, null);
        registeredFragments.remove(position);
        iPageCount--;
        pagerIndicator.notifyDataSetChanged();
        pagerAdapter.notifyDataSetChanged();
    }

    public void addPage() {

        iPageCount++;
        pagerIndicator.notifyDataSetChanged();
        pagerAdapter.notifyDataSetChanged();
    }
}

例如,我正在将View pager与ViewPagerIndicator一起使用,并且希望能够删除其间的页面.

I am using a view pager with ViewPagerIndicator and I want to be able to remove a page in between, for example.

因此仍然是一个问题,在ViewPager中处理添加和删除片段的正确方法是什么?

Hence remains the question, what is the proper way handling addition and removal of fragments in a ViewPager?

谢谢!

推荐答案

如果要从ViewPager中删除项目,则以下代码没有意义:

If you want to remove items from a ViewPager, this following code does not make sense:

@Override
public Fragment getItem(int position) {
    return CreateWishFormDatePaginationFragment.newInstance(position);
}

基本上,您将基于position创建一个Fragment.无论您删除哪个页面,position的范围都会从 [0,iPageCount)更改为 [0,iPageCount-1),这意味着它总是会删除最后一个Fragment.

Essentially, you create a Fragment based on the position. No matter which page you remove, the range of the position will change from [0, iPageCount) to [0, iPageCount-1), which means that it will always get rid of the last Fragment.

您所需的或多或少是以下内容:

What you need is more or less the following:

public class DatePickerPagerAdapter extends FragmentStatePagerAdapter
{
    private ArrayList<Integer> pageIndexes;

    public DatePickerPagerAdapter(FragmentManager fm) {
        super(fm);
        pageIndexes = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            pageIndexes.add(new Integer(i));
        }
    }

    @Override
    public int getCount() {
        return pageIndexes.size();
    }

    @Override
    public Fragment getItem(int position) {
        Integer index = pageIndexes.get(position);
        return CreateWishFormDatePaginationFragment.newInstance(index.intValue());
    }

    // This is called when notifyDataSetChanged() is called
    @Override
    public int getItemPosition(Object object) {
        // refresh all fragments when data set changed
        return PagerAdapter.POSITION_NONE;
    }

    // Delete a page at a `position`
    public void deletePage(int position)
    {
        // Remove the corresponding item in the data set
        pageIndexes.remove(position);
        // Notify the adapter that the data set is changed
        notifyDataSetChanged();
    }
}

请参阅此完整的示例,以获取有关从FragmentStatePagerAdapter删除项目的更多详细信息.

Please refer to this complete example for more details about removing item from FragmentStatePagerAdapter.

这篇关于使用FragmentStatePagerAdapter从ViewPager动态删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 03:07