列表视图 OnClick 不起作用

Listview OnClick does not work

我有一些代码可以显示 ListView 并允许单击某个项目以将其带到单个视图 activity。但是,由于某种原因,onClickListener 无法正常工作。我认为这可能是由于 EditText 是可聚焦的。但是,将其设置为false后,我发现并非如此。

listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:textSize="15dp"
android:inputType="textMultiLine">


</TextView>

删除 TextView 布局上的 inputType 属性:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/text"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="5dp"
          android:textSize="15sp"/>

另外,如您所见,我分别使用 match_parentwrap_content 作为宽度和高度。 fill_parent 现已弃用。

匹配父项意味着它将适合整个 ListView 的宽度,并且内容会根据需要进行换行,因此整个文本将可见,即使它是多行的。

另一个注意事项:对于文本大小,您使用 sp,对于宽度、高度等,您使用 dpWorthy article 进一步解释。

后台任务一旦执行就没有句柄。所以你的列表视图监听器将被杀死。您需要将其从后台移除并将其放在您的 onCreate 中。您可以在 postExecute 中设置一个标志,如果它依赖于该标志,则执行实施。

在您的列表项布局中使用 android:descendantFocusability="blocksDescendants"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants">

    <TextView 
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5sp"
        android:textSize="15dp"
        android:inputType="textMultiLine" />

</LinearLayout>

已弃用 fill_parent 替换为 match_parent