问题描述
我有一个带有适配器的图库,该适配器为其提供ScrollViews作为其子视图.我需要确保正确并按预期处理了触摸事件:
I have a Gallery with an adapter which supplies it ScrollViews as its child views.I need to make sure that the touch events are handled correctly and as expected:
- 当用户水平滚动时,图库水平滚动.
- 当用户垂直滚动时,滚动视图垂直滚动.
- 两个滚动都不应该在同一手势上发生(用户必须抬起手指才能滚动另一个视图).
- 一切都必须顺畅滚动.
在不覆盖任何方法的情况下,滚动视图是唯一可滚动的内容-画廊永远不会滚动.
Without overriding any methods, the scroll view is the only thing that scrolls - the gallery never scrolls.
所以我知道我需要在图库中使用onInterceptTouchEvent(...)来决定接管一系列MotionEvent,但是我不确定如何检查触摸的本质是水平还是垂直.
So I understand I need to use onInterceptTouchEvent(...) in the gallery to decide to take over a certain series of MotionEvents but I am unsure how to check if the touch is horizontal or vertical in nature.
推荐答案
好的,经过一些重大摆弄和logcat黑客攻击之后,以下是解决方法:
OK, after some major fiddling and logcat hacking, here's the solution:
public class SwipeInterceptingGallery extends Gallery {
private float mInitialX;
private float mInitialY;
private boolean mNeedToRebase;
private boolean mIgnore;
public SwipeInterceptingGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SwipeInterceptingGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SwipeInterceptingGallery(Context context) {
super(context);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
if (mNeedToRebase) {
mNeedToRebase = false;
distanceX = 0;
}
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN: {
mIgnore = false;
mNeedToRebase = true;
mInitialX = e.getX();
mInitialY = e.getY();
return false;
}
case MotionEvent.ACTION_MOVE: {
if (!mIgnore) {
float deltaX = Math.abs(e.getX() - mInitialX);
float deltaY = Math.abs(e.getY() - mInitialY);
mIgnore = deltaX < deltaY;
return !mIgnore;
}
return false;
}
default: {
return super.onInterceptTouchEvent(e);
}
}
}
}
这篇关于Gallery中的ScrollView,两者均独立滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!