Created
September 5, 2013 08:03
-
-
Save laaptu/6447263 to your computer and use it in GitHub Desktop.
Android View Pager or Horizontal Scroll View/List inside a vertical ScrollView
http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.widget.ScrollView; | |
public class VerticalScrollView extends ScrollView { | |
public VerticalScrollView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
public VerticalScrollView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public VerticalScrollView(Context context) { | |
super(context); | |
} | |
private float xDistance, yDistance, lastX, lastY; | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent ev) { | |
switch (ev.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
xDistance = yDistance = 0f; | |
lastX = ev.getX(); | |
lastY = ev.getY(); | |
break; | |
case MotionEvent.ACTION_MOVE: | |
final float curX = ev.getX(); | |
final float curY = ev.getY(); | |
xDistance += Math.abs(curX - lastX); | |
yDistance += Math.abs(curY - lastY); | |
lastX = curX; | |
lastY = curY; | |
if (xDistance > yDistance) | |
return false; | |
} | |
return super.onInterceptTouchEvent(ev); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment