To insert child views that represent each page, you need to hook this layout to a PagerAdapter. There are two kinds of adapter you can use:
FragmentPagerAdapter
This is best when navigating between sibling screens representing a fixed, small number of pages.
FragmentStatePagerAdapter
This is best for paging across a collection of objects for which the number of pages is undetermined. It destroys fragments as the user navigates to other pages, minimizing memory usage.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="iterator.ir.a043.MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewPager">
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
fragment_page.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="iterator.ir.a043.PageFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textViewCounter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Fragment"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</RelativeLayout>
</FrameLayout>
PageFragment.java
public class PageFragment extends Fragment {
public PageFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_page, container, false);
TextView textViewCounter= (TextView) view.findViewById(R.id.textViewCounter);
Bundle bundle=getArguments();
int counter=bundle.getInt("counter");
String output=String.format("Page %d",counter);
textViewCounter.setText(output);
return view;
}
}
SwipeAdapter.java
public class SwipeAdapter extends FragmentStatePagerAdapter {
public SwipeAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=new PageFragment();
Bundle bundle=new Bundle();
bundle.putInt("counter",position+1);
fragment.setArguments(bundle);
return fragment;
}
@Override
public int getCount() {
return 5;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.viewPager);
SwipeAdapter adapter=new SwipeAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
}
}
References
https://github.com/mhdr/AndroidSamples/tree/master/043
https://developer.android.com/training/implementing-navigation/lateral.html