6

używam adaptera pager fragmentu z 5 stron. i im ustawienie adaptera aby pager jak poniżejUsuń i dodaj stronę do FragmentPagerAdaptera

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; 
      } 

     } 

aw fragmentu utworzyć układ liniowy indywidualnie dla każdej strony jak poniżej

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

i IM ustawienie adaptera poniżej

m_oPager.setAdapter(m_oPagerAdapter); 

Moje pytanie brzmi: jak mogę dynamicznie dodawać i usuwać 5. stronę ... jak w przypadku niektórych przypadków potrzebuję strony 5, a niektóre sprawy muszę usunąć na stronie 5.

Odpowiedz

4

Mam coś, co może pomóc, nie zmieniam Fragment, po prostu usuwam i dodajemy z powrotem. To zmieni się tylko z ostatniej pozycji. Innymi słowy, jeśli masz pięćFragments i chcesz usunąć trzeci jeden, to nie będzie działać. Nie jestem pewien, że to jest najlepsze rozwiązanie, ale tutaj jest to, co mam zrobić:

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

Następnie, jako próbkę Używam dodawać i usuwać click metody:

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(); 
} 

I dodanej setCount() metoda do mojego SectionsPagerAdapter:

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

Zmieniono getCount() się i dodaje liczbę domyślnie:

private int _count = 2; 

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

Oto moje pełne 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

hi ... dzięki za odpowiedź ... ale próbowałem it..the problem ..when liczba zmienia się i notfy adapter jest nazywany strona nie jest sidplayed, ale zawartość strony jest wyświetlana, gdy przewijam obecną ostatnią stronę ... ponieważ adpater ma ostatnią stronę utworzoną na początku – user1340801

+0

To dziwne, nie dostaję tej samej reakcji, nie jestem w stanie aby przewinąć do strony, którą usunąłem. – jnthnjns

+0

nie można przeskoczyć, ale po wydłużeniu przewijania można zobaczyć zawartość tej strony ... nie zobaczysz tytułu i tylko treść: – user1340801

Powiązane problemy