`
xidajiancun
  • 浏览: 456196 次
文章分类
社区版块
存档分类
最新评论

ListView的Item单击无响应

 
阅读更多

这个问题整了很久,今天终于解决了

问题描述:

点击ListView的Item无效果


问题分析:

Item的布局代码是这样的:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/llRight"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"
        android:gravity="right"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btMark"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@android:color/holo_orange_dark"
            android:text="Mark" />

        <Button
            android:id="@+id/btDelete"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@android:color/holo_green_dark"
            android:text="Delete" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/llLeft"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:text="tvTitle" />
    </LinearLayout>

</FrameLayout>
按理说点击这个Item的时候ListView背景应该改变,但是现在什么效果都没有。第一想到的是会不会Item被盖住了,所以给Item设置个padding,运行下padding是有了,但是还是不能点击:

其实始终心里都感觉不会是这个原因。

既然不是这个原因,还会是什么呢?想不到原因,就又把自定义的listview和adapter重新写了一遍,仔细检查了代码,折腾了好久,问题依旧。实在没办了,就用很久以前学习的方法试试吧(虽然这个时候感觉就这几个元素不削用这种方法):把ItemView中的元素全部删除,然后一个一个加入,这个是否发现了问题:当加入两个Button按钮所在布局的时候问题就出现了。会是什么原因呢?也许你可能会想到ListView和Button是不是有冲突之类,但是悲剧的是我压根就没这么想过,原因很简单:我这么干过——在listview中加button,当时没出现过问题,参考这里。然后找同事帮看下,果然是ListView和Button的问题。现在想想,应该是之前写的时候这个问题被别人给屏蔽(解决了),到我这没体现可怜


解决办法:

在Button的parentView的属性中添加:android:descendantFocusability="blocksDescendants"意思为ViewGroup会阻止他的子view获取焦点。

android:descendantFocusability

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

Must be one of the following constant values.

Constant Value Description
beforeDescendants 0 The ViewGroup will get focus before any of its descendants.
afterDescendants 1 The ViewGroup will get focus only if none of its descendants want it.
blocksDescendants 2 The ViewGroup will block its descendants from receiving focus.

This corresponds to the global attribute resource symboldescendantFocusability.

总结下下:

再遇到这种问题,还是第一时间请教别人吧


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics