Android Viewpager and ScrollView nested scrolling problem solving solution

Source: Internet
Author: User

Problem Description:

    1. My nesting is viewpager-->scrollview-->viewpager.


    2. 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&nbsp, 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

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.