【发布时间】:2011-11-30 13:20:11
【问题描述】:
编辑: 询问完整代码的问题here。
我有一个孩子的布局。我设置了一个手势监听器来检测布局上的水平滑动。当布局是 LinearLayout 时,会正确检测到滑动,但当它是 ScrollView 时,则不会。我猜手势首先被 ScrollView 检测到,并没有传播到它的后代,但我不知道如何解决它。
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView android:layout_width="320dp" android:layout_height="30dp"
android:src="@drawable/header"/>
<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content">
<!-- stuff -->
</ScrollView>
</LinearLayout>
我为我的布局设置了以下监听器:
class ProductGestureListener extends SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
final int SWIPE_MIN_DISTANCE = 120;
final int SWIPE_MAX_OFF_PATH = 250;
final int SWIPE_THRESHOLD_VELOCITY = 200;
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// show previous item
} else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// show next item
}
} catch (Exception e) {
}
return false;
}
}
【问题讨论】:
-
这个问题应该提供答案stackoverflow.com/questions/2646028/…
-
我试过了,但仍然没有检测到布局上的滑动。
-
几天前我使用了该链接的答案,它对我有用,你能发布你的布局文件和相关代码吗?
-
我将手势监听器的代码添加到我的布局中。
-
我并不是说将手势检测器附加到布局中,而是发布布局 xml 文件,但我认为我看到了问题,我会在答案中发布
标签: android scrollview gesture