SimpleOnGestureListener代码在Android 2.2中无效

Jus*_*tin 6 android swipe onfling gesturedetector

我编写了一些代码来实现Gallery小部件的垂直滑动.它在Android 1.5和1.6中运行良好但在Android 2.2中不起作用(我还没有尝试使用2.1).

public class SwipeUpDetector extends SimpleOnGestureListener
implements OnTouchListener
{
       private GestureDetector m_detector;

       public SwipeUpDetector()
       {
               m_detector = new GestureDetector(m_context, this);
       }

       @Override
       public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
       {
               if (Math.abs(e1.getX() - e2.getX()) < s_swipeMaxOffPath &&
                       e1.getY() - e2.getY() >= s_swipeMinDistance &&
                       Math.abs(velocityY) >= s_swipeMinVelocity)
               {
                       int pos = m_gallery.pointToPosition((int)e1.getX(), (int)e2.getY());
                       startAnimation(pos);

                       return true;
               }

               return false;
       }

       @Override
       public boolean onTouch(View v, MotionEvent event)
       {
               return m_detector == null ? false : m_detector.onTouchEvent(event);
       }
}
Run Code Online (Sandbox Code Playgroud)

为了能够让我的画廊检测到onFling我有以下内容:

   m_gallery.setOnTouchListener(new SwipeUpDetector());
Run Code Online (Sandbox Code Playgroud)

在Android 1.5和1.6中,这非常有效.在Android 2.2中,永远不会调用onFling().在浏览Google和StackOverflow时,我发现一个可能的解决方案是实现onDown()并返回true.

但是,我也在听单击,并在此库中设置了上下文菜单监听器.当我实现onDown()并返回true时,我确实让滑动工作.但是,当我这样做时,上下文菜单不会显示长按,单击也不起作用...单击图库中的项目会导致图库跳转,我没有收到任何反馈单击库中的项目.它只是立即使该项目成为所选项目并将其移动到中心.

我查看了1.6,2.1和2.2之间的API差异报告,并没有看到可能导致其破坏的重要性.

我究竟做错了什么?

编辑:

知道图库嵌套在几个布局中可能也会有所帮助,如下所示(这不是一个完整的布局......它只是为了显示这个图库所在的层次结构):

 <ScrollView>
      <LinearLayout>
           <RelativeLayout> <!-- This relative layout is a custom one that I subclassed -->
                <Gallery />
           </RelativeLayout>
      </LinearLayout>
 </ScrollView>
Run Code Online (Sandbox Code Playgroud)

编辑#2:

以下是请求的布局......为了可重用性,有两个布局.这是第一个,它是主要活动的布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myns="http://com.magouyaware/appswipe"
    android:id="@+id/main_layout_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal"
    android:scrollbarAlwaysDrawVerticalTrack="false"
>
    <LinearLayout 
        android:id="@+id/appdocks_layout_id"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:gravity="center"
        android:background="@null"
    >
        <com.magouyaware.appswipe.TitledGallery
            android:id="@+id/running_gallery_layout_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            myns:gallery_title="@string/running_title"
        />

        <com.magouyaware.appswipe.TitledGallery
            android:id="@+id/recent_gallery_layout_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            myns:gallery_title="@string/recent_title"
        />

        <com.magouyaware.appswipe.TitledGallery
            android:id="@+id/favs_gallery_layout_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            myns:gallery_title="@string/favs_title"
        />

        <com.magouyaware.appswipe.TitledGallery
            android:id="@+id/service_gallery_layout_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            myns:gallery_title="@string/service_title"
        />

        <com.magouyaware.appswipe.TitledGallery
            android:id="@+id/process_gallery_layout_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            myns:gallery_title="@string/process_title"
        />

        <include 
            android:id="@+id/indeterminate_progress_layout_id" 
            layout="@layout/indeterminate_progress_layout" 
        />
    </LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

这里是com.magouyaware.appswipe.TitledGallery的布局文件......这只是一个RelativeLayout子类,用于将多个视图作为代码中的单个项控制并具有可重用性:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titled_gallery_main_layout_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    android:background="@null"
>
    <LinearLayout
        android:id="@+id/titled_gallery_expansion_layout_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusable="true"
        android:clickable="true"
        android:gravity="center_vertical"
    >
        <ImageView
            android:id="@+id/titled_gallery_expansion_image_id"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:duplicateParentState="true"
            android:clickable="false"
        />

        <TextView
            style="@style/TitleText"
            android:id="@+id/titled_gallery_title_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:paddingLeft="1sp"
            android:paddingRight="10sp"
            android:textColor="@drawable/titled_gallery_text_color_selector"
            android:duplicateParentState="true"
            android:clickable="false"
        />
    </LinearLayout>

    <Gallery
        android:id="@+id/titled_gallery_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/titled_gallery_expansion_layout_id"
        android:layout_alignWithParentIfMissing="true"
        android:spacing="5sp"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:unselectedAlpha=".5"
        android:focusable="false"
    />

    <TextView 
        style="@style/SubTitleText"
        android:id="@+id/titled_gallery_current_text_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/titled_gallery_id"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_horizontal"
    />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Ido*_*lon 1

通过在SimpleOnGestureListener 的实现中实现onSingleTapConfirmedonDoubleTaponLongPress (同时从onDown返回true ) ,我还能够接收单击/双击和长按。

关于为什么我们应该覆盖onDown方法。我认为这与问题#8233有关。据报道一年前针对2.1版本。由于到目前为止只有 10 人加星,我猜它不会在不久的将来得到修复。

更新

事实证明,该问题是由ScrollView和的组合Gallery和使用引起的OnTouchListenerGallery它本身实现OnGestureListener并封装了当我们设置OnTouchListenerGestureDetector时禁用的功能,有时会导致奇怪的画廊行为。另一方面,如果我们只是对 Gallery 组件进行子类化并在其 onLongPress/onFling 方法中执行长按/滑动检测,则父 ScrollView 将拦截垂直移动事件,从而防止 onFling 调用此类事件。解决方案是覆盖并调用画廊父级。 Gallery.dispatchTouchEventrequestDisallowInterceptTouchEvent(true)

总结一下:如果您想检测 Gallery 的滑动(长按、双击等)(并可能将其放置在 ScrollView 内),请使用下面提供的自定义组件,而不是 GestureDetector/OnTouchListener。

public class FlingGallery extends android.widget.Gallery implements OnDoubleTapListener {

  private static final int SWIPE_MIN_VELOCITY = 30;   // 30dp, set to the desired value

  private static final int SWIPE_MIN_DISTANCE = 50;   // 50dp, set to the desired value

  private static final int SWIPE_MAX_OFF_PATH = 40;   // 40dp, set to the desired value

  private final float mSwipeMinDistance;

  private final float mSwipeMaxOffPath;

  private final float mSwipeMinVelocity;

  public FlingGallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    float density = context.getResources().getDisplayMetrics().density;
    this.mSwipeMinDistance = density * SWIPE_MIN_DISTANCE;
    this.mSwipeMaxOffPath = density * SWIPE_MAX_OFF_PATH;
    this.mSwipeMinVelocity = density * SWIPE_MIN_VELOCITY;
  }

  public FlingGallery(Context context, AttributeSet attrs) {
    this(context, attrs, android.R.attr.galleryStyle);
  }

  public FlingGallery(Context context) {
    this(context, null);
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
    final ViewParent parent;
    if (ev.getAction() == MotionEvent.ACTION_MOVE && (parent = getParent()) != null) {
      parent.requestDisallowInterceptTouchEvent(true);  // this will be passed up to the root view, i.e. ScrollView in our case
    }
    return super.dispatchTouchEvent(ev);
  }

  @Override
  public boolean onDoubleTap(MotionEvent e) {
  // Your double-tap handler...
    return true;
  }

  @Override
  public boolean onDoubleTapEvent(MotionEvent e) {
    return false;
  }

  @Override
  public boolean onSingleTapConfirmed(MotionEvent e) {
  // Your single-tap handler...
    return true;
  }    

  @Override
  public void onLongPress(MotionEvent event) {
  // Your long-press handler...
    super.onLongPress(event);
  }

  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    if (e1 == null) {
      return super.onFling(e1, e2, velocityX, velocityY);
    }
    float dx = e2.getX() - e1.getX();
    float dy = e2.getY() - e1.getY();
    if (abs(dx) < mSwipeMaxOffPath && abs(velocityY) > mSwipeMinVelocity && abs(dy) > mSwipeMinDistance) {
      if (dy > 0) {
        // Your from-top-to-bottom handler...
      } else {
        // Your from-bottom-to-top handler...
      }
    } else if (abs(dy) < mSwipeMaxOffPath && abs(velocityX) > mSwipeMinVelocity && abs(dx) > mSwipeMinDistance) {
      if (dx > 0) {
        // Your from-left-to-right handler...
      } else {
        // Your from-right-to-left handler...
      }
    }
    return super.onFling(e1, e2, velocityX, velocityY);
  }
}
Run Code Online (Sandbox Code Playgroud)