Problem Description:
-
My nesting is viewpager-->scrollview-->viewpager.
-
First of all the innermost viewpager horizontal scrolling will always trigger the outermost viewpager scrolling, see many of the online solutions are basically the same, need to customize 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, resolves Viewpagger nesting when used without sliding issues. * Created by Administrator on 2015/4/20. */public class horizontalinnerviewpager extends viewpager { /** touch-pressed point **/ pointf downp = new pointf (); /** Touch the current point **/ pointf curp = new pointf (); public horizontalinnerviewpager (context context, attributeset attrs) { super (context, attrs); mgesturedetector = new gesturedetector (Context, new xscrolldetector ()); &nbSp; } public horizontalinnerviewpager (Context context) { 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) { //each time the Ontouch event is logged, the current pressed coordinates are recorded curP.x = ev.getx (); curp.y = ev.gety (); if (Ev.getactioN () == motionevent.action_down) { //record the coordinates at the time of //remember not to use  DOWNP  = CURP&NBSP, so DOWNP will change when CURP is changed downp.x = ev.getx (); downp.y = ev.gety (); // This code is to inform his father that 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 that Viewpager is now doing the operation of this control, Do not interfere with my operation &Nbsp; getparent (). Requestdisallowintercepttouchevent (True); } return super.ontouchevent (EV); }}
3. Solve the above problem, there is a new problem, when the innermost viewpager vertical scrolling when the outer layer of the 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, Resolves the problem when viewpagger nesting is used without sliding. * Created by Administrator on 2015/4/20. */public class horizontalinnerviewpager extends viewpager { /** touch-pressed point **/ pointf downp = new pointf (); /** Touch the current point **/ pointf curp = new pointf (); /** Custom Gestures **/ private gesturedetector mgesturedetector; public horizontalinnerviewpager (context context, attributeset attrs) { super (context, attrs); &nBsp; mgesturedetector = new gesturedetector (Context, new xscrolldetector ()); } public Horizontalinnerviewpager (Context context) { super ( context); mgesturedetector = new gesturedetector (Context, new xscrolldetector ()); } @Override public boolean onintercepttouchevent (Motionevent ev) { return super.onintercepttouchevent (EV);//default //when the Intercept touch event arrives at this position, return to true, // Instructions to intercept ontouch in this control, and then execute the ontouchevent// return true; of this control //close to waterThe child control handles the event when it is smoothed, otherwise it is handed to the parent control to handle the// return Mgesturedetector.ontouchevent (EV); } @Override public boolean ontouchevent (Motionevent ev) { //each time the Ontouch event is logged, the current pressed coordinates are recorded curP.x = Ev.getx (); curp.y = ev.gety (); if (Ev.getaction () == motionevent.action_down) { //record the coordinates at the time of the press //remember not to use downp = curp , so when changing curp, DOWNP will also change downp.x = ev.getx (); downp.y&nbSp;= ev.gety (); // This code is to inform his father that 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 swipe, Viewpager control capture gesture, horizontal scrolling if (Math.Abs (Distancex) > math.abs (Distancey)) { //This code is to inform his father Viewpager is now doing the operation of this control, do not interfere with my operation getparent (). Requestdisallowintercepttouchevent (True); }else{ //close to vertical swipe to parent control handling getparent (). Requestdisallowintercepttouchevent ( False); } } return super.ontouchevent (EV); } private class XScrollDetector extends gesturedetector.simpleongesturelistener{ @Override &nbsP;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 close to the horizontal slide, or it is handed to the parent control handle return (Math.Abs (Distancex) > math.abs (Distancey)); } }}
Android Viewpager and ScrollView nested scrolling problem solving solution