In this tip, I will give a simple way to implement OnPageChangeListener interface to put data from ViewPager to Activity (update TextView and RatingBar status in Activity).
1. Declaring some xml files:
- Activity layout (activity_view_pager.xml):- Fragment layout (fragment_view_pager.xml):
2. Programmatically coding:
- Create a model - implements Serializable to put through Bundle later (Employee.java):- Customizing a ViewPager adapter (extends FragmentStatePagerAdapter):
- Programmatically code for each ViewPager Fragment:
- Activity coding:
* For "listening" ViewPager change page, we provide a onPageChangedListener method to get data from adapter and update data in views (TextView and RatingBar):
* Events code of button Back and Next (change ViewPager's page):
Full ViewPagerActivity.java code:
3. Result after build and run (click for full size):