android - RecyclerView Viewholder reusing -


i creating recyclerview expand when user touches , closes when user touches again. below code:

public class recyclerviewadapter extends recyclerview.adapter<recyclerviewadapter.recviewholder> {     arraylist<string> values;     arraylist<integer> expandedposition;     public static class recviewholder extends recyclerview.viewholder {         ...         private boolean resultsopened = false;         ...         relativelayout favheaderlayout;         relativelayout favresultslayout;          public recviewholder (view itemview) {             super(itemview);             favheaderlayout.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     if (!resultsopened) {                         expandlayout();                     } else {                         favresultslayout.setvisibility(view.gone);                         resultsopened = false;                     }                 }             });          }          public void expandlayout() {             favresultslayout.setvisibility(view.visible);             resultsopened = true;             //i populate expanded layout here         }     }      public recyclerviewadapter (context context, arraylist<string> values) {         this.context = context;         this.values= values;     }      @override     public recviewholder oncreateviewholder(viewgroup parent, int viewtype) {         view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.layout_file, parent, false);         recviewholder recviewholder= new recviewholder(v);         return recviewholder;     }      @override     public void onbindviewholder(recviewholder recviewholder , int position) {     }      @override     public void onattachedtorecyclerview(recyclerview recyclerview) {         super.onattachedtorecyclerview(recyclerview);     }      @override     public int getitemcount() {         return values.size();     }  } 

when touch first view, should expand itself. however, when adapter has more data , touch first view, view expands , view @ bottom of recyclerview (e.g. 8th view) expands too. how ensure view have been touched expand the other stays close?

edit

i've tried storing expanded views position in arraylist , in onbindviewholder, check views expanded , expand them only:

public class recyclerviewadapter extends recyclerview.adapter<recyclerviewadapter.recviewholder> {     arraylist<string> values;     arraylist<integer> expandedposition;     public static class recviewholder extends recyclerview.viewholder {         ...         private boolean resultsopened = false;         ...         relativelayout favheaderlayout;         relativelayout favresultslayout;          public recviewholder (view itemview) {             super(itemview);           }      }      public recyclerviewadapter (context context, arraylist<string> values) {         this.context = context;         this.values= values;     }      @override     public recviewholder oncreateviewholder(viewgroup parent, int viewtype) {         view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.layout_file, parent, false);         recviewholder recviewholder= new recviewholder(v);         return recviewholder;     }      @override     public void onbindviewholder(recviewholder recviewholder , int position) {         recviewholder.favresultslayout.setvisibility(view.gone);         for(int i= 0; < expandedposition.size(); ++){             recviewholder.favresultslayout.setvisibility(view.visible);         }         recviewholder.favheaderlayout.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                      if (!expandedposition.contains(position)) {                         expandlayout(recviewholder);                         expandedposition.add(position);                     } else {                         recviewholder.favresultslayout.setvisibility(view.gone);                         for(int = 0; < expandedposition.size(); ++){                             if (expandedposition.get(i) == position){                                     expandedposition.remove(i);                             }                         }                     }                 }             });     }     public void expandlayout() {             favresultslayout.setvisibility(view.visible);             //i populate expanded layout here         }     @override     public void onattachedtorecyclerview(recyclerview recyclerview) {         super.onattachedtorecyclerview(recyclerview);     }      @override     public int getitemcount() {         return values.size();     }  } 

still no luck :(

as class name says recycling ...

public class recyclerviewadapter extends recyclerview.adapter<recyclerviewadapter.recviewholder> {     arraylist<string> values;     sparsebooleanarray expanded = new sparsebooleanarray();     public static class recviewholder extends recyclerview.viewholder {         //edit!!!!!         public recviewholder (view itemview, final recyclerviewadapter adapter) {         //rest of code ...         favheaderlayout.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                  final int currentposition = getadapterposition();                 //is view @ current position expanded?                 if(adapter.expanded.get(currentposition ))                     //if shouldn't                     adapter.expanded.delete(getadapterposition(currentposition));                 else                     //if not, expand                     adapter.expanded.put(currentposition, true);                 //wheeee inform adapter rebind                 adapter.notifyitemchanged(currentposition);                 //setupexpanded(expanded.get(currentposition));              }         });         }         protected void setupexpanded(boolean state) {         //only hide show layouts based on state         }     }     @override     public recviewholder oncreateviewholder(viewgroup parent, int viewtype) {         view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.layout_file, parent, false);         recviewholder recviewholder= new recviewholder(v, this);         return recviewholder;     }      @override     public void onbindviewholder(recviewholder recviewholder , int position)    {         //this line should self explanatory         recviewholder.setupexpanded(expanded.get(position));     }      //rest of code } 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -