在 listView 之前调用 onTouchEvent()

Call onTouchEvent() before listView

在我的 activity 中,我有一个由 ArrayList 填充的 ListView,它实现了 Android MediaPlayerControl。

MusicController 使用默认行为,因此它会在 3000 毫秒后消失,我希望能够在用户触摸屏幕一次时随时显示它。在用户触摸屏幕的那一刻,ListView 中的项目被选中。我只想调用 controller.show() 来代替。不幸的是,下面的代码不起作用。它只是激活在列表视图中选择的任何项目。

我是否需要在我的 activity 中添加某种叠加层?我只想要某种 "super touch listener" 来监听屏幕上任何地方的触摸事件,用 controller.show() 响应。

我已将此添加到我的 MainActivity:

import android.widget.MediaController.MediaPlayerControl;
import android.view.MotionEvent;


    private MusicController controller;

 @Override
public boolean onTouchEvent(MotionEvent event) {
    //the MediaController will hide after 3 seconds - tap the screen to make it appear again
    controller.show();
    return false;
}

编辑:这是在我尝试了 Marcus 的建议之后

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="@+id/wholeScreenLayout">

<ListView
    android:id="@+id/media_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</ListView>

mainActivity.java 片段。添加到 onCreate() 方法

//当用户触摸屏幕时显示控件

    RelativeLayout wholeScreen = (RelativeLayout) findViewById(R.id.wholeScreenLayout);
    wholeScreen.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            controller.show();
            return false;
        }
    });

您可以为您的应用程序布局添加 OnTouchListener。在您的布局文件中,将此添加到您的根布局元素:

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="@+id/wholeScreenLayout"

然后你添加监听器

//Assuming it's a RelativeLayout element
RelativeLayout yourRelativeLayout = (RelativeLayout) findViewById(R.id.wholeScreenLayout);
yourRelativeLayout.setOnTouchListener(new View.OnTouchListener() {  
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {

        //Do your work here

        return true;//always return true to consume event
    }
});

要解决 "ignores subsequent events until 3000ms later.",您需要以某种方式跟踪时间。您可以使用 System.currentTimeMillis(); 获取当前时间,保存该值然后在 onTouch 方法中检查它。