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));
}
}
}