我有一个 ViewPager,我正在使用 FragmentStatePagerAdapter 填充片段(表示来自 arrayListOfObjects 的对象)。一切正常:
mMyFragmentPagerAdapter = new fragmentAdapter(getSupportFragmentManager(),orientation
,rePopulatedfireInfoList);
mPager = (ViewPager)findViewById(R.id.fireInfoFragment_container);
initButton();
setTab();
mPager.setAdapter(mMyFragmentPagerAdapter);
片段适配器扩展了 FragmentStatePagerAdapter。
从主要活动中,我启动了一个对话主题活动;用户可以添加一个新的收藏位置,创建一个新对象,该对象改变主要活动传递的对象的数组列表。这是启动对话活动的代码;一切正常:
Intent locationIntent = new Intent(afisController.this, locationActivity.class);
locationIntent.putExtra("firesList", new fireInfoListWrapper(arrayListOfObjects));
startActivityForResult(locationIntent,1);
浮动活动将对象添加到 arrayListOfObjects。
在主要活动的 onActivityResult 上,我将收到的 arraListOfObjects 与我发送的进行比较;如果不同,我想完全删除 viewPager 的内容并使用新的 arrayListOfObjects 重新创建它。这是 onActivityResults:
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
Toast.makeText(this, "Activity Results fired..." , 1500 ).show();
if ((resultCode == 0) && (data != null)) {
Log.i("onActvityResult", "Inside resultCode check");
Bundle b = data.getExtras();
if(b != null){
Log.i("onActvityResult", "not null");
returnedFireInfoList = (ArrayList<fireInfo>) data.getSerializableExtra("firesListResult");
Log.i("onActvityResult", "results Size: "+returnedFireInfoList.size());
if(returnedFireInfoList.size()>0){
Log.i("onActvityResult", "locationName: "+returnedFireInfoList.get(0).getLocationName());
//compare returnedFireInfoList and rePopulatedfireInfoList, if different;
//add difference to rePopulatedfireInfoList and write back to file.
updateFireInfos(returnedFireInfoList, rePopulatedfireInfoList);
if(returnedFireInfoList.size()!=rePopulatedfireInfoList.size()){
mMyFragmentPagerAdapter1 = new fragmentAdapter(getSupportFragmentManager(),orientation
,returnedFireInfoList);
mPager = (ViewPager)findViewById(R.id.fireInfoFragment_container);
Log.i("updateFireInfos", "fragmentsCount is"+mPager.getCurrentItem());
fireInfoFragment fragment =
(fireInfoFragment) getSupportFragmentManager().findFragmentById(R.id.fireInfoFragment_container);
//This is where the problem is, I don't want to remember what was already on the viewPager //called mPager before.
// mPager.removeAllViews();
//mPager.setAdapter(null);
mMyFragmentPagerAdapter1.notifyDataSetChanged();
mPager.setAdapter(mMyFragmentPagerAdapter1); mMyFragmentPagerAdapter1.notifyDataSetChanged();
}
}
}
这是 fragmentStateAdapter 代码:
public class fragmentAdapter extends FragmentStatePagerAdapter {
private FragmentManager fragmentManager;
private FragmentTransaction mCurTransaction = null;
private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>();
private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
private Fragment mCurrentPrimaryItem = null;
public void restoreState(Parcelable state, ClassLoader loader) {
//Need to only delete info from marked fragments (theoned that are stored on orientationchange
//Currently redoing the entire call; resulting in delay due to server call
//if(isLastOrientationPortrait != isPortrait){
if(state != null){
Bundle bundle1 = (Bundle) state;
bundle1.setClassLoader(loader);
Iterable<String> keys = bundle1.keySet();
Log.i("restoreState", "containsKey FragmentStatePagerAdapter: "+keys);
//android.support.v4.app.FragmentManager fragmentManager= fragmentAdapter.this.fragmentManager;
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//if (fragmentTransaction == null) {
// Log.i("restoreState", "fragmentTransactionTest");
fragmentTransaction = fragmentManager.beginTransaction();
// }
for (String key : keys) {
if (key.startsWith("f")) {
Fragment f = fragmentManager.getFragment(bundle1,
key);
fragmentTransaction.remove(f);
fragmentTransaction.commit();
}
}
}
//}
}
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
//return super.getItemPosition(object);
return fragmentAdapter.POSITION_NONE;
}
public fragmentAdapter(android.support.v4.app.FragmentManager fragmentManager,String orientation,
ArrayList<fireInfo> fireInfoList) {
super(fragmentManager);
this.orientation = orientation;
this.fireInfoList = fireInfoList;
this.numItems = fireInfoList.size();
this.fragmentManager=fragmentManager;
}
ArrayList<fireInfo> fireInfoList;
String orientation;
int numItems;
@Override
public int getCount() {
Log.i("numItems", "is: "+fireInfoList.size());
return numItems;
}
@Override
public Fragment getItem(int arg0) {
Log.i("fragmentAdapterIndex", "is: "+arg0);
return fireInfoFragment.newInstance(orientation, fireInfoList.get(arg0));
}
}
问题: 但是在我触发 startActivityFor 结果之前,新的 ArrayListOfObjects 与旧的一起添加。
如何强制 viewPager 忘记旧内容?基本上使用我的 fragmentStateAdapter 用这个 newArrayListofObjects 重置 viewPager 适配器?