7

我有框架布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp" >

<LinearLayout
    android:id="@+id/widgetView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:gravity="center" >
</LinearLayout>

<LinearLayout
    android:id="@+id/widgetOverlayFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="false" >
</LinearLayout>

第一个布局包含小部件布局,第二个是透明的并接收 onLongClick 用于拖动方法。这有效,问题是当我想与小部件交互时,单击某些东西时,单击事件由 overlayFrame 获取。如何通过overlayFrame将点击事件传递给widgetView?

编辑:

现在我正在尝试将 GestureLister 附加到 overlayFrame LinearLayout,以某种方式查看代表单击和长按的 MotionEvent 之间的区别。我面临的问题是手势监听器中的 OnLongPress 总是被称为无论我做什么,单击或长按。

这种行为有解释吗?天呐!

4

5 回答 5

5

在我的情况下,我在视图的 layoutParams 中添加了标志,它应该在下面传递了一个触摸事件:

WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE

这种方式视图将忽略所有触摸(如可见性消失的视图)

于 2018-12-28T08:45:21.563 回答
0

也许设置覆盖的属性android:focusable="false"或者android:focusableInTouchMode="false"就足够了。

您还可以制作自己的覆盖小部件并覆盖其onInterceptTouchEvent方法以始终返回 false。

public class NotTouchableLinearLayout extends LinearLayout {

    // Override constructors
    // ...

   @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }
}
于 2012-11-21T08:59:27.260 回答
0

GestureListener您应该创建自己的扩展类LinearLayout并覆盖它的onTouchEvent方法,而不是在顶部布局上设置 a 。在那里你可以实现长点击\短点击等的逻辑。

点击事件将首先被发送到小部件布局,并且只有当它不处理它们(因此它是onTouchEventreturn false)时,您才会将它们放在顶部布局中。

编辑:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" >

<LinearLayout
android:id="@+id/widgetView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center" >
</LinearLayout>

<com.example.touchtesting.MyLinearLayout
android:id="@+id/widgetOverlayFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" >
</com.example.touchtesting.MyLinearLayout>

</FrameLayout>

更改com.example.touchtesting为您的包的名称。这是课程:

        public class MyLinearLayout extends LinearLayout{

        public MyLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        private long startClick = 0;

        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startClick = ev.getEventTime();
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    if (ev.getEventTime() - startClick < 500) {
                        Log.i("example","short click");
                    }
                    else {
                        Log.i("example","long click");
                    }
                    break;
            }

            return true;
        }

    }
于 2012-11-27T22:25:39.527 回答
0

您可以通过这种方式覆盖 onTouchListener,而不是使用 GestureListener。

这将在计时器用完时调用 longPress,如果在两者之间出现 up ,它将取消 LongPress

   CountDownTimer c;
    long time=5000;
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                c= new CountDownTimer(5000, 1000) {
                          public void onTick(long millisUntilFinished) {
                                  time=millisUntilFinished;
                           }

                          public void onFinish() {
                                 Log.i("","LONG PRESS");
                           }}.start();

            break;
            case MotionEvent.ACTION_UP:
                if (time>0) {
                    Log.i("example","short click");
                    c.cancel();
                }
            break;
        }

        return true;
    }
于 2012-11-30T10:33:29.217 回答
0

要获得长点击以通过 widgetViewLinearLayout添加android:longClickable="true"。(对于普通点击添加android:clickable="true"

所以 widgetView 变成:

<LinearLayout
    android:id="@+id/widgetView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:gravity="center" 
    android:longClickable="true">
</LinearLayout>
于 2014-07-09T19:24:25.463 回答