【问题标题】:In ListView when list item has button onListItemClick doesn't work在 ListView 中,当列表项有按钮时 onListItemClick 不起作用
【发布时间】:2013-02-28 15:00:32
【问题描述】:

在我的 ListView 中,我的列表项具有如下视图

这是我的 row.xml 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/Peru"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/et_back_yellow"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/ib_edit"
            android:layout_width="35dp"
            android:layout_height="30dp"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="@drawable/ib_selector"
            android:contentDescription="@string/edit"
            android:src="@drawable/edit" />

        <TextView
            android:id="@+id/tv_date"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="100"
            android:gravity="center_vertical|center_horizontal" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="100"
            android:gravity="center_vertical|center_horizontal" />

        <ImageButton
            android:id="@+id/ib_delete"
            android:layout_width="35dp"
            android:layout_height="30dp"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="@drawable/ib_selector"
            android:contentDescription="@string/edit"
            android:src="@drawable/delete" />
    </LinearLayout>

    <EditText
        android:id="@+id/et_note"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/title"
        android:layout_alignRight="@+id/title"
        android:layout_below="@+id/title"
        android:layout_marginTop="5dp"
        android:ems="10"
        android:inputType="textMultiLine" 
        android:background="@color/yellow">

        <requestFocus />
    </EditText>

</RelativeLayout>

当我单击列表项时,onListItemClick 方法不起作用。但是当我从 row.xml 中删除按钮时,它起作用了。谁能帮助我。提前致谢。

【问题讨论】:

  • 发布您的按钮点击代码。
  • 请为您的活动发布 Java 代码

标签: android


【解决方案1】:

在您的LinearLayoutRelativeLayout 中使用此属性:

android:descendantFocusability="blocksDescendants"

【讨论】:

  • 非常感谢我的兄弟,它成功了,但为什么我们只将它用于线性和相对布局?
  • 它将阻止您的Button 获得焦点,因此整个ListView 项目将获得焦点。如果它有效,请接受它作为答案。
【解决方案2】:

您不能在 ListView 的子视图中同时拥有 onItemClickonClick,如 here 所述。 Button 启用了焦点和点击功能,这将导致 onItemClick 不触发。

【讨论】:

  • 错了。您可以为某些子 Views 实现 onClick,同时仍对整个 ListView 项目使用 onItemClick
  • Romain Guy 的回答在问题的上下文中是正确的。还阅读了 Delyan 对他的回答的评论,他能够做到。我已经做了好几次了。
【解决方案3】:

我认为问题的原因是图像按钮的布局权重。因此,请尝试为线性布局中的所有视图赋予权重,并将 weightSum 属性赋予线性布局本身。

我觉得 clickListener 不起作用的原因是 ImageButton 将自身包裹在整个行的宽度上,并且该项目不再具有侦听器代码可以执行的空间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    相关资源
    最近更新 更多