使用FragmentPagerAdapter向左側添加選項卡

[英]Add tabs to the left with FragmentPagerAdapter


This will be the last question I have about tabs, I promise :) (maybe) So from previous questions and try and error I have FragmentPagerAdapter called PagerAdapter which creates a few tabs and also gives me the ability to create new tabs dynamicly. The problem is that the new tabs will always get inserted to the RIGHT of my existing tabs:

這將是我對標簽的最后一個問題,我保證:)(也許)所以從以前的問題和嘗試和錯誤我有FragmentPagerAdapter調用PagerAdapter創建一些選項卡,並使我能夠動態創建新選項卡。問題是新選項卡將始終插入到現有選項卡的右側:

(Picture: http://s7.directupload.net/file/d/3198/73dyzvzv_png.htm

I tried the following:

我嘗試了以下方法:

  1. Add a static entry of "null" that would be skipped when creating the intial set of tabs. Then later when creating "Temp Tab" I could override that entry to whatever fragment I desired. Gave me some strange IllegalSTateException...

    添加一個靜態條目“null”,在創建初始選項卡集時將跳過該條目。然后在創建“Temp Tab”時,我可以將該條目覆蓋到我想要的任何片段。給了我一些奇怪的IllegalSTateException ...

  2. The thing I thought would work the best and actually really wanted to work badly: Instead of adding fragments with:

    我認為這將是最好的,實際上真的很想工作:而不是添加片段:

    private final List<Fragment> mFragments = new ArrayList<Fragment>();
    
    public void addFragment(Fragment fragment) {
        mFragments.add(fragment);
        notifyDataSetChanged();
    }
    

to use

mFragments.add(0, fragment);

But it gives an IllegalStateException with the message:

但是它給出了一條帶有消息的IllegalStateException:

Can't change tag of fragment ChatFragment

When I try to add the temp fragment. Now, two questions:

當我嘗試添加臨時片段時。現在,有兩個問題:

  1. Is there a way around that fragment tab, can I take away all fragment tags for a moment and rebuild them all or something? Or

    有沒有辦法繞過該片段選項卡,我可以暫時取走所有片段標簽並重建它們全部或其他東西嗎?要么

  2. Is there a much better and elegant way to do this? Or should I just redesign my UI so that the tab can be used to the right? :P

    有沒有更好,更優雅的方式來做到這一點?或者我應該重新設計我的用戶界面,以便選項卡可以正確使用? :P

EDIT: Here some code: (I never wanna just post a Wall of death to people because it might scare them off :o )

編輯:這里有一些代碼:(我從不想向人們發布死亡之牆,因為它可能嚇跑他們:o)

FragmentPagerAdapter:

public class PagerAdapter extends FragmentPagerAdapter {

  private Context mContext;

  public PagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    mContext = context;
  }

  private final List<Fragment> mFragments = new ArrayList<Fragment>();
  public void addFragment(Fragment fragment) {
    mFragments.add(fragment);
    notifyDataSetChanged();
  }

  @Override
  public Fragment getItem(int position) {
    return mFragments.get(position);
  }

  @Override
  public int getCount() {
    return mFragments.size();
  }

  @Override
  public String getPageTitle(int position) {

    switch (position) {
    case 0:
      return mContext.getString(R.string.tab_title_chats);
    case 1:
      return mContext.getString(R.string.tab_title_contacts);
    case 2:
      return mContext.getString(R.string.tab_title_create);
    }
    return "WOOPS";

  }

}

MainActivty:

public class LaunchActivity extends FragmentActivity implements ActionBar.TabListener {

  private PagerAdapter mAdapter;
  private FragmentManager man;
  private ViewPager mViewPager;
  private ActionBar actionBar;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_container);

    // Set up the action bar.
    actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Layout stuff
    mViewPager = (ViewPager) findViewById(R.id.masterViewPager);

    // Creating the mAdapter and initializing the mViewPager
    man = getSupportFragmentManager();
    mAdapter = new PagerAdapter(man, getApplicationContext());
    mViewPager.setAdapter(mAdapter);

    for (int i = 0; i < 2; i++) {
      makeFragments(i);
      System.out.println("Baking tasty fragment instances...nom nom nom");
    }

    // Called when tab changes
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
      @Override
      public void onPageSelected(int position) {
        actionBar.setSelectedNavigationItem(position);
      }
    });

    for (int i = 0; i < mAdapter.getCount(); i++) {
      System.out.println("Working...");

      actionBar.addTab(actionBar.newTab().setText(mAdapter.getPageTitle(i)).setTabListener(this));
    }

  }

  // Add fragment instances to ArrayList depending on their future position on the ActionBar
  private void makeFragments(int i) {

    if (i == 0) {
      mAdapter.addFragment(ChatFragment.getInstance());

    }
    else if (i == 1) {
      mAdapter.addFragment(ContactFragment.getInstance());
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.layout_container, menu);
    return true;

  }

  @Override
  public void onTabSelected(Tab tab, FragmentTransaction ft) {
    mViewPager.setCurrentItem(tab.getPosition());

  }

  public void addTab(int i) {
    mAdapter.addFragment(ComposeFragment.getInstance());
    System.out.println("Created new Fragment, now adding it as a tab");
    actionBar.addTab(actionBar.newTab().setText(mAdapter.getPageTitle(i)).setTabListener(this));
    System.out.println("Added it as a tab. Hurray");

  }

  /** IGNORE BELOW **/

  @Override
  public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  }

  @Override
  public void onTabReselected(Tab tab, FragmentTransaction ft) {
  }



}

Stacktrace:

    03-18 18:32:29.274: E/AndroidRuntime(26435): FATAL EXCEPTION: main
    03-18 18:32:29.274: E/AndroidRuntime(26435): java.lang.IllegalStateException: Can't change tag of fragment ChatFragment{41492428 #1 id=0x7f070004 android:switcher:2131165188:1}: was android:switcher:2131165188:1 now android:switcher:2131165188:2
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:398)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.support.v4.view.ViewPager.populate(ViewPager.java:992)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:523)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:495)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:875)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2690)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at com.package.subpackage.components.PagerAdapter.addFragment(PagerAdapter.java:25)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at com.package.subpackage.someapp.core.LaunchActivity.addTab(LaunchActivity.java:116)
    03-18 18:32:29.274: E/AndroidRuntime(26435):    at com.

com.package.subpackage.fragments.full.ChatFragment$1.onClick(ChatFragment.java:51)
03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.view.View.performClick(View.java:4204)
03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.view.View$PerformClick.run(View.java:17355)
03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.os.Handler.handleCallback(Handler.java:725)
03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.os.Looper.loop(Looper.java:137)
03-18 18:32:29.274: E/AndroidRuntime(26435):    at android.app.ActivityThread.main(ActivityThread.java:5041)
03-18 18:32:29.274: E/AndroidRuntime(26435):    at java.lang.reflect.Method.invokeNative(Native Method)
03-18 18:32:29.274: E/AndroidRuntime(26435):    at java.lang.reflect.Method.invoke(Method.java:511)
03-18 18:32:29.274: E/AndroidRuntime(26435):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-18 18:32:29.274: E/AndroidRuntime(26435):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-18 18:32:29.274: E/AndroidRuntime(26435):    at dalvik.system.NativeStart.main(Native Method)

1 个解决方案

#1


0  

I could achieve this by overriding onPageSelected method of OnPageChangeListener. Consider you have three pages in this order A<->B<->C. To goal is to reach C if we scroll right from A and similarly to reach A if we scroll left from C.

我可以通過覆蓋OnPageChangeListener的onPageSelected方法來實現這一點。考慮你有三個頁面順序A < - > B < - > C。如果我們從A向右滾動,目標是達到C,如果我們從C向左滾動,則達到A。

To do this, define your to have 5 pages (3+2), and organize the pages as follows:

要執行此操作,請將您的頁面定義為5頁(3 + 2),並按如下方式組織頁面:

C<->A<->B<->C<->A

Now in the onPageSelected method, check and if position if 0, change it to 3 (getCount()-2) and if position is 4 (getCount()-1), change it to 1. Make sure to this like can help u .

現在在onPageSelected方法中,檢查並且如果位置為0,則將其更改為3(getCount() - 2),如果position為4(getCount() - 1),則將其更改為1.確保這樣可以幫助您。

Thanks


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/03/18/af71b5a54b6cd38fcb28521440987813.html



 
  © 2014-2022 ITdaan.com 联系我们: