First you should create a layout with ViewPager:
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Oncreate your Activity:
setContentView(R.layout.activity_main);
ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
Then you should create a class that will be your adapter:
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter
{
public AppSectionsPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int i)
{
Fragment fragment = null;
switch (i)
{
case 0:
fragment = new Main_fragment();
break;
case 1:
fragment = new Lock_Fragment();
}
return fragment;
}
@Override
public int getCount()
{
return 2;
}
@Override
public CharSequence getPageTitle(int position)
{
String tab = null;
switch (position)
{
case 0:
tab = "Main";
break;
case 1:
tab = "Bloqueio";
}
return tab;
}
}
Now you should put your adapter to ViewPager:
AppSectionsPagerAdapter mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAppSectionsPagerAdapter);
Remembering that "Main_fragment ();" and "Lock_Fragment ();" are fragmentation, you can create and register as many as you want, without forgetting to modify the:
@Override
public int getCount()
{
return 2;
(In my case I returned two snippets)
And the titles for each position.