java - Countdown timer in RecyclerView showing incorrect values -
i have developed app in want fetch target time values firebase , use these values in recycler adapter start countdown timer each row in recyclerview. earlier whenever values in row changed in realtime firebase time values started flickering , whenever scrolled recycler view countdown timer got messed , started showing erroneous values. found answer in stackoverflow, suggested start countdown timer in oncreateviewholder
method instead of onbindview
holder , helped in removing erroneous values , scrolling problem.
problem
but after new problem arised. values first 7 positions(0 6th position) shown correctly after 0th , 1st position values shown @ 7th , 8th positions , @ 9th position actual value should have been @ position 7 shown. don't know why happening. recycler view contain native ads starting @ position 2 , after every 3 items. oncreateviewholder
, onbindviewholder
code
public mainactivityadapter(context context, list<object> recyclerviewitems) { mcontext = context; mrecyclerviewitems = recyclerviewitems; positionforcounter = 0; } /** * creates new view menu item view or native express ad view * based on viewtype. method invoked layout manager. */ @override public recyclerview.viewholder oncreateviewholder(viewgroup viewgroup, int viewtype) { switch (viewtype) { case native_express_ad_view_type : view nativeexpresslayoutview = layoutinflater.from( viewgroup.getcontext()).inflate(r.layout.native_express_ad_container, viewgroup, false); log.d("counterads",string.valueof(positionforcounter)); positionforcounter++; return new nativeexpressadviewholder(nativeexpresslayoutview); case match_item_view_type : // fall through default:view matchitemlayoutview = layoutinflater.from(viewgroup.getcontext()).inflate( r.layout.match_item_container, viewgroup, false); //experimenting countdown timer matcheslist matchtimer = (matcheslist) mrecyclerviewitems.get(positionforcounter); string targettime = matchtimer.gettime(); log.d("counterpositions",string.valueof(positionforcounter)); log.d("counter", targettime); long milliseconds = 0; long diff; simpledateformat formatter = new simpledateformat("dd mmm yy hh:mm aa"); formatter.setlenient(false); date enddate; try { enddate = formatter.parse(targettime); milliseconds = enddate.gettime(); } catch (parseexception e) { e.printstacktrace(); } diff = milliseconds - system.currenttimemillis(); final matchitemviewholder matchitemholder = new matchitemviewholder(matchitemlayoutview); matchcounter =new countdowntimer(diff, 1000) { @override public void ontick(long millisuntilfinished) { long diffseconds = millisuntilfinished/1000; string hoursleft = string.format("%d", diffseconds / 3600); matchitemholder.mtimehr.settext(hoursleft + "h"); string minutesleft = string.format("%d", ((diffseconds % 86400) % 3600) / 60); matchitemholder.mtimeminutes.settext(minutesleft + "m"); string secondsleft = string.format("%d", ((diffseconds % 86400) % 3600) % 60); matchitemholder.mtimeseconds.settext(secondsleft + "s"); } @override public void onfinish() { matchitemholder.mtimehr.settext(""); matchitemholder.mtimeminutes.settext("match started"); matchitemholder.mtimeseconds.settext(""); } }; //matchcounter.cancel(); matchcounter.start(); positionforcounter++; return new matchitemviewholder(matchitemlayoutview); } } /** * replaces content in views make menu item view , * native express ad view. method invoked layout manager. */ @override public void onbindviewholder(recyclerview.viewholder holder, int position) { int viewtype = getitemviewtype(position); switch (viewtype) { case native_express_ad_view_type: nativeexpressadviewholder nativeexpressholder = (nativeexpressadviewholder) holder; nativeexpressadview adview = (nativeexpressadview) mrecyclerviewitems.get(position); viewgroup adcardview = (viewgroup) nativeexpressholder.itemview; // nativeexpressadviewholder recycled recyclerview may different // instance 1 used position. clear // nativeexpressadviewholder of subviews in case has different // adview associated it, , make sure adview position doesn't // have parent of different recycled nativeexpressadviewholder. adcardview.removeallviews(); if (adview.getparent() != null) { ((viewgroup) adview.getparent()).removeview(adview); } // add native express ad native express ad view. adcardview.addview(adview); break; case match_item_view_type: default: final matchitemviewholder matchitemholder = (matchitemviewholder) holder; matcheslist match = (matcheslist) mrecyclerviewitems.get(position); // used pass key creating problems matchitemholder.itemview.settag(position); matchitemholder.setkeys(match); matchitemholder.mseriesname.settext(match.getseries()); matchitemholder.mteam1name.settext(match.getteam1()); matchitemholder.mteam2name.settext(match.getteam2()); picasso.with(matchitemholder.viewholdercontext) .load(match.getimaget1()) //.networkpolicy(networkpolicy.offline) //.placeholder(r.drawable.noimage) .centercrop() .resize(256,256) .into(matchitemholder.mteam1image); picasso.with(matchitemholder.viewholdercontext) .load(match.getimaget2()) //.networkpolicy(networkpolicy.offline) //.placeholder(r.drawable.noimage) .centercrop() .resize(256,256) .into(matchitemholder.mteam2image); break; } }
Comments
Post a Comment