java - FragmentStatePagerAdapter does not handle changes -
i having problem managing changing list of item using fragmentstatepageradapter. please check source here.
the problem when underlying list changes, , call notifydatasetchanged
, adapter not rebuild it's internal list of fragments. referring code in instantiateitem
:
@override public object instantiateitem(viewgroup container, int position) { // if have item instantiated, there nothing // do. can happen when restoring entire pager // saved state, fragment manager has // taken care of restoring fragments had instantiated. if (mfragments.size() > position) { fragment f = mfragments.get(position); if (f != null) { return f; } }
i believe code in comment wrong! if delete item @ position 0 in list, , call notifydatasetchanged
, fragment @ position 0 should deleted. adapter has never updated position of fragments in own private list. therefore still shows old, deleted, data.
i found this answer hack relies on getitemposition being called , forcing fragment update it's views.
how possible manage fragments in changing list?
per viewpager.datasetchanged() source, uses pageradapter.getitemposition() determine new position of item after data change. default, getitemposition()
returns position_unchanged, meaning never destroys , recreates items has created.
override getitemposition()
return correct value if want update fragments exist in response notifydatasetchanged()
call. example, returning position_none destroy every fragment , recreate them scratch (which, in cases, more work needs done if items reordering).
Comments
Post a Comment