android - RecyclerView - a single selection selects every 10 row -


it's strange when long click on 1 row using code, star on row turns gold (which want), each star ten rows apart. code:

    public class myviewholder extends recyclerview.viewholder {     public textview localedisplay;     private static final int grey_blue = 0xff546e7a;     private static final int gold = 0xffffa000;      public myviewholder(final view view) {         super(view);         localedisplay = (textview) view.findviewbyid(r.id.languagetext);          view.setonclicklistener(new view.onclicklistener() {                                      @override                                     public void onclick(view view) {                                         languages currentlanguages = languageslist.get(getadapterposition());                                         snackbar snackbar;                                         snackbar = snackbar.make(view, currentlanguages.getlocaledisplay() + " selected", snackbar.length_indefinite)                                                 .setaction("next", new view.onclicklistener() {                                                     @override                                                     public void onclick(view v) {                                                         intent intent = new intent(v.getcontext(), mainactivity.class);                                                         v.getcontext().startactivity(intent);                                                     }                                                 });                                         view snackbarview = snackbar.getview();                                         snackbarview.setbackgroundcolor(grey_blue);                                         snackbar.show();                                     }                                 }          );          view.setonlongclicklistener(new view.onlongclicklistener()                                      {                                         @override                                         public boolean onlongclick(view v) {                                             toast.maketext(v.getcontext(), "position " + getadapterposition(), toast.length_short).show();                                              imageview star = (imageview) view.findviewbyid(r.id.imageview);                                              if (star.getcolorfilter() == null) {                                                 star.setcolorfilter(gold);                                             } else star.clearcolorfilter();                                              return true;                                         }                                     }          );     } 

i have tried following this tutorial same behavior happens. ideas correct this?

seems view being recycled, means listener reference being called recycled views.

an easy way solve setting new references each single view @ onbindviewholder method. viewholder used find views , keep references recycled views, isn't idea set data inside it.

you change viewholder for:

public exampleviewholder(view view) {         super(view);         mycontainer = (linearlayout) view.findviewbyid(r.id.mycontainer);         localedisplay = (textview) view.findviewbyid(r.id.languagetext);         star = (imageview) view.findviewbyid(r.id.imageview);     } 

and set listener @ onbindviewholder:

@override public void onbindviewholder(recyclerview.viewholder viewholder, int position) {      exampleholder holder = (exampleholder) viewholder;      //insert position reference     holder.mycontainer.settag(position);      // setting listeners     holder.mycontainer.setonclicklistener(this);     holder.mycontainer.setonlongclicklistener(this);   } 

then implement onlongclicklistener , onclicklistener @ adapter.

@override public boolean onlongclick(view v) {      //getting position     int position = (int)v.gettag();      //insert code     return false; }  @override public void onclick(view v) {     //code here } 

drop me line if works. luck.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -