a textview in a listview' s row cannot click after setting descendantFocusability=“blocksDescendants”

有些话、适合烂在心里 提交于 2019-12-20 04:12:51

问题


I write a customized item layout for a listview. The layout has many widgets and some will have its own clicklistener.

When I click the row, sometimes the listview' s onListItemClick work, but sometimes not.

After I spent some time searching, I find a way, setting android:descendantFocusability="blocksDescendants" in the layout' s root. It works, but only one textview cannot work,(the clickable, fucusable attr have been tried). In the list' s adapter, I set the textview' s onClickListener, it works. But that' s not nature, does anyone know how to solve it?

btw, is there a way to distinguish diffderent widget' click? Now, no matter what I click(except that textview), the whole row' s background selector got changed.

many thanks!

// my list row layout root

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:descendantFocusability="blocksDescendants"
android:padding="@dimen/medium"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

// the awful textview

<TextView
        android:id="@+id/text"
        android:textColor="@android:color/secondary_text_light"
        android:textColorLink="@android:color/holo_blue_dark"
        android:layout_marginTop="@dimen/small"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault"
        android:layout_below="@id/nick"
        android:layout_alignLeft="@id/nick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

update

the textview uses Linkify to provide some links functionalities.


回答1:


After spenting some time trying, I solved both.

  1. the first. if your customized listview' s layout has a textview which has been set autolink or Linkify in the code, the click event in the textview won' t affect the list' s row. You have to extends your own TextView and override onTouchEvent method. please see it here

  2. the second. just set in the customized listview' s root node: android:descendantFocusability="blocksDescendants", and then, in your widget which will have its own onClickListener, set android:clickable="true", it works for me.

hope it useful for those you encounter the same:-)



来源:https://stackoverflow.com/questions/21891008/a-textview-in-a-listview-s-row-cannot-click-after-setting-descendantfocusabilit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!