整个屏幕的onTouchListener

| 我的屏幕上装满了按钮,但是希望onTouch方法使用整个屏幕的坐标。我首先尝试将RelativeLayout与onTouchListener结合使用,但从未设法使其与监听器“连接”(即,触摸屏幕时什么也没发生),我还尝试将ImageView放在屏幕顶部,然后使它查看不可见。 最后一种方法对onClicks给出了正确的响应,但是我从未设法使其不可见。 如果这是最好的解决方案,我对此表示高度怀疑,那么如何在不丢失ImageView的onTouchListener的情况下使其完全不可见(我已经尝试了白色backgroundColor和setAlpha(0))。 我可以以某种方式使onTouchListener使用全局坐标对整个屏幕做出反应,而屏幕正在显示(和更改)多个按钮(最好没有可见的imageview)? 如果您不明白我的要求,请随时投诉。我将尝试根据需要填补空白。 编辑: 我现在已经通过使用常规的onTouch方法设法解决了该问题。我在使ACTION_DOWN和ACTION_MOVE都激活按钮时遇到了几个问题,但最终使它起作用了。对于其他阅读本文的人:可以使用onInterceptTouchEvent(但是我从不知道如何获取屏幕坐标而不是视图坐标)。     
已邀请:
抱歉,如果我错了,但我相信我也遇到了类似的问题。我想要一个显示图片的标题屏幕,并在图片上写上“单击以继续”或类似内容。我四处乱逛,发现您可以使布局可点击。
    android:focusable=\"true\" 
    android:id=\"@+id/titlescreenframe\">
在我的xml文件中用于布局。背景图片只是在background属性中(我知道您没有使用图片) 无论如何,回到我的活动中
    private FrameLayout fl;
    ...
    fl = (FrameLayout)findViewById(R.id.titlescreenframe);
    fl.setOnClickListener(this);
然后,我使用switch语句来处理该内容以及下一个布局上的按钮。如果需要,请在这里:使用switch语句处理按钮单击 似乎这也应该与其他布局一起使用,并且我在主布局上几乎没有任何视图。 (除非布局本身算作一个?) 哈!刚刚意识到您说您找到了解决方案。愚蠢的时机。我会适时发布这样的文章,以帮助他人,为每个人编写快乐代码。 :)     
您是否在布局上尝试过onInterceptTouchEvent?     
我使用了dispatchTouchEvent来解决类似的问题。您可以认为它是
onTouchEvent
的直接替代品,只是它始终向您发送一个事件,即使它在现有视图上也是如此。 如果正在处理事件,则返回true,否则,请确保调用
super.dispatchTouchEvent()
并返回其结果。 至于获取屏幕坐标-只需调用MotionEvent的
getRawX()
getRawY()
而不是
getX()
getY()
。这些是绝对的屏幕坐标,包括操作栏和全部。如果要交叉引用带有视图的视图,则getLocationOnScreen可能是最简单的解决方案。     
首先将事件返回触摸到子视图。如果为它们定义onClick或onTouch侦听器,则parnt视图(例如片段)将不会收到任何触摸侦听器。因此,如果要在这种情况下为片段定义滑动监听器,则必须在新类中实现它:
    package com.neganet.QRelations.fragments;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

public class SwipeListenerFragment extends FrameLayout {
    private float x1,x2;
    static final int MIN_DISTANCE=150;
    private onSwipeEventDetected mSwipeDetectedListener;


    public SwipeListenerFragment(Context context) {
        super(context);
    }

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

    public SwipeListenerFragment(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean result=false;
        switch(ev.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                x1 = ev.getX();
                break;
            case MotionEvent.ACTION_UP:
                x2 = ev.getX();
                float deltaX = x2 - x1;
                if (Math.abs(deltaX) > MIN_DISTANCE)
                {
                    if(deltaX<0)
                    {
                        result=true;
                        if(mSwipeDetectedListener!=null)
                            mSwipeDetectedListener.swipeLeftDetected();

                    }else if(deltaX>0){
                        result=true;
                        if(mSwipeDetectedListener!=null)
                            mSwipeDetectedListener.swipeRightDetected();
                    }
                }
                break;
        }
        return result;
    }

    public interface onSwipeEventDetected
    {
        public void swipeLeftDetected();
        public void swipeRightDetected();

    }

    public void registerToSwipeEvents(onSwipeEventDetected listener)
    {
        this.mSwipeDetectedListener=listener;
    }
}
您可以完全像这样为其他类型的布局制作工具。此类可以检测到左右滑动,特别是它在检测到后返回onInterceptTouchEvent true。这很重要,因为如果某些时候我们不这样做,子视图可能会接收到事件,并且Swipe(用于片段)和onClick(用于子视图)都会运行,并引起一些问题。 制作完此类后,您必须更改片段xml文件:
    <com.neganet.QRelations.fragments.SwipeListenerFragment xmlns:android=\"http://schemas.android.com/apk/res/android\"
    xmlns:tools=\"http://schemas.android.com/tools\" android:layout_width=\"match_parent\"
    android:id=\"@+id/main_list_layout\"
    android:clickable=\"true\"
    android:focusable=\"true\"
    android:focusableInTouchMode=\"true\"
    android:layout_height=\"match_parent\" tools:context=\"com.neganet.QRelations.fragments.mainList\"
    android:background=\"@color/main_frag_back\">

    <!-- TODO: Update blank fragment layout -->
    <android.support.v7.widget.RecyclerView
        android:id=\"@+id/farazList\"
        android:scrollbars=\"horizontal\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"match_parent\"
        android:layout_gravity=\"left|center_vertical\" />
</com.neganet.QRelations.fragments.SwipeListenerFragment>
您会看到begin标记是我们制作的类。现在在片段类中:
            View view=inflater.inflate(R.layout.fragment_main_list, container, false);
        SwipeListenerFragment tdView=(SwipeListenerFragment) view;
        tdView.registerToSwipeEvents(this);


and then Implement SwipeListenerFragment.onSwipeEventDetected in it:

        @Override
    public void swipeLeftDetected() {
        Toast.makeText(getActivity(), \"left\", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void swipeRightDetected() {
        Toast.makeText(getActivity(), \"right\", Toast.LENGTH_SHORT).show();
    }
这有点复杂,但是效果完美:)     

要回复问题请先登录注册