ViewPager nested viewPager in Android cannot slide sub-viewPager, androidviewpager

Source: Internet
Author: User
Tags gety

ViewPager nested viewPager in Android cannot slide sub-viewPager, androidviewpager

The biggest headache is that the demand is not clear. No, there are three tabs in the parent viewPager, and a child viewPager is embedded in the first tab, at this time, the problem arises. The child viewPager cannot slide. I guess the parent viewPager intercepts the child viewPager, so I tried the first method below.

1. Override the onInterceptTouchEvent method of the parent viewPager.

/*** @ Author sub-ink ** 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 methods, you can also try to override the child viewPager

2. Override the child viewPager and notify the parent control not to intercept through the getParent (). requestDisallowInterceptTouchEvent (true) method.

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 MotionEvent. ACTION_DOWN: // record the downPoint coordinate when it is pressed. x = Evt. getX (); downPoint. y = evt. getY (); if (this. getChildCount ()> 1) {// when there are more than one content, // notify its parent control. The current operation is performed on this control and the getParent () cannot be intercepted (). requestDisallowInterceptTouchEvent (true);} break; case MotionEvent. ACTION_MOVE: if (this. getChildCount ()> 1) {// when there are more than one content, // notify its parent control. The current operation is performed on this control and the getParent () cannot be intercepted (). requestDisallowInterceptTouchEvent (true);} break; case MotionEvent. ACTION_UP: // when the group is up, determine whether to press or the coordinate of the release operator to be a point if (Po IntF. 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) {onSingleTouchListener. onSingleTouch (v) ;}} public interface OnSingleTouchListener {public void onSingleTouch (View v);} public void setOnSingleTouchListener (OnSingleTouchListener onSingleTouchListener) {this. onSingleTouchListener = onSingleTouchListener ;}}
Rewrite the child 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 destroy the event distribution process and directly tell the parent control not to intercept, I handle it myself, but it's always a bit overbearing. Haha, it's not in line with the moderate saying.


In this way, the problem can be solved, but the experience is poor, and I finally gave up. I just shared my problems and solutions. If you have a better way to communicate with each other.

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.