调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment之我的解决方法
private class ViewPagerAdapter extends FragmentPagerAdapter {
FragmentManager mFragmentManager;
FragmentTransaction mCurTransaction;
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
this.mFragmentManager = fragmentManager;
}
@Override
public Fragment getItem(int position) {
final String mimeType = mSortedActionMimeTypes.get(position);
Log.d("lyl", "getItem-->mimeType: " + mimeType + " position: " + position);
QuickContactListFragment fragment = new QuickContactListFragment(mimeType);
return fragment;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
removeFragment(container,position);
QuickContactListFragment fragment =
(QuickContactListFragment) super.instantiateItem(container,position);
final String mimeType = mSortedActionMimeTypes.get(position);
final List<Action> actions = mActions.get(mimeType);
Log.d("lyl", "instantiateItem-->mimeType: " + mimeType + " position: " + position
+ " container.id: " + container.getId());
Log.d("lyl", "actions: " + actions);
fragment.setActions(actions);
return fragment;
}
private void removeFragment(ViewGroup container, int position){
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
String name = getFragmentName(container.getId(), position);
Fragment fragment = mFragmentManager.findFragmentByTag(name);
mCurTransaction.remove(fragment);
mCurTransaction.commit();
mCurTransaction = null;
mFragmentManager.executePendingTransactions();
}
private String getFragmentName(int viewId, int index) {
return "android:switcher:" + viewId + ":" + index;
}
@Override
public int getCount() {
return mSortedActionMimeTypes.size();
}
@Override
public int getItemPosition(Object object) {
final QuickContactListFragment fragment = (QuickContactListFragment) object;
final String mimeType = fragment.getMimeType();
for (int i = 0; i < mSortedActionMimeTypes.size(); i++) {
if (mimeType.equals(mSortedActionMimeTypes.get(i))) {
return i;
}
}
return PagerAdapter.POSITION_NONE;
}
FragmentManager mFragmentManager;
FragmentTransaction mCurTransaction;
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
this.mFragmentManager = fragmentManager;
}
@Override
public Fragment getItem(int position) {
final String mimeType = mSortedActionMimeTypes.get(position);
Log.d("lyl", "getItem-->mimeType: " + mimeType + " position: " + position);
QuickContactListFragment fragment = new QuickContactListFragment(mimeType);
return fragment;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
removeFragment(container,position);
QuickContactListFragment fragment =
(QuickContactListFragment) super.instantiateItem(container,position);
final String mimeType = mSortedActionMimeTypes.get(position);
final List<Action> actions = mActions.get(mimeType);
Log.d("lyl", "instantiateItem-->mimeType: " + mimeType + " position: " + position
+ " container.id: " + container.getId());
Log.d("lyl", "actions: " + actions);
fragment.setActions(actions);
return fragment;
}
private void removeFragment(ViewGroup container, int position){
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
String name = getFragmentName(container.getId(), position);
Fragment fragment = mFragmentManager.findFragmentByTag(name);
mCurTransaction.remove(fragment);
mCurTransaction.commit();
mCurTransaction = null;
mFragmentManager.executePendingTransactions();
}
private String getFragmentName(int viewId, int index) {
return "android:switcher:" + viewId + ":" + index;
}
@Override
public int getCount() {
return mSortedActionMimeTypes.size();
}
@Override
public int getItemPosition(Object object) {
final QuickContactListFragment fragment = (QuickContactListFragment) object;
final String mimeType = fragment.getMimeType();
for (int i = 0; i < mSortedActionMimeTypes.size(); i++) {
if (mimeType.equals(mSortedActionMimeTypes.get(i))) {
return i;
}
}
return PagerAdapter.POSITION_NONE;
}
}
贴出源代码FragmentPagerAdapter中关于instantiateItem方法的机制
@Override
public ObjectinstantiateItem(View container, int position) {
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
// Do we already have this fragment?
String name = makeFragmentName(container.getId(), position);
Fragment fragment = mFragmentManager.findFragmentByTag(name);
if (fragment != null) {
if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment){
mCurTransaction.attach(fragment);
} else {
fragment = getItem(position);
if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
mCurTransaction.add(container.getId(), fragment,
makeFragmentName(container.getId(), position));
}
if (fragment != mCurrentPrimaryItem) {
fragment.setMenuVisibility(false);
}
return fragment;
}
在继承fragmentpagerAdapter的类中。重载instantiateItem后,首先清除掉缓存内的fragment,这样保证了调用父类的instantiateItem后,会每次从geiItem()里获得最新的fragment.就攻克了使用的是缓存内的fragment,而数据却不是最新的问题。