Android 安卓:根视图中的onTouchListener(RelativeLayout)不是';行不通

Android 安卓:根视图中的onTouchListener(RelativeLayout)不是';行不通,android,touch,android-relativelayout,ontouchlistener,Android,Touch,Android Relativelayout,Ontouchlistener,我正在开发一个Android应用程序,并试图在我的整个屏幕中捕获OnTouch事件。我所有的活动都会有一个带有两个按钮的标题,然后是一个将要更改的主体 在活动中我现在正在测试主体是一个列表视图,因此活动具有: -屏幕顶部有两个按钮 -ListView在这两个按钮下 我想在整个屏幕中捕获onTouchEvents。我尝试将一个OnTouchListener设置为我的根RelativeLayout,并将clickable=false和focusable=false设置为所有其他视图,但它不起作用:只

我正在开发一个
Android
应用程序,并试图在我的整个屏幕中捕获
OnTouch
事件。我所有的
活动
都会有一个带有两个
按钮的标题
,然后是一个将要更改的主体

活动中
我现在正在测试主体是一个
列表视图
,因此
活动
具有:

-屏幕顶部有两个
按钮

-
ListView
在这两个按钮下

我想在整个屏幕中捕获
onTouchEvents
。我尝试将一个
OnTouchListener
设置为我的根
RelativeLayout
,并将clickable=false和focusable=false设置为所有其他视图,但它不起作用:只有在单击第一个按钮时才会触发onTouch事件

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <include
        layout="@layout/header"
        android:clickable="false"
        android:focusable="false" />

    <ListView
        android:id="@+id/lvLocations"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/header_layout"
        android:clickable="false"
        android:focusable="false" />

</RelativeLayout>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myList"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
正如我所说,只有在单击Home按钮时,我的日志才会显示TOUCH。为什么呢?我做错了什么


谢谢大家!

为什么不为您的
RelativeLayout
创建一个自定义视图,捕捉触摸事件,然后您可以对其执行任何操作

public class CustomRelativeLayout extends RelativeLayout{

    CustomRelativeLayout(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev){
        //handle touch event
        return true;
    }
}
然后,通过在项目名称空间中查找该类,可以在XML中使用该相对布局。当然,您可以添加任何您想要的修改器,但下面是一个示例

<com.example.whatever.your.project.CustomRelativeLayout
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"/>


一旦你抓住了触感,你可以用它做任何你想做的事。您可以将它传递给其他活动,您可以使用它调用其他方法,等等。有关如何使用android文档的自定义视图部分的更多信息

我终于解决了这个问题,为此我重新设计了布局。现在,我的布局中没有标题和列表视图,而是有一个列表视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <include
        layout="@layout/header"
        android:clickable="false"
        android:focusable="false" />

    <ListView
        android:id="@+id/lvLocations"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/header_layout"
        android:clickable="false"
        android:focusable="false" />

</RelativeLayout>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myList"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
现在我将onTouchListener设置为ListView,瞧,我在整个屏幕中接收onTouch事件


我知道这是一个限制性的解决方案,它不能用于复杂的布局,但我可以在我需要的屏幕上使用它。

我刚刚尝试了它,结果是一样的,只有当我触摸我的第一个视图(homeButton)时才会调用onTouchEvent,如果我单击任何其他视图,什么都不会发生。有趣。我认为正在发生的是,因为有多个视图正在捕获触摸-例如listview、标题、按钮-它甚至不可能以您想要的方式显示。我敢打赌,如果您创建自定义视图,在任何视图抓取之前抓取触摸,然后使用控制器类型模式确定发生了什么,您可以使用更复杂的布局来实现这一点。我以前在将地图或网格放入ViewPager时也遇到过类似的问题。只是一个想法。真的很有意思的评论,我从来没有进入过自定义视图,所以我没有想到这一点。似乎是个不错的方法:)
View header = (View)getLayoutInflater().inflate(R.layout.header,null);
mList.addHeaderView(header);