How Android Viewpager and ScrollView nested scrolling solve

Source: Internet
Author: User
Tags abs gety

The innermost viewpager will always trigger the outermost viewpager scrolling when scrolling horizontally, requiring a custom viewpager.

Import Android.content.Context;
Import Android.graphics.PointF;
Import Android.support.v4.view.ViewPager;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;

/**
* Custom Viewpager to solve the problem of viewpagger nested use without sliding.
* Created by the Administrator on 2015/4/20.
*/
public class Horizontalinnerviewpager extends Viewpager {
/** the point **/when touching
PointF DOWNP = new PointF ();
/** the current point **/when touching
PointF curp = new PointF ();


Public Horizontalinnerviewpager (context context, AttributeSet Attrs) {
Super (context, attrs);
Mgesturedetector = new Gesturedetector (context, new Xscrolldetector ());
}
Public Horizontalinnerviewpager {
Super (context);

Mgesturedetector = new Gesturedetector (context, new Xscrolldetector ());
}

@Override
public boolean onintercepttouchevent (Motionevent ev) {
return super.onintercepttouchevent (EV);//default
}

@Override
public boolean ontouchevent (Motionevent ev) {
Record the current pressed coordinates each time the Ontouch event is performed
curp.x = Ev.getx ();
CURP.Y = Ev.gety ();

if (ev.getaction () = = Motionevent.action_down) {
Record the coordinates at the next time
Remember not to use DOWNP = Curp, so that when you change the CURP, DOWNP will also change
downp.x = Ev.getx ();
DOWNP.Y = Ev.gety ();
This code is to inform his parent Viewpager is now doing the operation of this control, do not interfere with my operation
GetParent (). Requestdisallowintercepttouchevent (True);
}

if (ev.getaction () = = Motionevent.action_move) {
This code is to inform his parent Viewpager is now doing the operation of this control, do not interfere with my operation
GetParent (). Requestdisallowintercepttouchevent (True);
}

return super.ontouchevent (EV);
}

}


Solve the above problem, and there are new problems, when the innermost viewpager vertical scrolling when the outer scrollview does not scroll, the solution is as follows:

Import Android.content.Context;
Import Android.graphics.PointF;
Import Android.support.v4.view.ViewPager;
Import Android.util.AttributeSet;
Import Android.view.GestureDetector;
Import android.view.MotionEvent;

/**
* Custom Viewpager to solve the problem of viewpagger nested use without sliding.
* Created by the Administrator on 2015/4/20.
*/
public class Horizontalinnerviewpager extends Viewpager {
/** the point **/when touching
PointF DOWNP = new PointF ();
/** the current point **/when touching
PointF curp = new PointF ();

/** Custom Gesture **/
Private Gesturedetector Mgesturedetector;

Public Horizontalinnerviewpager (context context, AttributeSet Attrs) {
Super (context, attrs);
Mgesturedetector = new Gesturedetector (context, new Xscrolldetector ());
}
Public Horizontalinnerviewpager {
Super (context);

Mgesturedetector = new Gesturedetector (context, new Xscrolldetector ());
}

@Override
public boolean onintercepttouchevent (Motionevent ev) {
return super.onintercepttouchevent (EV);//default
Returns True when the Intercept touch event arrives at this location,
Describes intercepting Ontouch in this control, and then executing the control's ontouchevent
return true;
The child control handles the event when it is near horizontal sliding, or it is handed to the parent control to handle the
return mgesturedetector.ontouchevent (EV);
}

@Override
public boolean ontouchevent (Motionevent ev) {
Record the current pressed coordinates each time the Ontouch event is performed
curp.x = Ev.getx ();
CURP.Y = Ev.gety ();

if (ev.getaction () = = Motionevent.action_down) {
Record the coordinates at the next time
Remember not to use DOWNP = Curp, so that when you change the CURP, DOWNP will also change
downp.x = Ev.getx ();
DOWNP.Y = Ev.gety ();
This code is to inform his parent Viewpager is now doing the operation of this control, do not interfere with my operation
GetParent (). Requestdisallowintercepttouchevent (True);
}

if (ev.getaction () = = Motionevent.action_move) {
float Distancex = curp.x-downp.x;
float Distancey = curp.y-downp.y;
Close to horizontal sliding, Viewpager control captures gestures, horizontal scrolling
if (Math.Abs (Distancex) > Math.Abs (Distancey)) {
This code is to inform his parent Viewpager is now doing the operation of this control, do not interfere with my operation
GetParent (). Requestdisallowintercepttouchevent (True);
}else{
Close to vertical slide, to be handled by the parent control
GetParent (). Requestdisallowintercepttouchevent (false);
}
}

return super.ontouchevent (EV);
}

Private class Xscrolldetector extends gesturedetector.simpleongesturelistener{
@Override
public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {
Return Super.onscroll (E1, E2, Distancex, Distancey);

The child control handles the event when it is near horizontal sliding, or it is handed to the parent control to handle the
Return (Math.Abs (Distancex) > Math.Abs (Distancey));
}
}

}

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.