android - FragmentStatePagerAdapter with dynamic data, IllegalStateException -
i have main fragment has list of items, on clicking item, open detailed view of item in new fragment
using
transaction.replace(r.id.container, feeddetail.newinstance(title, position)).commit();
the detailed fragment has fragmentstatepageradapter show sliding list of items. works right, main list of item gets updated in background (asynctask). when sliding items in detailed view,
java.lang.illegalstateexception: application's pageradapter changed adapter's contents without calling pageradapter#notifydatasetchanged! expected adapter item count: 30, found: 40 pager id
i call adapter.notifydatasetchanged() onpostexecute(), still not fix issue. guess issue similar 1 shared here http://translate.google.co.in/translate?hl=en&sl=zh-cn&u=http://blog.csdn.net/hlglinglong/article/details/34845519&prev=search not sure though. have tried different values in getitemposition() of adapter without success. nice hint on issue. pasting detailed fragment code here reference,
public class feeddetail extends fragment { private viewpager mpager = null; private pageradapter mpageradapter; private onfragmentinteractionlistener mlistener; private fragmentactivity mycontext; public static string title; public static integer pos; private layoutinflater minflater; private view mrootview = null; viewgroup mviewgroup = null; public static feeddetail newinstance(string title, int pos) { feeddetail fragment = new feeddetail(); bundle args = new bundle(); args.putstring("title", title); args.putint("pos", pos); fragment.setarguments(args); return fragment; } public feeddetail() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null) { title = getarguments().getstring("title"); pos = getarguments().getint("pos"); } } @override public void ondestroyview() { super.ondestroyview(); viewgroup parentviewgroup = (viewgroup) mrootview.getparent(); if( null != parentviewgroup ) { parentviewgroup.removeallviews(); } mviewgroup.removeallviews(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment mviewgroup = container; if (mrootview == null) { mrootview = (view) inflater.inflate(r.layout.feedpager, null, false); } else { viewgroup parent = (viewgroup) mrootview.getparent(); parent.removeview(mrootview); } mpager = (viewpager) mrootview.findviewbyid(r.id.pager); mpageradapter = new screenslidepageradapter(getchildfragmentmanager()); mpager.setadapter(mpageradapter); mpager.setcurrentitem(pos); mrootview.setfocusableintouchmode(true); mrootview.requestfocus(); return mrootview; } @override public void onattach(activity activity) { mycontext=(fragmentactivity) activity; super.onattach(activity); } @override public void ondetach() { super.ondetach(); try { field childfragmentmanager = fragment.class.getdeclaredfield("mchildfragmentmanager"); childfragmentmanager.setaccessible(true); childfragmentmanager.set(this, null); } catch (nosuchfieldexception e) { throw new runtimeexception(e); } catch (illegalaccessexception e) { throw new runtimeexception(e); } } public static interface onfragmentinteractionlistener { public void onfragmentinteraction(uri uri); } private class screenslidepageradapter extends fragmentstatepageradapter{ public screenslidepageradapter(fragmentmanager fm) { super(fm); setretaininstance(true); } @override public fragment getitem(int position) { return screenslidepagefragment.newinstance(position); } @override public int getitemposition(object object) { return 1; } @override public int getcount() { return navigation.adapter.getcount(); } @override public charsequence getpagetitle(int position) { return navigation.adapter.getfeeds().get(position).title; } } }
Comments
Post a Comment