android - RecyclerView Adapter Lint Error do not treat position as fixed -
i have surf type of error didn't can in case.
i getting following lint error.
do not treat position fixed; use , call holder.getadapterposition() later.
recyclerview not call onbindviewholder again when position of item changes in data set unless item invalidated or new position cannot determined. reason, should use position parameter while acquiring related data item inside method, , should not keep copy of it.
if need position of item later on (e.g. in click listener), use getadapterposition() have updated adapter position.
my recyclerview adapter:
/** * right content adapter */ public class rightcontentadapter extends recyclerview.adapter<rightcontentadapter.viewholder> { public final arraylist<idnamepair> mvalues; public activity context; private int pos; public rightcontentadapter(activity context, arraylist<idnamepair> items) { mvalues = items; this.context = context; } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()) .inflate(r.layout.activity_filter_checkbox, parent, false); log.loginfo("oncreateviewholder call"); return new viewholder(view); } @override public void onbindviewholder(final viewholder holder, final int position) { final idnamepair idnamepair = mvalues.get(position); holder.onbind = true; log.logerror("onbind"); if (idnamepair.getname() != null) { holder.mitemcheckbox.settext(idnamepair.getname()); holder.mitemcheckbox.setchecked(idnamepair.ischecked()); if (idnamepair.ischecked()) countfilter++; } holder.onbind = false; holder.mitemcheckbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { log.loginfo("ischecked : " + ischecked); boolean check = false; if (ischecked) countfilter++; else { countfilter--; } if (filterattributearraylist != null) { int filterattribsize = filterattributearraylist.size(); (int = 0; < filterattribsize; i++) { filterattribute fa = filterattributearraylist.get(i); if (selectedattributename.equalsignorecase(fa.getattrname())) { check = true; filterattributearraylist.get(i).getattrval().get(position).setischecked(ischecked); filterattributearraylist.get(i).setfiltercount(countfilter); mvalues.get(position).setischecked(ischecked); //countfilter = 0; // if(!holder.onbind) // notifyitemchanged(position); //mrightcontentadapter.notifyitemchanged(position); break; } } } if (!check) { if (staticfilterarraylist != null) { int staticfiltersize = staticfilterarraylist.size(); (int = 0; < staticfiltersize; i++) { staticfilterdata sf = staticfilterarraylist.get(i); if (selectedattributename.equalsignorecase(sf.getdisplayname())) { staticfilterarraylist.get(i).getvalue().get(position).setischecked(ischecked); staticfilterarraylist.get(i).setfiltercount(countfilter); mvalues.get(position).setischecked(ischecked); // countfilter = 0; // if(!holder.onbind) // notifyitemchanged(position); //mrightcontentadapter.notifyitemchanged(position); break; } } } } } }); } @override public int getitemcount() { return mvalues.size(); } //view holder public class viewholder extends recyclerview.viewholder { // private final view mview; private checkbox mitemcheckbox; viewholder(view view) { super(view); // mview = view; mitemcheckbox = (checkbox) view.findviewbyid(r.id.filter_checkbox); mitemcheckbox.settypeface(font.getmyriadproregular(context)); } } }
i have used make filter type activity flipkart. checkbox getting unchecked automatically while scrolling. there wrong in code?
thank you.
do not make position
final in onbindviewholder
method instead of make holder
final , make use of holder.getadapterposition()
wherever using position
.
don't this: int var = holder.getadapterposition()
, straight away use holder.getadapterposition()
. first problem solved. :)
next when scrolling of checkboxes gets unchecked because recyclerview reuses itemview instead of creating new that's why better listview. need handle properly. if can tell trying in oncheckchangedlistener
can surely out in handling this.
Comments
Post a Comment