简体   繁体   English

在listView之前调用onTouchEvent()

[英]Call onTouchEvent() before listView

In my activity I have a ListView that is populated by an ArrayList, and it implements the Android MediaPlayerControl. 在我的活动中,我有一个由ArrayList填充的ListView,它实现了Android MediaPlayerControl。

MusicController is using the default behaviour so it disappears after 3000ms, and I would like to be able to show it any time the user touches the screen once. MusicController使用默认行为,因此它会在3000毫秒后消失,我希望能够在用户每次触摸屏幕时显示它。 At the moment when the user touches the screen, the item in the ListView is selected. 在用户触摸屏幕时,将选择ListView中的项目。 I would like to simply call controller.show() instead. 我想简单地调用controller.show()代替。 Unfortunately, the code below does not work. 不幸的是,下面的代码不起作用。 It simply activates whatever item is selected in the listview. 它只是激活在列表视图中选择的任何项目。

Do I need to add some kind of overlay to my activity? 我需要在活动中添加某种叠加层吗? I just want some sort of "super touch listener" that listens for an on touch event anywhere on the screen, responds with controller.show(). 我只想要某种“超级触摸监听器”,它可以监听屏幕上任何地方的触摸事件,并通过controller.show()进行响应。

I've added this to my MainActivity: 我已将其添加到我的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;
}

EDIT: This is after I attempted what Marcus suggested 编辑:这是在我尝试了马库斯建议之后

activity_main.xml 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 snippet. mainActivity.java代码段。 Added to onCreate() method 添加到onCreate()方法

//when user touches screen show controls //当用户触摸屏幕显示控件时

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

You could add a OnTouchListener for your application layout. 您可以为应用程序布局添加OnTouchListener In your layout file, add this to your root layout element: 在布局文件中,将其添加到根布局元素中:

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

Then you add the listener 然后添加侦听器

//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
    }
});

To address "ignores subsequent events until 3000ms later.", you would need to keep track of the time somehow. 要解决“直到3000ms之后才忽略后续事件”,您需要以某种方式跟踪时间。 You can get the current time with System.currentTimeMillis(); 您可以使用System.currentTimeMillis();获得当前时间System.currentTimeMillis(); , save that value then check it in the onTouch method. ,保存该值,然后在onTouch方法中对其进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM