gpt4 book ai didi

android - 从 ViewPager 中删除 fragment

转载 作者:行者123 更新时间:2023-11-29 01:00:35 25 4
gpt4 key购买 nike

目前,我可以从我的应用程序页面打开一个新 fragment 。我想要“固定”食谱,如果单击固定按钮,它们将保留在那里。

目前,我存储了一个“固定”食谱的 ArrayList,每当为食谱打开一个 fragment 时,都会为该列表中的每个项目生成一个 fragment 。

不过,我想要做的是能够通过取消选中固定按钮来从此列表中删除项目。目前,删除项目非常不可靠,有时会删除不同的 fragment ,或者根本不删除任何内容。

适配器:

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();
}

fragment 生成代码:

        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,用于在 fragment 中移动。

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

下面是我尝试删除未使用的 fragment 和项目的方法

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() 的生命周期被证明是不合适的。我无法找到任何在适当时间调用的覆盖方法。

最佳答案

试试这个

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();
}

关于android - 从 ViewPager 中删除 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51531605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com