備忘録 〜プログラミング〜

プログラミングに関する事をつらつらと、、

ListViewの中にButtonを配置すると、ListViewのOnItemClickListenerが呼ばれなくなる問題

通常であれば、ListViewをタップしたときのEventは、OnItemClickListenerを設定してあげると、ハンドリングする事が出来ます。
しかし、ListViewの中にButton等のwidgetを配置した場合、ButtonのOnClickListenerにイベントがブロックされてしまいハンドリングする事が出来なくなります。
この問題を解決するためには、このようにListViewのlayout_hoge.xmlにdescendantFocusabilityを追加してあげると良いみたいです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>

OnItemCLickListener not working in listview ANDROID - Stack Overflow