Android ListView中的按钮不可选(可点击)

8

我遇到了一个奇怪的问题,我在我的ListView中添加了自定义行,当我删除按钮行时,行是可选择的,但是当我添加按钮时,我无法单击行,请参见下面的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" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="77dp"
    android:layout_height="77dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="18dp"
    android:src="@drawable/company_logo" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Idds  sdsad "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/imageView1"

    android:layout_below="@+id/textView1"
    android:textColor="#8b8989"
    android:layout_marginLeft="5dp"
    android:text="Tap to see detail"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:text="Button" />

请帮忙解释为什么会出现这种情况。

2个回答

27

在使用自定义行时。

在您的getView中为button设置onclickListener后,将其focusability false

button.setFocusable(false)

并且还要为您的行布局容器设置android:descendantFocusability="blocksDescendants"。 您可以直接设置android:focusable="false",但这将使您的按钮无法点击。


11
在行的布局容器上设置 android:descendantFocusability="blocksDescendants" 就足以解决我的问题。我不需要对按钮的focusable属性做任何处理。 - harmanjd
更好的解决方案,因为它更具声明性和高级别。非常好。 - Marchy

20

尝试设置

android:focusable="false"
android:focusableInTouchMode="false"

在xml中添加Button。由于Button获得了行的焦点,所以您无法选择该行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接