recyclerview - Data List duplicate after go to other fragment (Sliding Tab) -


there 3 tabs : one || two || tri

example : mylist = 1,2,3

issue :

after go page two mylist = 1,2,3,1,2,3 (double duplicate)

if go page tri mylist = 1,2,3,1,2,3,1,2,3 (triple duplicate)

after looking solution on internet, found code (on 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 in onefragment.java:

   mlistfoodadapter = new listfoodadapter(getcontext(), mfoodmodel);         mlistfoodadapter.swap(mfoodmodel);         mrecyclerviewlistfood.setlayoutmanager(linearlayoutmanager);         mrecyclerviewlistfood.setadapter(mlistfoodadapter);         mrecyclerviewlistfood.setitemanimator(new defaultitemanimator()); 

but gave me nullpointer there no data show in recyclerview

please, suggest me how make list data isn't duplicate anymore after page two of tri

bellow codes onefragment.java , 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);     } } 

there problem in onviewcreated() method. onviewcreated() method called each time after fragment's view created. fragment reused if in memory. reason mfoodmodel list doesn't reinitialized. need reinitialize mfoodmodel create previous data.

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

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -