【问题标题】:Android Listview OnItemClickListener sometimes not workingAndroid Listview OnItemClickListener 有时不工作
【发布时间】:2014-07-16 13:57:39
【问题描述】:

我有一个带有列表视图的小型 Android 应用程序。有时列表视图不会对我的输入做出反应。我可以看到我触摸了一个项目(当我触摸它时它会改变颜色),但程序什么也不做。 这是我的 XML 文件:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="false"
    android:focusableInTouchMode="false" >

    <TextView
        android:id="@+id/textViewRowLable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/textViewTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/textViewRowLable"
        android:gravity="right"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/textViewRemainingTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/textViewTime"
        android:layout_toRightOf="@id/textViewRowLable"
        android:gravity="right"
        android:textSize="25sp" />

</RelativeLayout>

这是监听器:

 .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG).show();
                    }

                });

有人可以告诉我为什么它并不总是有效吗?

谢谢

【问题讨论】:

    标签: android listview onitemclicklistener


    【解决方案1】:

    尝试在您的RelativeLayout 上使用android:descendantFocusability="blocksDescendants" 还要通过本教程: Listview tips and tricks

    【讨论】:

    • 问题仍然存在。应用程序没有注意到十分之二的触摸。我不知道为什么。
    • 我也有同样的问题,没搞清楚。你找到导致这种行为的根源了吗?谢谢。
    【解决方案2】:

    我遇到了类似的问题。而不是我查看了Android源代码并找到了原因。

    在点击处理中,有如下一行

    if (mDataChanged) return;
    

    所以当内部列表数据发生变化时,点击被丢弃。

    我经常更新列表。减少更新间隔几乎解决了问题(更改后,100次点击中只有1次无法识别)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      相关资源
      最近更新 更多