android - Optimize way to use ViewPager? -
i'm writing application need show 100 page in viewpager. there facility jump 1 index direct random index page. here every page self contain list of item.
now if user viewpager.current(index) jump 1 random index page..then ui stuck moment , display.
so avoid thought implement 7 adapter page view..where these 100(getcount()) page reuses these 7 page.
but im'm getting
e/androidruntime(25961): java.lang.illegalstateexception: fragment added: arraylistfragment{24dad9b6 #0 id=0x7f060009} e/androidruntime(25961): @ android.support.v4.app.fragmentmanagerimpl.addfragment(fragmentmanager.java:1192) e/androidruntime(25961): @ android.support.v4.app.backstackrecord.run(backstackrecord.java:616) e/androidruntime(25961): @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1484) e/androidruntime(25961): @ android.support.v4.app.fragmentmanagerimpl.executependingtransactions(fragmentmanager.java.
i'm using fragmentstatepageradapter adapter implementation.
following snap of code i'm using
static final int num_items = 100; private int max_available_count = 7; private arraylist<arraylistfragment> listfragment = new arraylist(); public class myadapter extends fragmentstatepageradapter { public myadapter(fragmentmanager fragmentmanager) { super(fragmentmanager); (int = 0; < max_available_count; i++) { listfragment.add(arraylistfragment.newinstance(i)); } } @override public int getcount() { return num_items; } @override public fragment getitem(int position) { original = position; if (position >= max_available_count - 1) { int newposition = position % max_available_count; position = newposition; } fragment fragment = listfragment.get(position); return fragment; } }
any suggestion i'm doing wrong here. or other suggestion achieve it!!
your error because manipulating position in getitem() method. can't because @ point return fragment tied other page , exception above. , storing references fragments bad practice, return new instance of fragment object.
Comments
Post a Comment