以下是您需要在示例中进行的更改,或者也可以创建一个新项目。
1.将您的所有视图写为片段,而不是您希望在导航选项卡中显示的活动,例如
Fragment1, Fragment2,...
Fragment 将是所有 Fragment 的超类。
为每个片段创建 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.现在为您的活动创建一个 xml 布局,仅在其中包含一个 viewpager(我正在使用支持 v4 库作为片段)
<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.现在这里是您的 MainActivity.java 的代码 sn-p(示例中相同,没有任何变化)
@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,但它与示例一起提供,无需更改任何内容。
所有这些类(MainActivity、YourFragmentPagerAdapter、Fragment1、Fragment2..)都在 Studio 示例的同一个 Activity 类中,但您可以将它们全部分开,我更喜欢这样。