Android Viewpager nested Viewpager cannot slide sub Viewpager

Source: Internet
Author: User
Tags gety

Now the most headache is the demand is not clear, this is not, there is a wonderful need to meet you, the parent Viewpager total three tabs, the first tab embedded a sub-viewpager, this time the problem comes, the son Viewpager cannot slide, I guess the Father Viewpager the Viewpager intercept, so tried the following first method

1. Overriding the Onintercepttouchevent method of the parent Viewpager

/** * @author Sub-ink * * 2015-2-26 a.m. 8:59:50 */public class Myviewpager extends Viewpager {public Myviewpager (context context, AttributeSet attrs) {Super (context, attrs);} @Overridepublic boolean onintercepttouchevent (Motionevent arg0) {return false;}}

Of course, there are many ways to rewrite sub-viewpager

2. Rewrite the child Viewpager, by getParent (). Requestdisallowintercepttouchevent (true) method to notify the parent control not to intercept

public class Myviewpager extends Viewpager {public Myviewpager (context context) {super (context);      } public Myviewpager (context context, AttributeSet Attrs) {Super (context, attrs);      } PointF downpoint = new PointF ();        Onsingletouchlistener Onsingletouchlistener; @Override public boolean ontouchevent (motionevent evt) {switch (evt.getaction ()) {case Motioneven              T.action_down://Record the coordinates at the time of downpoint.x = Evt.getx ();              Downpoint.y = Evt.gety (); if (This.getchildcount () > 1) {//has content, more than 1 hours//notifies its parent control, now is the operation of this control, does not allow the interception of getParent ().              Requestdisallowintercepttouchevent (TRUE);          } break; Case MotionEvent.ACTION_MOVE:if (This.getchildcount () > 1) {//has content, more than 1 hours//notifies its parent control, now in progress         Is the operation of this control and is not allowed to intercept getParent (). Requestdisallowintercepttouchevent (True);     } break; Case MOTIONEVENT.ACTION_UP://At up time to determine whether to press and let go of the coordinates of a point if (Pointf.length (Evt.getx ()-Downpoint.x,                  Evt.gety ()-Downpoint.y) < (float) 5.0) {Onsingletouch (this);              return true;          } break;      } return Super.ontouchevent (EVT); public void Onsingletouch (View v) {if (Onsingletouchlistener! = null) {Onsingletouchliste          Ner.onsingletouch (v);      }} public interface Onsingletouchlistener {public void Onsingletouch (View v); } public void Setonsingletouchlistener (Onsingletouchlistener onsingletouchlistener) {THIS.O      Nsingletouchlistener = Onsingletouchlistener;   }  }
Rewrite sub-viewpager code from Hangzhou Mountain not high

In fact, the above two methods are more inclined to the first, of course, the second can also solve the problem, but the second will break the process of distribution of events, directly tell the parent control do not intercept, my own is handling, there are some overbearing, haha, not in line with the mean


This can solve the problem, but the experience is not good, and finally gave up, I just share my problems and solutions, if you have a better way to communicate with each other.

Android Viewpager nested Viewpager cannot slide sub Viewpager

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.