从ViewPager删除片段

时间:2018-07-26 05:29:17

标签: android android-fragments

当前,我能够从应用程序页面打开一个新片段。我想要“固定”食谱,如果单击“固定”按钮,它将保留在那里。

当前,我存储“固定”配方的ArrayList,并在为配方打开片段时为该列表中的每个项目生成一个片段。

不过,我想做的就是能够通过取消选中图钉按钮来从此列表中删除一项。当前,删除项目非常不可靠,有时它将删除另一个碎片,或者根本不删除任何内容。

适配器:

public class CookingRecipesPagerAdapter extends FragmentStatePagerAdapter {

private final List<CookingRecipeFragment> fragmentList = new ArrayList<>();
private final List<Recipe> fragmentRecipeList = new ArrayList<>();


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

public void addFragment(CookingRecipeFragment fragment, Recipe recipe){
    fragmentList.add(fragment);
    fragmentRecipeList.add(recipe);
}

public CookingRecipeFragment getFragment(int position) {
    return fragmentList.get(position);
}

@Override
public Fragment getItem(int position) {
    return fragmentList.get(position);
}

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

public Recipe getRecipe(int position) {
    return fragmentRecipeList.get(position);
}

public void removeItemAtPosition(int position){
    fragmentList.remove(position);
    fragmentRecipeList.remove(position);
    notifyDataSetChanged();
}

片段生成代码:

        for(int i = 0; i < recipeList.size(); i++) {
        Recipe recipe = recipeList().get(i);
        CookingRecipeFragment fragment = new CookingRecipeFragment();
        Bundle bundl`enter code here`e = new Bundle();
        bundle.putInt("recipe",i);
        fragment.setArguments(bundle);
        adapter.addFragment(fragment,recipe);
    }

我有两个按钮,nextButton和prevButton,用于遍历片段。

        public void onClick(View view) {
            removeCurrentRecipeIfUnpinned();
            try {
                viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
            }
            catch (IndexOutOfBoundsException e){
                //
            }
        }

这是我在不使用片段和项目时尝试删除的内容

public void removeCurrentRecipeIfUnpinned(){
    int index = viewPager.getCurrentItem();
    final CookingRecipesPagerAdapter adapter = ((CookingRecipesPagerAdapter)viewPager.getAdapter());
    CookingRecipeFragment fragment = adapter.getFragment(index);

    if (!fragment.getPinned()){
        adapter.removeItemAtPosition(index);
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }
}

我以前曾尝试在onStop()方法中调用某些删除功能,但这没有用,因为事实证明onStop()的生命周期不合适。我找不到在适当时间调用的任何方法来覆盖。

1 个答案:

答案 0 :(得分:1)

尝试

private class CookingRecipesPagerAdapter extends FragmentPagerAdapter {

    final SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

    public CookingRecipesPagerAdapter() {
        super(getChildFragmentManager());
    }

    @Override
    public Fragment getItem(final int pos) {
        CookingRecipeFragment fragment = new CookingRecipeFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("recipe",i);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * Implementing this method is IMPORTANT for detorying the fragment.
     * Use unique id for each item on pagerData.
     */
    @Override
    public long getItemId(int position) {
        return fragmentRecipeList.get(position).getId(); // Unique id by position and question id. 
    }


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

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

    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }

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

更新1

protected Fragment getCurrentFragment() {
    if (pager.getAdapter() instanceof CookingRecipesPagerAdapter)
        return ((CookingRecipesPagerAdapter) pager.getAdapter()).getRegisteredFragment(currentIndex);
    return null;
}

public void removeCurrentRecipeIfUnpinned(){
    int index = viewPager.getCurrentItem();
    fragmentRecipeList.remove(index);
    adapter.notifyDataSetChanged();
}