Skip to main content
added 281 characters in body
Source Link
Suragch
  • 516.3k
  • 340
  • 1.4k
  • 1.4k
public class ScrollViewForNesting extends ScrollView {  private final int DIRECTION_VERTICAL = 0;  private final int DIRECTION_HORIZONTAL = 1;  private final int DIRECTION_NO_VALUE = -1;    private final int mTouchSlop;  private int mGestureDirection;    private float mDistanceX;  private float mDistanceY;  private float mLastX;  private float mLastY;  public ScrollViewForNesting(Context context, AttributeSet attrs,   int defStyle) {   super(context, attrs, defStyle);     final ViewConfiguration configuration = ViewConfiguration.get(context);   mTouchSlop = configuration.getScaledTouchSlop();  }  public ScrollViewForNesting(Context context, AttributeSet attrs) {   this(context, attrs,0);  }  public ScrollViewForNesting(Context context) {   this(context,null);  }    @Override  public boolean onInterceptTouchEvent(MotionEvent ev) {   switch (ev.getAction()) {   case MotionEvent.ACTION_DOWN:   mDistanceY = mDistanceX = 0f;   mLastX = ev.getX();   mLastY = ev.getY();   mGestureDirection = DIRECTION_NO_VALUE;   break;   case MotionEvent.ACTION_MOVE:   final float curX = ev.getX();   final float curY = ev.getY();   mDistanceX += Math.abs(curX - mLastX);   mDistanceY += Math.abs(curY - mLastY);   mLastX = curX;   mLastY = curY;   break;   }     return super.onInterceptTouchEvent(ev) && shouldIntercept();  }      private boolean shouldIntercept(){   if((mDistanceY > mTouchSlop || mDistanceX > mTouchSlop) && mGestureDirection == DIRECTION_NO_VALUE){   if(Math.abs(mDistanceY) > Math.abs(mDistanceX)){   mGestureDirection = DIRECTION_VERTICAL;   }   else{   mGestureDirection = DIRECTION_HORIZONTAL;   }   }     if(mGestureDirection == DIRECTION_VERTICAL){   return true;   }   else{   return false; } } } 

}

public class ScrollViewForNesting extends ScrollView{ private final int DIRECTION_VERTICAL = 0; private final int DIRECTION_HORIZONTAL = 1; private final int DIRECTION_NO_VALUE = -1; private final int mTouchSlop; private int mGestureDirection; private float mDistanceX; private float mDistanceY; private float mLastX; private float mLastY; public ScrollViewForNesting(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); final ViewConfiguration configuration = ViewConfiguration.get(context); mTouchSlop = configuration.getScaledTouchSlop(); } public ScrollViewForNesting(Context context, AttributeSet attrs) { this(context, attrs,0); } public ScrollViewForNesting(Context context) { this(context,null); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mDistanceY = mDistanceX = 0f; mLastX = ev.getX(); mLastY = ev.getY(); mGestureDirection = DIRECTION_NO_VALUE; break; case MotionEvent.ACTION_MOVE: final float curX = ev.getX(); final float curY = ev.getY(); mDistanceX += Math.abs(curX - mLastX); mDistanceY += Math.abs(curY - mLastY); mLastX = curX; mLastY = curY; break; } return super.onInterceptTouchEvent(ev) && shouldIntercept(); } private boolean shouldIntercept(){ if((mDistanceY > mTouchSlop || mDistanceX > mTouchSlop) && mGestureDirection == DIRECTION_NO_VALUE){ if(Math.abs(mDistanceY) > Math.abs(mDistanceX)){ mGestureDirection = DIRECTION_VERTICAL; } else{ mGestureDirection = DIRECTION_HORIZONTAL; } } if(mGestureDirection == DIRECTION_VERTICAL){ return true; } else{ return false; } } 

}

public class ScrollViewForNesting extends ScrollView {  private final int DIRECTION_VERTICAL = 0;  private final int DIRECTION_HORIZONTAL = 1;  private final int DIRECTION_NO_VALUE = -1;    private final int mTouchSlop;  private int mGestureDirection;    private float mDistanceX;  private float mDistanceY;  private float mLastX;  private float mLastY;  public ScrollViewForNesting(Context context, AttributeSet attrs,   int defStyle) {   super(context, attrs, defStyle);     final ViewConfiguration configuration = ViewConfiguration.get(context);   mTouchSlop = configuration.getScaledTouchSlop();  }  public ScrollViewForNesting(Context context, AttributeSet attrs) {   this(context, attrs,0);  }  public ScrollViewForNesting(Context context) {   this(context,null);  }    @Override  public boolean onInterceptTouchEvent(MotionEvent ev) {   switch (ev.getAction()) {   case MotionEvent.ACTION_DOWN:   mDistanceY = mDistanceX = 0f;   mLastX = ev.getX();   mLastY = ev.getY();   mGestureDirection = DIRECTION_NO_VALUE;   break;   case MotionEvent.ACTION_MOVE:   final float curX = ev.getX();   final float curY = ev.getY();   mDistanceX += Math.abs(curX - mLastX);   mDistanceY += Math.abs(curY - mLastY);   mLastX = curX;   mLastY = curY;   break;   }     return super.onInterceptTouchEvent(ev) && shouldIntercept();  }      private boolean shouldIntercept(){   if((mDistanceY > mTouchSlop || mDistanceX > mTouchSlop) && mGestureDirection == DIRECTION_NO_VALUE){   if(Math.abs(mDistanceY) > Math.abs(mDistanceX)){   mGestureDirection = DIRECTION_VERTICAL;   }   else{   mGestureDirection = DIRECTION_HORIZONTAL;   }   }     if(mGestureDirection == DIRECTION_VERTICAL){   return true;   }   else{   return false; } } } 
deleted 341 characters in body
Source Link
snapix
  • 334
  • 2
  • 8
public class ScrollViewForNesting extends ScrollView{ private final int DIRECTION_VERTICAL = 0; private final int DIRECTION_HORIZONTAL = 1; private final int DIRECTION_NO_VALUE = -1; private final GestureDetectorint mGestureDetector;mTouchSlop; private final int mTouchSlop;mGestureDirection;  private intfloat mGestureDirection;mDistanceX; private float mDistanceY; private float mLastX; private float mLastY; public ScrollViewForNesting(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle);   mGestureDetector = new GestureDetector(context, new YScrollDetector());  setFadingEdgeLength(0);  final ViewConfiguration configuration = ViewConfiguration.get(context); mTouchSlop = configuration.getScaledTouchSlop(); } public ScrollViewForNesting(Context context, AttributeSet attrs) { this(context, attrs,0); } public ScrollViewForNesting(Context context) { this(context,null); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) {  ifswitch (ev.getActionMaskedgetAction()) =={  case MotionEvent.ACTION_DOWN || : ev.getActionMasked()mDistanceY === MotionEvent.ACTION_UPmDistanceX ||= 0f; mLastX = ev.getActionMaskedgetX();  == MotionEvent mLastY = ev.ACTION_CANCELgetY(); {  mGestureDirection = DIRECTION_NO_VALUE; }  break; return super.onInterceptTouchEvent(ev) && mGestureDetector case MotionEvent.onTouchEvent(ev); } @OverrideACTION_MOVE: public boolean onTouchEvent(MotionEvent ev) {  if( final float curX = ev.getActionMaskedgetX();  == MotionEvent.ACTION_DOWN ||    final float curY = ev.getActionMaskedgetY();  == MotionEvent mDistanceX += Math.ACTION_UPabs(curX ||- mLastX); evmDistanceY += Math.getActionMaskedabs()curY ==- MotionEvent.ACTION_CANCELmLastY); {  mGestureDirectionmLastX = DIRECTION_NO_VALUE;curX;   }     return super.onTouchEvent(ev); } // ReturnmLastY false= ifcurY;  we're scrolling in the x direction  class YScrollDetector extends SimpleOnGestureListener {break; @Override} public boolean onScroll(MotionEvent e1, MotionEvent   e2, float distanceX, floatreturn distanceYsuper.onInterceptTouchEvent(ev) {&& shouldIntercept(); }   private boolean shouldIntercept(){  if((distanceYmDistanceY > mTouchSlop || distanceXmDistanceX > mTouchSlop) && mGestureDirection == DIRECTION_NO_VALUE){   if(Math.abs(distanceYmDistanceY) > Math.abs(distanceXmDistanceX)){   mGestureDirection = DIRECTION_VERTICAL;   }   else{   mGestureDirection = DIRECTION_HORIZONTAL;   }   }     if(mGestureDirection == DIRECTION_VERTICAL){   return true;   }   else{   return false;   } } } 

}

public class ScrollViewForNesting extends ScrollView{ private final int DIRECTION_VERTICAL = 0; private final int DIRECTION_HORIZONTAL = 1; private final int DIRECTION_NO_VALUE = -1; private final GestureDetector mGestureDetector; private final int mTouchSlop; private int mGestureDirection; public ScrollViewForNesting(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle);   mGestureDetector = new GestureDetector(context, new YScrollDetector());  setFadingEdgeLength(0);  final ViewConfiguration configuration = ViewConfiguration.get(context); mTouchSlop = configuration.getScaledTouchSlop(); } public ScrollViewForNesting(Context context, AttributeSet attrs) { this(context, attrs,0); } public ScrollViewForNesting(Context context) { this(context,null); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if(ev.getActionMasked() == MotionEvent.ACTION_DOWN ||  ev.getActionMasked() == MotionEvent.ACTION_UP || ev.getActionMasked() == MotionEvent.ACTION_CANCEL) {  mGestureDirection = DIRECTION_NO_VALUE; }  return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent ev) {  if(ev.getActionMasked() == MotionEvent.ACTION_DOWN ||    ev.getActionMasked() == MotionEvent.ACTION_UP || ev.getActionMasked() == MotionEvent.ACTION_CANCEL) {  mGestureDirection = DIRECTION_NO_VALUE;   }     return super.onTouchEvent(ev); } // Return false if we're scrolling in the x direction  class YScrollDetector extends SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if((distanceY > mTouchSlop || distanceX > mTouchSlop) && mGestureDirection == DIRECTION_NO_VALUE){   if(Math.abs(distanceY) > Math.abs(distanceX)){   mGestureDirection = DIRECTION_VERTICAL;   }   else{   mGestureDirection = DIRECTION_HORIZONTAL;   }   }     if(mGestureDirection == DIRECTION_VERTICAL){   return true;   }   else{   return false;   } } } 
public class ScrollViewForNesting extends ScrollView{ private final int DIRECTION_VERTICAL = 0; private final int DIRECTION_HORIZONTAL = 1; private final int DIRECTION_NO_VALUE = -1; private final int mTouchSlop; private int mGestureDirection;  private float mDistanceX; private float mDistanceY; private float mLastX; private float mLastY; public ScrollViewForNesting(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); final ViewConfiguration configuration = ViewConfiguration.get(context); mTouchSlop = configuration.getScaledTouchSlop(); } public ScrollViewForNesting(Context context, AttributeSet attrs) { this(context, attrs,0); } public ScrollViewForNesting(Context context) { this(context,null); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) {  switch (ev.getAction()) {  case MotionEvent.ACTION_DOWN: mDistanceY = mDistanceX = 0f; mLastX = ev.getX();   mLastY = ev.getY(); mGestureDirection = DIRECTION_NO_VALUE; break;  case MotionEvent.ACTION_MOVE:  final float curX = ev.getX();  final float curY = ev.getY();   mDistanceX += Math.abs(curX - mLastX); mDistanceY += Math.abs(curY - mLastY); mLastX = curX; mLastY = curY;  break; }    return super.onInterceptTouchEvent(ev) && shouldIntercept(); }   private boolean shouldIntercept(){  if((mDistanceY > mTouchSlop || mDistanceX > mTouchSlop) && mGestureDirection == DIRECTION_NO_VALUE){ if(Math.abs(mDistanceY) > Math.abs(mDistanceX)){ mGestureDirection = DIRECTION_VERTICAL; } else{ mGestureDirection = DIRECTION_HORIZONTAL; } } if(mGestureDirection == DIRECTION_VERTICAL){ return true; } else{ return false; } } 

}

Source Link
snapix
  • 334
  • 2
  • 8

It wasn't working well for me. I changed it and now it works smoothly. If anyone interested.

public class ScrollViewForNesting extends ScrollView{ private final int DIRECTION_VERTICAL = 0; private final int DIRECTION_HORIZONTAL = 1; private final int DIRECTION_NO_VALUE = -1; private final GestureDetector mGestureDetector; private final int mTouchSlop; private int mGestureDirection; public ScrollViewForNesting(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mGestureDetector = new GestureDetector(context, new YScrollDetector()); setFadingEdgeLength(0); final ViewConfiguration configuration = ViewConfiguration.get(context); mTouchSlop = configuration.getScaledTouchSlop(); } public ScrollViewForNesting(Context context, AttributeSet attrs) { this(context, attrs,0); } public ScrollViewForNesting(Context context) { this(context,null); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if(ev.getActionMasked() == MotionEvent.ACTION_DOWN || ev.getActionMasked() == MotionEvent.ACTION_UP || ev.getActionMasked() == MotionEvent.ACTION_CANCEL) { mGestureDirection = DIRECTION_NO_VALUE; } return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent ev) { if(ev.getActionMasked() == MotionEvent.ACTION_DOWN || ev.getActionMasked() == MotionEvent.ACTION_UP || ev.getActionMasked() == MotionEvent.ACTION_CANCEL) { mGestureDirection = DIRECTION_NO_VALUE; } return super.onTouchEvent(ev); } // Return false if we're scrolling in the x direction class YScrollDetector extends SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if((distanceY > mTouchSlop || distanceX > mTouchSlop) && mGestureDirection == DIRECTION_NO_VALUE){ if(Math.abs(distanceY) > Math.abs(distanceX)){ mGestureDirection = DIRECTION_VERTICAL; } else{ mGestureDirection = DIRECTION_HORIZONTAL; } } if(mGestureDirection == DIRECTION_VERTICAL){ return true; } else{ return false; } } }