ListViewをカスタマイズして実装しているときに、
リスト1つ1つの中にImageButtonがあると、
リスト全体のクリックイベントも、ImageButtonのクリックイベントも拾ってくれないらしい。
解決策としては、
1. xmlのImageButtonに
android:focusableInTouchMode="false"
android:focusable="false"
を追加する
または
javaで以下を設定する
((ImageButton)v.findViewById(R.id.recommend_checkin)).setFocusableInTouchMode(false);
((ImageButton)v.findViewById(R.id.recommend_checkin)).setFocusable(false);
2. ***_item.xml(リストの中身のレイアウト)の一番外に
android:descendantFocusability="blocksDescendants"
を追加する
例)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:gravity="center" >
<ImageView
android:id="@+id/recommend_product_image"
android:layout_width="250dp"
android:layout_height="250dp"
android:background="#e5fff7"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:gravity="left|center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/recommend_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="@+id/recommend_pickup_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
これで、全体のクリックイベントを拾ってくれるようになる。
さらに、ImageButtonのクリックイベントを拾うためには、
adapterの中でonClickイベントを拾ってやればいいらしい。
参考URL
http://tomstay.hatenablog.jp/entry/20110421/1303391426
http://blog.babukuma.com/2010/01/android-listviewbutton.html
0 件のコメント:
コメントを投稿