我有一个ViewPager,我使用FragmentStatePagerAdapter填充片段(表示来自arrayListOfObjects的对象)。 一切运作良好:
mMyFragmentPagerAdapter = new fragmentAdapter(getSupportFragmentManager(),orientation
,rePopulatedfireInfoList);
mPager = (ViewPager)findViewById(R.id.fireInfoFragment_container);
initButton();
setTab();
mPager.setAdapter(mMyFragmentPagerAdapter);
片段适配器扩展了FragmentStatePagerAdapter。
从主要活动开始,我发起一个对话主题活动;用户可以添加新的收藏位置,创建一个新对象,改变主要活动传递的对象的arraylist。 这是启动对话活动的代码;一切正常:
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适配器答案 0 :(得分:46)
我想问题在于旧片段仍然存在于您用于适配器的FragmentManager
中。如果是这种情况,您只需从片段管理器中删除所有旧片段。
所以基本上只需在适配器的构造函数中执行以下代码:
public fragmentAdapter(FragmentManager fragmentManager, String orientation, ArrayList<fireInfo> list) {
super(fragmentManager);
if (fragmentManager.getFragments() != null) {
fragmentManager.getFragments().clear();
}
//... your other code here
}
这行代码是不必要的:
mMyFragmentPagerAdapter1.notifyDataSetChanged();
编辑:使用FragmentTransaction
删除片段可能更为正确:
List<Fragment> fragments = fragmentManager.getFragments();
if (fragments != null) {
FragmentTransaction ft = fragmentManager.beginTransaction();
for (Fragment f : fragments) {
//You can perform additional check to remove some (not all) fragments:
if (f instanceof AddedByCurrentPagerAdapterFragment) {
ft.remove(f);
}
}
ft.commitAllowingStateLoss();
}
FragmentTransaction
执行(异步)需要一些时间。
答案 1 :(得分:0)
我需要做的就是重新分配FragmentStatePagerAdapter。
这已经在onActivityResult中可以看到,但屏蔽了正确行为的是我的viewPager页面指示器,它正在递增页面,例如如果我在viewPager上有2个页面并调用子actvity,它将在arrayListOfObjects上添加一个对象; viewPager页面指示器将显示我现在有2加3页(5)。
我不得不重置onActivityResult中的viewPage指示符,让它由浮动活动刚刚返回的这个新的arrayListOfObjects确定。