在滚动型的父母没有检测横向轻扫横向、父母

2023-09-05 04:34:04 作者:散落一地的泪

可能重复:   手势检测和滚动型问题

编辑:的问题与全code问here.

question with full code asked here.

我有一个孩子的布局。我定了个手势监听器来检测水平轻扫上的布局。当布局是刷卡正确检测到的LinearLayout,但是当它是一个滚动型,它不是。我猜手势的滚动型第一次检测,并不会传播到其祖先,但我不知道如何解决它。

I've got a layout with a child. I set a gesture listener to detect horizontal swipe on the layout. When the layout is a LinearLayout the swipe is properly detected, but when it's a ScrollView, it's not. I guess the gesture is first detected by the ScrollView and is not propagated to its ascendants, but I don't know how to solve it.

下面是我的布局:

<?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>

我设置下面的听众给我的布局:

I set the following listener to my layout:

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;
    }
}

任何人都可以帮忙吗?

Anybody can help?

感谢

推荐答案

我不得不添加

@Override
public boolean dispatchTouchEvent(MotionEvent ev){
    super.dispatchTouchEvent(ev);    
    return productGestureDetector.onTouchEvent(ev); 
}