我在这个特定问题上找到的最好的(虽然我不使用图库):ScrollView and Gallery interfering - 虽然没有给出具体答案 . 显然,我的实现不使用Gallery .
Jump down to the next bold part for the interesting part
所以我得到了Fling / Swipe / Flick /你想要在我的应用程序上调用它的一段时间 . 灵感来自几个不同的地方,其中一些是Stack Overflow(Fling gesture detection on grid layout),Code Shogun(http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/)和开发Android(http://developingandroid.blogspot.com/2009/09/implementing-swipe-gesture.html),但我在我的应用程序中没有使用ViewFlipper . 当发生甩尾时,我只需更换标签(在末端缠绕) .
现在,我的一些标签包含ScrollViews . 这些ScrollViews显然会响应向上/向下滚动,以便让您查看其中的所有数据,这并不奇怪 . 问题是看起来这些ScrollViews的“滚动”功能会覆盖我的投掷手势 . 我不能在ScrollView内部滚动(滚动得很好),但它在它们之外完美无缺(在同一个选项卡上,在TableRow等其他视图上) .
我也快速查看了http://blog.velir.com/index.php/2010/11/17/android-snapping-horizontal-scroll/,这提供了一种实现HorizontalScrollView的方法 . 但它仍然通过扩展SimpleOnGestureListener(并覆盖onFling)的类处理手势,这与我的实现相同(这使我相信它不会真正有用) . 来自Google的ScrollView的源代码:http://google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/widget/ScrollView.java&d=3
Is there any way to have my implementation of Swipe and ScrollView working together effortlessly?
我想这就是问题所在 . ScrollView.java还使用一个名为onTouchEvent的方法和onTouchEvent for Activity状态的文档:
“当触摸屏事件未被其下的任何视图处理时调用 . 这对于处理在窗口范围之外发生的触摸事件非常有用,其中没有视图可以接收它 . ”
所以ScrollView会“覆盖”它 - 我该怎么办?有没有办法确保两者都被检查?我的onTouchEvent在ScrollView处理onTouchEvent时没有被击中:
@Override
/** Used for swipe gestures */
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
下面是更一般的源代码,它可能不是很重要 . 我的Tabs类中的gestureDetector及其关联的侦听器:
// Gestures
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
我的手势类是我的Tabs类的嵌套类(它扩展了TabActivity) - 它与你在这个主题上找到的任何其他代码相同:
/** GestureDetector used to swipe between classes */
class MyGestureDetector extends SimpleOnGestureListener {
TabHost tabHost = getTabHost();
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false;
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// my tab code
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// my tab code
return true;
}
} catch (Exception e) {
Log.e("MyGestureDetector onFling", e.toString());
}
return false;
}
}
3 回答
我建议您查看Google I / O 2010应用程序源代码,因为它们的
FlingableTabHost
实现似乎已经解决了这个问题:http://iosched.googlecode.com/svn/trunk/src/com/google/android/apps/iosched/ui/ScheduleActivity.java
我认为关键在于扩展
TabHost
并覆盖其onInterceptTouchEvent
方法 .为了解决我遇到的类似问题,我遇到了这个问题:
从Android Dev site开始,它讨论了在布局文件中的
<android.gesture.GestureOverlayView>
Root中使用此属性 .对于它的 Value ,我发现onFling方法非常不可靠 . 我重写了SimpleGestureDetector中的onScroll方法,并将onInterceptTouchEvent定义为: