android - RX Java RecycleView -
i'm not familiar rx java. trying use recycleview. reason code not working here code.
fragment recycle view
public class cheeselistfragment extends fragment { private final compositesubscription subscriptions = new compositesubscription(); private publishsubject<string> timespansubject; private final func1<string, observable<liveinfo>> trendingsearch = new func1<string, observable<liveinfo>>() { @override public observable<liveinfo> call(string s) { radioliveinfoobservableservice radioliveinfoobservableservice=apiprovider.getinstance().getradioobserverinfo(); return radioliveinfoobservableservice.radioinfo(type.interval) .observeon(androidschedulers.mainthread()) .doonerror(trendingerror) .onerrorresumenext(observable.<liveinfo>empty()); } }; private final action1<throwable> trendingerror = new action1<throwable>() { @override public void call(throwable throwable) { timber.e(throwable, "failed trending repositories"); } }; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { timespansubject = publishsubject.create(); final recyclerview rv = (recyclerview) inflater.inflate( r.layout.fragment_cheese_list, container, false); setuprecyclerview(rv); subscriptions.add(timespansubject .flatmap(trendingsearch) .map(searchresulttorepositorylist.instance()) .subscribe(adapter)); return rv; } private simplestringrecyclerviewadapter adapter; private void setuprecyclerview(recyclerview recyclerview) { recyclerview.setlayoutmanager(new linearlayoutmanager(recyclerview.getcontext())); adapter=new simplestringrecyclerviewadapter(getactivity(), new simplestringrecyclerviewadapter.currentshowclicklistener() { @override public void oncurrentshowclick(current currentshow) { intent intent = new intent(capplication.getappcontext(), cheesedetailactivity.class); intent.putextra(cheesedetailactivity.extra_name, currentshow.getname()); capplication.getappcontext().startactivity(intent); } }); recyclerview.setadapter(adapter); adapter.registeradapterdataobserver(new recyclerview.adapterdataobserver() { @override public void onchanged() { toast.maketext(getactivity(),"data changed",toast.length_short).show(); } }); } private list<string> getrandomsublist(string[] array, int amount) { arraylist<string> list = new arraylist<>(amount); random random = new random(); while (list.size() < amount) { list.add(array[random.nextint(array.length)]); } return list; } public static class simplestringrecyclerviewadapter extends recyclerview.adapter<simplestringrecyclerviewadapter.viewholder> implements action1<list<current>> { private list<current> currentshows = collections.emptylist(); public interface currentshowclicklistener { void oncurrentshowclick(current currentshow); } private final currentshowclicklistener currentshowclicklistener; private final typedvalue mtypedvalue = new typedvalue(); private int mbackground; @override public void call(list<current> currentshows) { this.currentshows = currentshows; notifydatasetchanged(); } public simplestringrecyclerviewadapter(context context,currentshowclicklistener currentshowclicklistener) { context.gettheme().resolveattribute(r.attr.selectableitembackground, mtypedvalue, true); mbackground = mtypedvalue.resourceid; this.currentshowclicklistener = currentshowclicklistener; } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { listitemview view = (listitemview)layoutinflater.from(parent.getcontext()) .inflate(r.layout.list_item, parent, false); view.setbackgroundresource(mbackground); return new viewholder(view); } @override public void onbindviewholder(viewholder viewholder, int i) { viewholder.bindto(currentshows.get(i)); } @override public long getitemid(int position) { return position; } @override public int getitemcount() { return currentshows.size(); } public final class viewholder extends recyclerview.viewholder { public final listitemview itemview; private current currentshow; public viewholder(listitemview itemview) { super(itemview); this.itemview = itemview; this.itemview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { currentshowclicklistener.oncurrentshowclick(currentshow); } }); } public void bindto(current currentshow) { this.currentshow = currentshow; itemview.bindto(currentshow); } } } }
searchtoresultrepositorylist
public final class searchresulttorepositorylist implements func1<liveinfo, list<current>> { private static volatile searchresulttorepositorylist instance; public static searchresulttorepositorylist instance() { if (instance == null) { instance = new searchresulttorepositorylist(); } return instance; } @override public list<current> call(liveinfo repositoriesresponse) { list<current> currents=new arraylist<>(); currents.add(repositoriesresponse.getcurrent()); return currents; } }
rest
public interface radioliveinfoobservableservice { @get("/api/live-info/") observable<liveinfo> radioinfo( @query("type") type type); }
it's doing nothing. tried debug trendingsearch.call not called @ all.
i can make work way. still want know how subscription
radioliveinfoobservableservice radioliveinfoobservableservice=apiprovider.getinstance().getradioobserverinfo(); radioliveinfoobservableservice.commits(type.interval) .observeon(androidschedulers.mainthread()) .doonerror(trendingerror) .onerrorresumenext(observable.<liveinfo>empty()).subscribe(new action1<liveinfo>() { @override public void call(liveinfo liveinfo) { list<current> currents=new arraylist<current>(); currents.add(liveinfo.getcurrent()); adapter.currentshows=currents; adapter.notifydatasetchanged(); rv.setadapter(adapter); } });
that's lot of code digest looking errors, off top of head nothing happen until timespansubject.onnext()
called. don't see called anywhere maybe there missing code not showing.
if there no missing code calls timespansubject.onnext()
, use either behaviorsubject
emit item when first subscribed or observable such timer
or interval
depending on trying do. timer
subscribe trendingsearch
observable single time whereas using interval
subscribe multiple times.
Comments
Post a Comment