header_medi8

ビットコイン取引高日本一の仮想通貨取引所 coincheck bitcoin

2015年12月18日金曜日

【Android】ListViewのItemをカスタマイズしたらOnItemClick()イベントが取れなくなった場合の対処法

Alarmアプリを作成する際に、ListFragmentを利用してAlarm設定の一覧を表示する部分を作成しようとしていて、
ListViewのListItemをカスタマイズしてTextViewとSwitchを設置したら、ListViewのOnItemClickイベントがとれなくなってしまった。

SwitchのON/OFFには反応しているけど、Listの項目のクリックに反応してくれない。
それでも、Alarmの設定画面に移動できないので調べてみたら対処法があった。

ListItemのLayoutを定義しているxmlファイルを開いて、一番上のLayoutの属性に以下の1行を追加します。

android:descendantFocusability="blocksDescendants"




たとえば、こんな感じのListItemを表示するためのLayoutファイルの中身はこんな感じです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/abc_dialog_padding_top_material"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:id="@+id/list_item_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="00:00"
        android:textSize="@dimen/abc_text_size_display_1_material"
        android:layout_weight="1" />

    <Switch
        android:id="@+id/list_item_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="ON"
        android:textOff="OFF"/>

</LinearLayout>

0 件のコメント:

コメントを投稿