我有一个布局,其中Custom ScrollView下面有Horizontal viewpager和TextView。 如果我使用InterpretedScrollView而不是ScrollView,则TextView的onClick将停止工作。 我需要InterpretedScrollView来允许在ViewPager中进行水平滚动。
<com.package.InterceptedScrollView
<ViewPager
// scrolls horizontally thanks to InterceptedScrollView
/>
...
<TextView
// try scrolling vertically by touching this imageView and it doesn't scroll
onClick="something"/>
</com.package.InterceptedScrollView>
和我的自定义ScrollView
public class InterceptedScrollView extends ScrollView {
GestureDetector mGestureDetector;
private class VerticalScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float x, float y) {
if (Math.abs(y) > Math.abs(x)) {
// scrolling in Y direction essentially. Should scroll
// vertically.
return true;
}
// Not much of scroll in Y. Don't scroll.
return false;
}
}
public InterceptedScrollView(Context ctx, AttributeSet as) {
super(ctx, as);
mGestureDetector = new GestureDetector(ctx, new VerticalScrollDetector());
setFadingEdgeLength(0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return super.onInterceptTouchEvent(event) && mGestureDetector.onTouchEvent(event);
}
}
为什么我的InterceptedScrollview正在点击TextView?我该如何解决?
答案 0 :(得分:1)
我的自定义ScrollView使用xScroll和yScroll的totalSize。 我点击时记录当前的xPoint和yPoint。 在移动时计算它。
public class PageScrollView extends ScrollView {
private float xDistance, yDistance, lastX, lastY;
public PageScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@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);
}
}