2012-04-02 176 views
6

我目前正在嘗試爲我將來的應用程序製作框架。我非常喜歡ActionBar和ViewPager,但錯過了一個功能。我需要在運行時替換一個Fragment/Tab。替換Viewpager中的片段/選項卡

使用官方的example,我很想看到類似replaceTab()的東西,但片段本身沒有更新,無論我做什麼。

回答

1

你試試:

fragmentTransaction.remove(yourframent); 

然後:

fragmentTransaction.add(yournewfragment); 
0

非常接近你想要什麼,我需要添加片段(末)到ViewPager,而這樣做:

FragmentTransaction ft = fragmentManager.beginTransaction(); 
    new_fragment = Fragment.instantiate(activity, class_name, args); 

    // The below code removes old fragment and adds the new one at the end. 
    ft.attach(new_fragment);    // <- this adds the fragment at the end 
    ft.detach(old_fragment);    // <- this removes old fragment 

    // This should be what you're looking for when adding to an Activity instead of a ViewPager 
    ft.replace(view_id, new_fragment); // <- this is supposed to replace the fragment attached to view_id 

    // Or if the replace does not work, this might work: 
    ft.detach(old_fragment); 
    ft.add(view_id, new_fragment); 

    // Not to be forgotten: ;) 
    ft.commit(); 

上面的代碼可能需要一些調整,也許只需要替換呼叫以有效地工作?

如果替換調用失敗,您仍可以分離所有片段並按照所需順序重新連接它們。

2

您必須使用FragmentPagerAdapter來處理您的片段更改。有3件重要的事情:

  • 刪除前面的片段,如果它存在。

  • 調用notifyDataSetChanged()刷新頁面列表。

  • 返回getItemPosition中的POSITION_NONE如果它要求舊片段。

這是我使用左,右頁的代碼。我根據用戶的操作動態改變片段。

public class MyPager extends ViewPager { 

    private MyPagerAdapter mMyPagerAdapter; 

    public MyPager(Context context, FragmentActivity activity) { 
     super(context); 

     mMyPagerAdapter = new MyPagerAdapter(activity.getSupportFragmentManager()); 
     setAdapter(mMyPagerAdapter); 

    } 
    public void replaceLeftFragment(Fragment fragment) { 
     mMyPagerAdapter.replaceLeftFragment(fragment); 
    } 
    public void replaceRightFragment(Fragment fragment) { 
     mMyPagerAdapter.replaceRightFragment(fragment); 
    } 
} 

public class MyPagerAdapter extends FragmentPagerAdapter { 

    private FragmentManager mFragmentManager; 
    private Fragment mLeftFragment; 
    private Fragment mRightFragment; 

    public MyPagerAdapter(FragmentManager fm) { 
     super(fm); 
     mFragmentManager = fm; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     if (position == 0) { 
      return mLeftFragment; 
     } else { 
      return mRightFragment; 
     } 
    } 

    @Override 
    public int getCount() { 
     final int nbLeft = (mLeftFragment == null) ? 0 : 1; 
     final int nbRight = (mRightFragment == null) ? 0 : 1; 
     return (nbLeft + nbRight); 
    } 

    public void replaceLeftFragment(Fragment fragment) { 
     if (mLeftFragment != null) { 
      mFragmentManager.beginTransaction().remove(mLeftFragment).commit(); 
     } 
     mLeftFragment = fragment; 
     notifyDataSetChanged(); 
    } 
    public void replaceRightFragment(Fragment fragment) { 
     if (mRightFragment != null) { 
      mFragmentManager.beginTransaction().remove(mRightFragment).commit(); 
     } 
     mRightFragment = fragment; 
     notifyDataSetChanged(); 
    } 

    @Override 
    public int getItemPosition(Object object) { 
     if ((object!=mLeftFragment) && (object!=mRightFragment)) { 
      return POSITION_NONE; 
     } 
     return super.getItemPosition(object); 
    } 

}