具有 ImageButton 可聚焦性的 ListView

ListView with ImageButton focusability

我有一个带有 TextView 和 ImageButton 的 ListView。

ListView 有 OnItemClickListener(),ImageButton 可能有也可能没有 OnClickListener()

下面是我的layout.xml

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

<com.bolo.customview.CustomTextViewLight
    android:id="@+id/textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/contacts_friends_chat_button"
    android:textColor="@color/text_color_primary"
    android:textSize="@dimen/friend_name"/>

<ImageButton
    android:id="@+id/imagebutton"
    android:layout_width="@dimen/sixtyfour"
    android:layout_height="@dimen/sixtyfour"
    android:layout_alignParentRight="true"/>

</RelativeLayout>

有时我将 ImageButton 的可见性设置为 VISIBLE,有时设置为 INVISIBLE。

下面是我想要完成的。

  1. 如果 ImageButton 有 onclicklistener,那么 ImageButton 应该捕获对 ImageButton 的点击,而 ListView 应该捕获对其余区域的点击。目前这是 运行 完美。

  2. 如果 ImageButton 没有 onClickListener(ImageButton 可以是 VISIBLE 或 INVISIBLE 无关紧要),那么 ImageButton 永远不会捕获点击事件。 ListView 应捕获所有单击事件(在 ImageButton 和该区域的其余部分)。目前这还没有发生。当 ImageButton 可见时,它会捕获点击事件。

如何做到这一点?请帮帮我。

如果我没理解错的话,你可以试试:

制作不希望有点击事件的按钮:clickable=false。

或者如果您只是不想在不可见时发生点击,您可以将 imageButton 可见性设置为 GONE 而不是 INVISIBLE。