In last tutorial i wrote about how to access Fragments of ViewPager inside Parent Activity. But this this post i will write about how you can create ViewPager without having any Fragment in it. So this will be more developer friendly because sometime RecyclerView inside ViewPager will create problem if you are using ViewPager inside ScrollView or NestedScrollView. So this that case you can simply use RecyclerView as Pages of ViewPager.
But you need ViewPager adapter to control Views of ViewPager. In normal PagerAdapter we need to override below 2 methods.
public Object instantiateItem(ViewGroup collection, int position) {int resId = 0;switch (position) {case 0:resId = R.id.page_one; //pass id of that view to return, Views will be added in XML.break;case 1:resId = R.id.page_two;break;}return findViewById(resId); // return selected view.}//and@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1; // return true if both are equal.}
ActivityClass java file
activity_scrolling.xml file
As you can see i have added two LinearLayout inside ViewPager in xml file. and passed id’s of that LinearLayouts inside PagerAdapter.
Quick Links
Legal Stuff
Social Media