dev/Android

Android の開発メモなど

ImageButton に xml で focusable="false" しても無意味

なぜなら、ImageButton のコンストラクタの中で setFocusable(true) しているから・・・orz

ListView内にImageButtonを配置するとListViewがクリックできない場合の対処法 - さおくんC#とAndroid実践メモ - Yahoo!ブログ

上の記事の方と同じく、ListView に ImageButton を配置して android:focusable="false" したのにリストをクリックできなくなって悩みました。Button なら問題ないのに・・・。

上の記事の通り、ImageButton 生成後にコードから setFocusable(false) してあげれば解決。


ImageButton は ImageView を含むから、きっと ImageButton と ImageView それぞれに focusable を持っていて、どちらかがうまく設定されないのではないかと予想してたんですが。
実際には ImageButton は ImageView の子供で・・・。

public ImageButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setFocusable(true);
}

コンストラクタで super した後にわざわざ setFocusable(true) してる。
うぅむ。きっと訳あってのことなんでしょうけど・・・。


xml アトリビュートの設定漏れとかでなく、わざわざ意図的に setFocusable(true) してるとは。
楽しませてくれます^^;