本文介绍了涉及小部件时,如何检测挥动手势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在尝试检测我的应用中的边缘滑动(以显示菜单),并且搜索stackoverflow似乎表明,检测滑动的方法是从掠过检测开始。

Background: I'm trying to detect edge swipes in my app (to bring up a menu), and searching stackoverflow seems to indicate that the way to detect swipes is to start with fling detection.

我正在尝试检测我的应用中的逃逸事件。我可以覆盖 dispatchTouchEvent() onTouchEvent()并将事件传递给手势侦听器,一切按您的意愿进行

I'm trying to detect a fling event in my app. I can override dispatchTouchEvent() or onTouchEvent() and pass the event to a gesture listener and everything works as you would expect.

但是,我的应用程序中包含按钮小部件,但我既无法检测到掠夺也无法使用小部件。

However, my app has button widgets in it and I can't both detect the fling and use the widgets.

如果我从 onTouchEvent()调用手势检测器,则如果手势从使用该事件的小部件开始,则不会检测到挥动。如果我从 dispatchTouchEvent()调用手势检测器,则小部件将无法获取所需的事件。

If I call the gesture detector from onTouchEvent(), flings are not detected if the gesture starts over a widget that consumes the event. If I call the gesture detector from dispatchTouchEvent(), the widgets don't get the events they need.

我也尝试附加到容器的 onTouchEvent(),但是结果是相同的。

I've also tried attaching to the container's onTouchEvent() but the result was the same.

最后,我我们已经查看了ViewPager的源代码以了解它们的工作方式,以及它们的工作是重写 onInterceptTouchEvent()。我可以看到它的工作方式和原因,但我希望有一个更简单的解决方案,不需要我实现自己的容器小部件。

Finally, I've looked at the source code to ViewPager to see how they do it, and what they do is override onInterceptTouchEvent(). I can see how and why this works, but I was hoping there was a simpler solution that didn't require me to implement my own container widget.

源代码:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;

public class Fling extends Activity {
    private static final String TAG = "FlingTest";

    protected GestureDetector gestureDetector;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gestureDetector = new GestureDetector(this, listener);
    }

    /*
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
            return true;
        return super.dispatchTouchEvent(event);
    }
    */

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final SimpleOnGestureListener listener =
      new SimpleOnGestureListener() {
        public boolean onDown(MotionEvent e1) { return true; }
        public boolean onFling(MotionEvent e1, MotionEvent e2, float vx, float vy) {
            Log.d(TAG, "Fling: " + vx + "," + vy);
            return true;
        }
      };
}

layout.xml文件,尽管几乎任何布局都会显示以下问题:

The layout.xml file, although pretty much any layout will show these issues:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/buttonPane"
    android:background="#446"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      >
    <Button
        android:text="btn"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        />
    <Button
        android:text="btn"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        />
  </LinearLayout>

  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      >
    <Button
        android:text="btn"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        />
    <Button
        android:text="btn"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        />
  </LinearLayout>
</LinearLayout>


推荐答案

我找到了一个似乎可行的答案。我将dispatchTouchEvent()更改为两者并调用手势检测器,然后将事件沿链条传递。

I've found one answer that seems to work. I change dispatchTouchEvent() to both call the gesture detector and pass the event up the chain.

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    gestureDetector.onTouchEvent(event);
    return super.dispatchTouchEvent(event);
}

这不是很漂亮,我不确定它是否健壮,但似乎

It's not pretty, and I'm not sure it's robust, but it seems to work.

仍然希望有更好的答案,但我想我会尝试这种方法。

Still hoping for a better answer, but I think I'll give this method a try.

这篇关于涉及小部件时,如何检测挥动手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:04