War*_*lax 6 user-interface android touch scrollview android-gallery
我有一个带适配器的Gallery,它为ScrollViews提供子视图.我需要确保按预期正确处理触摸事件:
在不覆盖任何方法的情况下,滚动视图是滚动的唯一内容 - 图库永远不会滚动.
所以我理解我需要在库中使用onInterceptTouchEvent(...)来决定接管一系列的MotionEvents,但我不确定如何检查触摸是否是水平或垂直的.
War*_*lax 19
好的,经过一些重大的摆弄和logcat黑客攻击,这是解决方案:
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3547 次 |
| 最近记录: |