6

我正在使用5頁的片段尋呼機適配器。和IM設置適配器,以查看尋呼機如下刪除並添加頁面到FragmentPagerAdapter

public class xxx extends FragmentPagerAdapter 
    { 
     final int PAGE_COUNT_LOGGED_IN = 6; 
     final int PAGE_COUNT_LOGGED_OUT = 2; 
     TextView oTextView = null; 
     LayoutInflater inflater = null; 
     PagerTabStrip m_oPageTabStrip = null; 
     String m_strTab = null; 
     String[] m_strArray = null;  
     Context m_oContext = null; 

     /** Constructor of the class */ 
     public xxxx (Context context, android.support.v4.app.FragmentManager oFragmentManager) 
     { 
      super (oFragmentManager); 
      m_oContext = context; 
     } 

     /** This method will be invoked when a page is requested to create */ 
     @Override 
     public Fragment getItem(int arg0) 
     {  
      xxxxFragment myFragment = new xxxFragment(); 
      Bundle data = new Bundle(); 
      data.putInt("current_page", arg0+1); 
      myFragment.setArguments(data); 
      return myFragment; 
     } 

     /** Returns the number of pages */ 
     @Override 
     public int getCount() 
     {  
      if (Utility.m_bIsLoggedIn == true) 
      { 
       return PAGE_COUNT_LOGGED_IN; 
      } 
      else 
      { 
       return PAGE_COUNT_LOGGED_OUT; 
      } 
     } 

     @Override 
     public CharSequence getPageTitle(int position) 
     { 
      String strText = " "; 

      switch(position) 
      { 
      case 0: 
       strText = getBaseContext().getString(R.string.ccc); 
       break; 
      case 1: 
       strText = getBaseContext().getString(R.string.bbb); 
       break; 
      case 2: 
       strText = getBaseContext().getString(R.string.bbb); 
       break; 
      case 3: 
       strText = getBaseContext().getString(R.string.bbb); 
       break; 
      case 4: 
       strText = getBaseContext().getString(R.string.bbb); 
       break; 
      case 5: 
       strText = getBaseContext().getString(R.string.Sbbb); 
       break; 
      } 

     } 

和在片段我單獨創建一個線性佈局對於每一頁,如下

bbb.m_oSharedPage = (LinearLayout) LayoutInflater.from(Utility.m_oService).inflate(R.layout.viewpage, null); 
          iView = Utility._YOURS_SHARED; 
          oCurrentPage = .m_oSharedPage; 

和IM設置適配器如下

m_oPager.setAdapter(m_oPagerAdapter); 

我的問題是我如何添加和刪除第5頁動態...像某些情況下,我需要第5頁和一些情況下,我需要刪除第5頁。

回答

4

我有些事可能會有所幫助,我不會更改Fragment,只需將其刪除並將其添加回來即可。這隻會從最後一個位置改變。換句話說,如果您有五個Fragments並且想要刪除第三個一個,那麼這將不起作用。我不知道這是最好的解決辦法,但這裏是我做了什麼:

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

那麼,作爲樣品我使用的是添加和刪除click方法:

public void remove(View v) { 
    // Change the count to whatever you would like 
    mSectionsPagerAdapter.setCount(1); 
    // Triggers a redraw of the PageAdapter 
    mSectionsPagerAdapter.notifyDataSetChanged(); 
} 

public void add(View v) { 
    // Change the count back to the initial count 
    mSectionsPagerAdapter.setCount(2); 
    // Triggers a redraw of the PageAdapter 
    mSectionsPagerAdapter.notifyDataSetChanged(); 
} 

我加一個setCount()方法我SectionsPagerAdapter

public void setCount(int count) { 
    this._count = count; 
} 

改變getCount()並添加缺省計:

private int _count = 2; 

@Override 
public int getCount() { 
    return this._count; 
} 

這裏是我的全部SectionsPagerAdapter

public class SectionsPagerAdapter extends FragmentPagerAdapter { 
    private int _count = 2; 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     return super.instantiateItem(container, position); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
     case 0: 
      return new FragmentOne(); 
     case 1: 
      return new FragmentTwo(); 
     default: 
      return null; 
     } 
    } 

    public void setCount(int count) { 
     this._count = count; 
    } 

    @Override 
    public int getCount() { 
     return this._count; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
     case 0: 
      return getString(R.string.fragment_title_one).toUpperCase(Locale.ENGLISH); 
     case 1: 
      return getString(R.string.fragment_title_two).toUpperCase(Locale.ENGLISH); 
     } 
     return null; 
    } 
} 
+1

喜...感謝您的答案...但我已經試過it..the問題..when數發生變化,notfy適配器被稱爲頁面沒有顯示,但當我滾動當前最後一頁時顯示頁面的內容...因爲adpater在開始時創建了最後一頁 – user1340801 2013-04-08 07:13:07

+0

奇怪的是,我沒有得到同樣的反應,我不能滾動到我已刪除的頁面。 – jnthnjns 2013-04-08 12:27:40

+0

不,你不能滾動,但當你拉長滾動,你可以看到該頁面的內容...你不會看到標題和所有,但只是內容 – user1340801 2013-04-09 11:41:51

相關問題