android - FragmentPagerAdapter showing fragments wrongly in ViewPager -
i have actionbaractivity
inside have viewpager
. expect see 4 (first, second, third, four) fragments in 4 (first, second, third, fourth) pages.
problem:
- i see second fragment on first page.
- i move second page. saw nothing.
- i move first page. saw second fragment.
in oncreate()
of actionbaractvity
, have:
mviewpager = (viewpager) findviewbyid(r.id.pager); mpageradapter = new mypageradapter(getsupportfragmentmanager(), mgoodarraylistofwords); mviewpager.setadapter(mpageradapter);
this how mypageradapter
looks:
public class mypageradapter extends fragmentpageradapter { private arraylist<string> mwords; public mypageradapter(fragmentmanager fm, arraylist<string> words) { super(fm); mwords = words; } @override public fragment getitem(int position) { string specificword = mwords.get(position); return myfragment.newinstance(specificword); } @override public int getcount() { return mwords.size(); } @override public charsequence getpagetitle(int position) { return mwords.get(position); } }
i instantiated myfragment
using static factory method , made no-arg constructor private:
public static myfragment newinstance(string word) { myfragment fragment = new myfragment(); bundle args = new bundle(); args.putstring("word", word); fragment.setarguments(args); return fragment; } private myfragment() { super(); }
moreover, in onviewoncreated()
, handle fragment's list view, use loadermanager
init()
loader. update list view's adapter in onloadfinished()
of loadermanager.loadercallbacks<cursor>
.
i think problem due loader
s finish jobs after fragments' views have been created. log when expect 2 pages , 2 fragments:
04-01 23:15:55.707 d/debugtag﹕ getitem called 04-01 23:15:55.717 d/debugtag﹕ getitem called 04-01 23:15:55.717 d/debugtag﹕ on view created called 04-01 23:15:55.797 d/debugtag﹕ on view created called 04-01 23:15:56.157 d/debugtag﹕ on load finished called 04-01 23:15:56.177 d/debugtag﹕ on load finished called
extend fragmentstatepageradapter
you're using getitem()
method. if want keep fragmentpageradapter
override instantiateitem()
.
Comments
Post a Comment