I'm really newbie in android and I would appreciate any help for my course work.
I need to do:
1) two ViewPagers (not nested) in one Activity
2) two ViewPagers (one ViewPager is nested in another ViewPager)
I found similar question, but I could not use it.ViewPager inside ViewPager
I add first ViewPager and do not know what to do next
LayoutInflater inflater = LayoutInflater.from(this); //this - context of my activity
List<View> pages = new ArrayList<View>();
View page = inflater.inflate(R.layout.activity_main, null);
//next I adding some buttons on page
pages.add(page);
page = inflater.inflate(R.layout.activity_main2, null); //my second page
//some buttons
pages.add(page);
page = inflater.inflate(R.layout.activity_main3, null); //my third page
//some buttons
pages.add(page);
SamplePagerAdapter pagerAdapter = new SamplePagerAdapter(pages);
ViewPager viewPager = new ViewPager(this);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0);
setContentView(viewPager);
If I add ViewPager2 and say setContentView(viewPager2), I lost my viewPager1. I spent a lot of time for this two questions, please, some help...
//This is my sketch what I need to do. Sorry, I can't attach my scetches...
I added an
OnTouchListener
to the interiorViewPager
:This just detects ACTION_DOWN touch events on the inner
ViewPager
and prevents the outer one from intercepting it. Because it returns false, only the ACTION_DOWN event should be hit; all the other events will be ignored. You can add this listener to every element you want to "protect" from the outerViewPager
's scrolling, though obviously if you want to pick up any other touch behaviour on those elements you'll need to deal with them inside the touch listener and possibly implement a better listener.A solution about having a nested Viewpager and disabling the nested Viewpager swiping (if you want to set the selected pages with buttons in the nested)
For the nested You SubClass ViewPager and you add these:
In the Layouts you put this:
I think you should reconsider having a viewpager inside another viewpager. You will have a lot of problems with the touch events plus the user experience might be confusing/tricky, I suggest you to rethink that one.
Now for question 1:
Declare both viewpagers in the xml file of the activity (/layout/activity.xml), for example:
Then inflate the xml on the onCreate method of your activity and wire both of the viewpagers:
Don't forget to declare those two variables
private ViewPager mViewpager1, mViewpager2;
Then you can do whatever you want with mViewpager1 and mViewpager2. One more tip, i suggest you to use adapters to set the pages instead of adding them manually one by one to each viewpager, it will be much cleaner and better to operate with.