Duplicate data list after moving to another fragment (sliding tab)

There are 3 tabs: ONE || TWO strong> || TRI

example: myList = 1,2,3

problem:

after going to the TWO page strong> myList = 1,2,3,1,2,3 (double duplicate)

if I go to the TRI page myList = 1,2,3,1,2,3,1,2,3 (triple duplicate)

after I searched the solution on the Internet, I found this code (in the adapter class):

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

I used this in oneFragment.java :

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

but it gave me nullPointer , sometimes no data in RecyclerView

please suggest me how to make the list data no longer duplicate after from the TWO strong> page from TRI

enter my codes oneFragment.java and ViewPagerAdapter.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); } } 
+6
source share
2 answers

You have onViewCreated() problem. The OnViewCreated () method is called every time after creating a fragment view. But a fragment is reused if it is in memory. That is why your mFoodModel list mFoodModel not receive reinitialization. Therefore, you need to reinitialize mFoodModel to create previous data.

So this is your modified 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()); } 
+3
source

just use this method to clear fragment data.

 @Override public void onDestroyView() { super.onDestroyView(); tutPojo.clear(); } 
+1
source

All Articles