android - ViewPager showing preloaded fragment instead of current fragment -
i using tablayout(android.support.design.widget.tablayout) viepager instantiating 4 fragments fragmentstatepageradapter.
when scroll through fragments loaded perfectly.but when select tab randomly pre-loaded fragment(i know view pager instantiate neighbor fragments along current fragment) shown instead of current fragment.
[in case when click 3rd tab fragment in 4th position being shown,same fragment shown in 4th position too].
how can correct situation. in advance.
adapter
public class orderstatusadapter extends fragmentstatepageradapter { private final list<fragment> mfragments = new arraylist<>(); private final list<string> mfragmenttitles = new arraylist<>(); public orderstatusadapter(fragmentmanager fm) { super(fm); } public void addfragment(fragment fragment, string title) { mfragments.add(fragment); mfragmenttitles.add(title); } @override public fragment getitem(int position) { return mfragments.get(position); } @override public int getcount() { return mfragments.size(); } @override public charsequence getpagetitle(int position) { return mfragmenttitles.get(position); } }
viewpager
viewpager viewpager = (viewpager) findviewbyid(r.id.viewpager); if (viewpager != null) { setupviewpager(viewpager); if (viewpager != null) { viewpager.setadapter(adapter); tablayout = (tablayout) findviewbyid(r.id.orderstatustabs); tablayout.settabmode(tablayout.mode_scrollable); tablayout.setupwithviewpager(viewpager); tablayout.setontabselectedlistener(new tablayout.ontabselectedlistener() { @override public void ontabselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tablayout.tab tab) { } @override public void ontabreselected(tablayout.tab tab) { } }); } } private void setupviewpager(viewpager viewpager) { orderstatusadapter adapter = new orderstatusadapter(getsupportfragmentmanager()); adapter.addfragment(failedfragment.newinstance(list1, title), "category 1"); adapter.addfragment(failedfragment.newinstance(list2, title), "category 2"); adapter.addfragment(failedfragment.newinstance(list3, title), "category 3"); adapter.addfragment(failedfragment.newinstance(list4, title), "category 4"); }
it because of recycleview custom adapter instance not being destroyed , previous data being loaded.so shifted adapter inside fragment , trick.
Comments
Post a Comment