java - Android RecyclerView, recycling not working properly -


i have recyclerview using. have used recyclerview before never had problem.

when scroll , down of items disappear, , of items disappear appear again in bottom.

code:

viewholder:

public class viewholder extends recyclerview.viewholder {     public textview txt;      public viewholder(view view) {         super(view);         txt = (textview) view.findviewbyid(r.id.txt);     } } 

adapter:

public class myadapter extends recyclerview.adapter<viewholder> {     private final activity activity;     private final arraylist<hashmap<string, string>> mitems;      public myadapter (activity activity, arraylist<hashmap<string, string>> mitems) {         this.activity = activity;         this.mitems= mitems;     }      @override     public viewholder oncreateviewholder(viewgroup viewgroup, int i) {         return new viewholder(layoutinflater.from(activity).inflate(r.layout.items, viewgroup, false));     }      @override     public void onbindviewholder(viewholder viewholder, int position) {         hashmap<string, string> item = mitems.get(position);          string info = item.get("info ");         if (info!= null) {             viewholder.txt.settext(info);         } else {             viewholder.txt.setvisibility(view.gone);         }     }      @override     public int getitemcount() {         return (null != mitems? mitems.size() : 0);     } } 

onbindviewholder reuses views let's first time onbindviewholder() called, info null. cause row have visibility of view.gone.

when onbindviewholder called again bind new row, view row still view.gone - nothing reset between rows being bound.

therefore if statement should reset state completely:

if (info!= null) {     viewholder.txt.settext(info);     viewholder.txt.setvisibility(view.visible); } else {     viewholder.txt.setvisibility(view.gone); } 

this ensure each row's visibility set correctly.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -