Вот что вам нужно сделать в примере или создать новый проект.
1.Запись ваши все мнения как фрагменты вместо деятельности, которые вы хотите показать в ваших вкладок навигации, как
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.Create новый класс, который переопределяет 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 создать макет 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.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 как хорошо, но это приходит с примером в этом ничего не нужно менять.
Все эти классы (MainActivity, YourFragmentPagerAdapter, Fragment1, Fragment2 ..) находятся в одном классе Activity для примера Studio, но вы можете сделать их все отдельными, я предпочитаю это.