android - RecyclerView scroll to top after back navigation if adapter's data set through RxAndroid -
when set adapter's data directly scroll position of list correct after come detial fragment, if i'm using rxandroid handle data scroll jump top. here example problem:
public class listfragment extends fragment { private myadapter adapter; private list<string> data = new arraylist<>(); @override public void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); (int = 1; < 30; i++) { data.add("pos: " + i); } } @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view root = inflater.inflate(r.layout.fragment_list, container, false); adapter = new myadapter(); ((recyclerview) root.findviewbyid(r.id.bone_list)).setadapter(adapter); return root; } @override public void onresume() { super.onresume(); data.add("pos: " + (data.size() + 1)); // working //adapter.setdata(data); // not working observable.just(data) .subscribeon(schedulers.io()) .observeon(androidschedulers.mainthread()) .subscribe(new consumer<list<string>>() { @override public void accept(list<string> strings) throws exception { adapter.setdata(strings); } }); } class myadapter extends recyclerview.adapter<myadapter.viewholder> { private list<string> values = new arraylist<>(); public void setdata(list<string> items) { values = items; notifydatasetchanged(); } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { return new viewholder(layoutinflater.from(parent.getcontext()).inflate(r.layout.item_list_content, parent, false)); } @override public void onbindviewholder(final viewholder holder, int position) { holder.content.settext(values.get(position)); holder.root.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { getfragmentmanager().begintransaction() .replace(r.id.container, fragment.instantiate(getcontext(), detialfragment.class.getname())) .addtobackstack(null) .commit(); } }); } @override public int getitemcount() { return values.size(); } class viewholder extends recyclerview.viewholder { final view root; final textview content; viewholder(view view) { super(view); root = view; content = (textview) view.findviewbyid(r.id.content); } } }
}
working (simple set data):
not working (rx):
thanks, robert
Comments
Post a Comment