更新框架:
@Override
public int getItemPosition(Object object) {
FragmentPage2 f = (FragmentPage2) object;
if (f != null) {
f.update();
}
return super.getItemPosition(object);
}
現在如果添加一個額外的Fragment
到getItem(int position)
中,還一切正常,但是一運行mPager.getAdapter().notifyDataSetChanged();
,就會報出異常:
08-07 19:45:50.844: E/AndroidRuntime(17776): java.lang.ClassCastException: com.foodbuilder.FragmentPage1 cannot be cast to com.foodbuilder.FragmentPage2
08-07 19:45:50.844: E/AndroidRuntime(17776): at com.foodbuilder.MainActivity$MyAdapter.getItemPosition(MainActivity.java:967)
08-07 19:45:50.844: E/AndroidRuntime(17776): at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:851)
08-07 19:45:50.844: E/AndroidRuntime(17776): at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2772)
08-07 19:45:50.844: E/AndroidRuntime(17776): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
08-07 19:45:50.844: E/AndroidRuntime(17776): at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
08-07 19:45:50.844: E/AndroidRuntime(17776): at com.foodbuilder.MainActivity.onDialogPositiveClick(MainActivity.java:888)
08-07 19:45:50.844: E/AndroidRuntime(17776): at com.foodbuilder.TablesDialogFragment$2.onClick(TablesDialogFragment.java:57)
08-07 19:45:50.844: E/AndroidRuntime(17776): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
`
Fragment1不能強制轉換為Fragment2
你要麼統一Fragament ,將所有Fragment加入一個list然後去get取出來