下面是您需要在示例中進行的更改或者可以創建新項目。
1.Write你要在你的導航選項卡顯示像
Fragment1, Fragment2,...
片段將是所有的碎片的超類的所有意見的片段,而不是活動。
爲您的每個片段的創建XML佈局,並覆蓋您的片段類像onCreateView方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from respectivelayout.xml
View view = inflater.inflate(R.layout.respectivelayoput, container, false);
// Do whatever you want to do like an activity here for all the tabs
return rootView;
}
2.創建它覆蓋FragmentPagerAdapter的選項卡之間滑動,這樣
東西的新類
public class ViewPagerAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 2;
Context context;
public ViewPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1.java
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1(context);
return fragmenttab1;
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2(context);
return fragmenttab2;
//And so on.., make sure cases must be equal to page count you specified
}
return null;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
}
3.Now爲您的活動只拿着viewpager裏面像(我使用的片段支持V4庫)創建一個XML佈局
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></android.support.v4.view.ViewPager>
4.Now這裏你MainActivity.java的代碼段(在exapmle沒有同改變)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
yourAdaperInstance= new YourFragmentPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager); //created in xml
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < yourAdaperInstance.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
// getCount and getPageTitle are defined in your FragmentPagerAdapter, This is the best practice or else you can add tabs and define names everything in Activity also
}
您需要設置TabListener很好,但它自帶的例子沒有什麼需要改變的。
對於Studio示例,所有這些類(MainActivity,YourFragmentPagerAdapter,Fragment1,Fragment2 ..)都在相同的Activity類中,但您可以將它們全部分開,我更喜歡這樣。