通过视图android传递点击事件

Vel*_*jko 5 android click

我有框架布局:

<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>
Run Code Online (Sandbox Code Playgroud)

第一个布局包含窗口小部件布局,第二个布局是透明的,并且接收onLongClick用于拖动方法.这有效,问题是当我想要与窗口小部件交互时,点击某些内容,点击事件由overlayFrame拍摄.如何将click事件通过overlayFrame传递给widgetView?

编辑:

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

这种行为有解释吗?TNX!

Eug*_*noy 6

就我而言,我向视图的layoutParams添加了标志,该标志应该在以下位置传递触摸事件:

WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
Run Code Online (Sandbox Code Playgroud)

这样视图将忽略所有触摸(例如可见性消失的视图)


cjd*_*jds 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;
    }
Run Code Online (Sandbox Code Playgroud)