2013-03-09 102 views
0

我正在開發一個應用程序與android 2.1包。 我使用ActionBarSherlock來獲取操作欄。 然後,我試圖使用一個Viewpager multilpe Fragments Class與自己的佈局。Android與Viewpager和ABS多個片段

但我所得到的是第一個片段。

我也用這個問題給出了建議,但沒有效果 See the Question Here

這是我用動作條和Viewpager MainActivity。

public class MainActivity extends SherlockFragmentActivity { 

private static final int NUMBER_OF_PAGES = 2; 
private MyFragmentPagerAdapter mMyFragmentPagerAdapter; 

ActionBar actionBar; 
ViewPager viewPager; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    actionBar = getSupportActionBar(); 
    actionBar.setHomeButtonEnabled(true); 
    //actionBar.setNavigationMode(actionBar.NAVIGATION_MODE_TABS); 

    viewPager = (ViewPager) findViewById(R.id.pager); 
    //viewPager.setOffscreenPageLimit(4); 

    mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager()); 
    viewPager.setAdapter(mMyFragmentPagerAdapter); 

} 



@Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex()); 
    } 


    private static class MyFragmentPagerAdapter extends FragmentPagerAdapter { 

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

     @Override 
     public Fragment getItem(int index) { 

      Fragment f = new Fragment(); 
      Log.d("index ", ""+index); 

      switch (index) { 
      case 0: 
       f = PageFragment.newInstance(index); 
      break; 
      case 1: 
       f = FragmentTwo.newInstance(index); 
      break; 


      default: 
      break; 
      } 

      return f; 


     } 

     @Override 
     public int getCount() { 

      return NUMBER_OF_PAGES; 
     } 
} 

}

這是我的一個片段類。

public class PageFragment extends SherlockFragment { 

public static PageFragment newInstance(int title) { 

    PageFragment pageFragment = new PageFragment(); 
    Bundle bundle = new Bundle(); 
    bundle.putInt("title", title); 
    pageFragment.setArguments(bundle); 
    return pageFragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment, container, false); 
    TextView textView = (TextView) view.findViewById(R.id.textView1); 
    textView.setText("View # 01"); 

    return view; 
} 

}

這裏是activity_main.xml中XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 


<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

    <fragment android:name="com.mzubair.test.PageFragment" 
      android:id="@+id/headlines_fragment" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 
    <fragment android:name="com.mzubair.test.FragmentTwo" 
      android:id="@+id/headlines_fragment2" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 

回答

1

從XML取下兩個片段標籤我的片段之一。

+0

檢查它。還是一樣 – Neo 2013-03-09 13:53:35

0

試試這個PagerAdapter(它適應你的代碼),並從佈局那些片段,你不想在下面添加或旁邊viewpager您的片段:/

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

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

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

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

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

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

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

    } 

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

    public void onPageScrollStateChanged(int state) { 

    } 

    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     // force l'appel de onOptionMenuPrepared 
     ((SherlockFragmentActivity) mContext).supportInvalidateOptionsMenu(); 
     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) { 

    } 

}