8

我有一个 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 适配器?

4

2 回答 2

52

我想问题在于旧片段仍然存在于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才能(异步)执行。

于 2013-11-19T10:04:32.107 回答
0

我需要做的就是重新分配 FragmentStatePagerAdapter。

这已经到位,可以在 onActivityResult 中看到,但是掩盖正确行为的是我的 viewPager 页面指示器,它正在增加页面,例如,如果我在 viewPager 上有 2 个页面并调用子活动,它将在 arrayListOfObjects 上添加一个对象; viewPager 页面指示器将显示我现在有 2 加 3 页 (5)。

我不得不重置 onActivityResult 中的 viewPage 指示器,让它由浮动活动刚刚返回的这个新 arrayListOfObjects 确定。

于 2013-05-16T15:11:46.763 回答