【问题标题】:Call onTouchEvent() before listView在 listView 之前调用 onTouchEvent()
【发布时间】:2015-03-04 08:20:40
【问题描述】:

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

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

我是否需要在我的活动中添加某种叠加层?我只想要某种“超级触摸侦听器”,它可以侦听屏幕上任何位置的触摸事件,并使用 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 sn-p。添加到 onCreate() 方法

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

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

【问题讨论】:

  • 我猜你可以在你的布局上设置一个id,然后在你的整个应用布局上设置一个OnTouchListener
  • @Marcus - 你确实给了我一个想法,但恐怕它没有成功。我创建了一个新方法 pubic void showControls(View view) { controller.show()} 并在我的 RelativeLayout 中创建了一个 onClick 属性。也许我需要在某处添加 onClick ?然而,我对 Android 的有限知识阻碍了我。我还将研究如何实现 OnTouchListener。谢谢你的建议。
  • 我添加了一个答案,它会给你一个关于如何做你所描述的事情的提示。试试看,让我知道!

标签: android listview android-listview touch-event


【解决方案1】:

您可以为您的应用程序布局添加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
    }
});

要解决“在 3000 毫秒后忽略后续事件。”,您需要以某种方式跟踪时间。您可以使用System.currentTimeMillis(); 获取当前时间,保存该值然后在onTouch 方法中检查它。

【讨论】:

  • 谢谢你,我已经按照你的建议试过了,但还是没有成功。我也将笔记与这个答案进行了比较stackoverflow.com/questions/11180879/…。我已经用代码编辑了我原来的问题,所以你可以看到我做了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-12
  • 1970-01-01
  • 1970-01-01
  • 2012-03-19
  • 2012-10-16
相关资源
最近更新 更多