android - Using RxJava inside RecyclerView Adapter -


so few weeks ago asked question: recyclerview periodic ui child updates.

and today want refactor funcionality using rxjava. it's pretty simple, accomplish following way:

 @override     public void onbindviewholder(recyclerview.viewholder holder, int position) {                  if (friend.getgamestatus().equals(gamestatus.ingame)) {                     holderonline.startrepeatingtask();                 } else {                     holderonline.stoprepeatingtask();                 }      }   class myviewholderonline extends recyclerview.viewholder {      private subscription subscribe;          public myviewholderonline(view itemview) {             super(itemview);             butterknife.bind(this, itemview);          }          public void startrepeatingtask() {             subscribe = observable.interval(updateinterval, timeunit.milliseconds)                     .map(along -> current.getgamestatustoprint())                     .subscribeon(schedulers.io())                     .observeon(androidschedulers.mainthread())                     .subscribe(new subscriber<string>() {                         @override public void oncompleted() { }                         @override public void onerror(throwable e) { }                          @override                         public void onnext(string gamestatustoprint) {                             gamestatus.settext(gamestatustoprint);                         }                     });         }          void stoprepeatingtask() {             if(subscribe != null && !subscribe.isunsubscribed())                 subscribe.unsubscribe();         }     } 

the problem different. have navigationdrawer implemented activities paused , not destroyed when switched. so, after switch activity don't contains adapter, observable keeps on sending stuff because periodic interval observable. question is, should do? need unsubscribe when activity paused, have no idea how since to, , how subscribe back. or ideas?

so, if understand correctly, 1 way solve problem answer question: in activity contains recyclerview, how references viewholders displayed/bound?

for example, stop updates following in onpause() of activity:

// assuming using linearlayoutmanager: final int firstvisibleindex = mlinearlayoutmanager.findfirstvisibleitemposition(); final int lastvisibleindex = mlinearlayoutmanager.findlastvisibleitemposition();  (int = firstvisibleindex; <= lastvisibleindex; i++) {     yourviewholder viewholder = (yourviewholder) findviewholderforadapterposition (i);     viewholder.stoprepeatingtask(); } 

and likewise, restart tasks in onresume of activity.

but: wrote not sure whether there may viewholders beyond visible area of recyclerview (i. e. before first or after last visible item) bound not reached method. if turns out case can still iterate on indices of items in adapter , discard null return values.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -