2012-12-18 58 views
0

這是我的傳呼機適配器。如何標記或ID添加到一個片段

public static class PagerAdapter extends FragmentPagerAdapter implements 
     ActionBar.TabListener, ViewPager.OnPageChangeListener { 
    private final Context mContext; 
    private final ActionBar mActionBar; 
    private final ViewPager mViewPager; 
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); 

    static final class TabInfo { 
     private final Class<?> clss; 
     private final Bundle args; 

     TabInfo(Class<?> _class, Bundle _args) { 
      clss = _class; 
      args = _args; 
     } 
    } 

    public PagerAdapter(SherlockFragmentActivity activity, ViewPager pager) { 
     super(activity.getSupportFragmentManager()); 
     mContext = activity; 
     mActionBar = activity.getSupportActionBar(); 
     mViewPager = pager; 
     mViewPager.setAdapter(this); 
     mViewPager.setOnPageChangeListener(this); 
    } 

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) { 
     TabInfo info = new TabInfo(clss, args); 
     tab.setTag(info); 
     tab.setTabListener(this); 
     mTabs.add(info); 
     mActionBar.addTab(tab); 
     notifyDataSetChanged(); 
    } 

    public int getCount() { 
     return mTabs.size(); 
    } 

    public SherlockFragment getItem(int position) { 
     TabInfo info = mTabs.get(position); 
     return (SherlockFragment) Fragment.instantiate(mContext, 
       info.clss.getName(), info.args); 
    } 

    public void onPageSelected(int position) { 
     mActionBar.setSelectedNavigationItem(position); 
    } 

    public void onPageScrollStateChanged(int state) { 
    } 

    public void onPageScrolled(int position, float positionOffset, 
      int positionOffsetPixels) { 
    } 

    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     mViewPager.setCurrentItem(tab.getPosition()); 
     Log.d("Tab position selected = ", "" + tab.getPosition()); 
     // Log.v(TAG, "clicked"); 
     Object tag = tab.getTag(); 
     for (int i = 0; i < mTabs.size(); i++) { 
      if (mTabs.get(i) == tag) { 
       mViewPager.setCurrentItem(i); 
      } 
     } 
    } 

    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 

    } 

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

如何在每個選項卡中爲片段添加標記或ID。以便我可以稍後訪問它們。

謝謝

回答

4

您可以使用覆蓋此方法並應用定製的片斷,

@Override 
public Fragment getItem(int position) { 
    if (position == 0) 
     fragment = new Fragment1(); 
    else (position == 1) 
     fragment = new Fragment2(); 
    return fragment; 
} 

等,如果還有什麼過你的要求實施和覆蓋此方法,

@SuppressLint("DefaultLocale") 
    public CharSequence getPageTitle(int position) { 

     switch (position) { 
     case 0: 
      return ; 
     case 1: 
      return ; 
     } 
     return null; 
    } 

嘗試一下。

+0

我沒有得到你。我想獲得一個Tab使用的特定片段。使用任何兩種方法之一:** ** findFragmentByTag或** ** findFragmentById。所以,我在哪裏初步確定它們添加標籤時至FragmentPagerAdapter –

+0

覆蓋此兩種方法,並實現了邏輯代碼 –

+0

你的意思是:不是** ** findFragmentByTag **或** findFragmentById使用** PagerAdapter.getItem(位置)**獲取相應的片段? –

相關問題