gpt4 book ai didi

android-recyclerview - 转到其他片段后数据列表重复(滑动选项卡)

转载 作者:行者123 更新时间:2023-12-04 14:42:14 25 4
gpt4 key购买 nike

有 3 个标签: || || 三里

例子 :myList = 1,2,3
问题:

在我转到页面后 myList = 1,2,3,1,2,3 (双份)

如果我去页面三里 myList = 1,2,3,1,2,3,1,2,3 (一式三份)

在互联网上寻找解决方案后,我找到了这个代码(在 Adapter 类上):

   public void swap(List<FoodModel> datas){
datas = new ArrayList<>();//updated
if(mListFood !=null || mListFood.size() !=0){
mListFood.clear();
mListFood.addAll(datas);
}else{
mListFood = datas;
}
notifyDataSetChanged();
}

我在 oneFragment.java 中是这样用的:
   mListFoodAdapter = new ListFoodAdapter(getContext(), mFoodModel);
mListFoodAdapter.swap(mFoodModel);
mRecyclerViewListFood.setLayoutManager(linearLayoutManager);
mRecyclerViewListFood.setAdapter(mListFoodAdapter);
mRecyclerViewListFood.setItemAnimator(new DefaultItemAnimator());

但它给了我 nullPointer有时 RecyclerView中没有数据显示

请建议我如何使列表数据在从页面返回后不再重复 三里

吼我的代码 oneFragment.javaViewPagerAdapter.java
oneFragment.java
public class FoodFragment extends Fragment {

private RecyclerView mRecyclerViewListFood;
private List<FoodModel> mFoodModel = new ArrayList<>();
private ListFoodAdapter mListFoodAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_foods, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FragmentActivity fragmentActivity = getActivity();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(fragmentActivity);
mRecyclerViewListFood = (RecyclerView) view.findViewById(R.id.recyclerView_list_foods);
mFoodModel.add(new FoodModel("1",String.valueOf(R.drawable.icon), "test 1"));
mFoodModel.add(new FoodModel("2",String.valueOf(R.drawable.icon), "test 2"));

mListFoodAdapter = new ListFoodAdapter(getContext(), mFoodModel);
mRecyclerViewListFood.setLayoutManager(linearLayoutManager);
mRecyclerViewListFood.setAdapter(mListFoodAdapter);
mRecyclerViewListFood.setItemAnimator(new DefaultItemAnimator());



}
}

ViewPagerAdapter.java :
public class ViewPagerDetailStandAdapter extends FragmentStatePagerAdapter {

private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerDetailStandAdapter(FragmentManager manager) {
super(manager);
}

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

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

public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}

最佳答案

你有问题onViewCreated()方法。
每次创建片段 View 后都会调用 OnViewCreated() 方法。但是如果片段在内存中,它会被重用。这就是为什么您的 mFoodModel列表不会重新初始化。所以需要重新初始化mFoodModel创建以前的数据。

所以这是你修改后的 OnCreateView()`

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FragmentActivity fragmentActivity = getActivity();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(fragmentActivity);
mRecyclerViewListFood = (RecyclerView) view.findViewById(R.id.recyclerView_list_foods);
mFoodModel = new ArrayList<>(); // initialize you list
mFoodModel.add(new FoodModel("1",String.valueOf(R.drawable.icon), "test 1"));
mFoodModel.add(new FoodModel("2",String.valueOf(R.drawable.icon), "test 2"));

mListFoodAdapter = new ListFoodAdapter(getContext(), mFoodModel);
mRecyclerViewListFood.setLayoutManager(linearLayoutManager);
mRecyclerViewListFood.setAdapter(mListFoodAdapter);
mRecyclerViewListFood.setItemAnimator(new DefaultItemAnimator());
}

关于android-recyclerview - 转到其他片段后数据列表重复(滑动选项卡),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34825785/

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