Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager inside ViewPager

I would like to create a ViewPager (with three items) where each of its view is another ViewPager (with two items). User then swipe items like this:

ViewPager1[0] ViewPager2[0] ViewPager1[0] ViewPager2[1] ViewPager1[1] ViewPager2[0] ViewPager1[1] ViewPager2[1] ViewPager1[2] ViewPager2[0] ViewPager1[2] ViewPager2[1] 

How would that be possible?

like image 917
xpepermint Avatar asked Aug 17 '11 20:08

xpepermint


People also ask

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

What is ViewPager used for?

Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android.

How do I stop ViewPager swiping?

To enable / disable the swiping, just overide two methods: onTouchEvent and onInterceptTouchEvent . Both will return "false" if the paging was disabled. You just need to call the setPagingEnabled method with false and users won't be able to swipe to paginate.


2 Answers

override canScroll in the parent ViewPager:

@Override protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {    if(v != this && v instanceof ViewPager) {       return true;    }    return super.canScroll(v, checkV, dx, x, y); } 
like image 183
straya Avatar answered Sep 29 '22 12:09

straya


Try this:

public class CustomViewPager extends ViewPager {     private int childId;      public CustomViewPager(Context context, AttributeSet attrs) {         super(context, attrs);     }      @Override     public boolean onInterceptTouchEvent(MotionEvent event) {         if (childId > 0) {                       ViewPager pager = (ViewPager)findViewById(childId);              if (pager != null) {                            pager.requestDisallowInterceptTouchEvent(true);             }          }          return super.onInterceptTouchEvent(event);     }      public void setChildId(int id) {         this.childId = id;     } } 
like image 39
Android Noob Avatar answered Sep 29 '22 12:09

Android Noob