开发者

Multiple choice ListView and buttons: focus issues

开发者 https://www.devze.com 2023-02-15 23:01 出处:网络
I\'m quite new to Android and I\'m trying to use a custom multiple choice listview whose items are defined as follows:

I'm quite new to Android and I'm trying to use a custom multiple choice listview whose items are defined as follows:

<?xml version="1.0" encoding="utf-8"?>
<com.jroy.android.views.CheckableLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="left|center_vertical">
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@drawable/checkbox_selector"
        android:button="@null" />
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <Button
        android:id="@+id/btn_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@drawable/button_view_selector" />
</com.jroy.android.views.CheckableLinearLayout>

The choice mode of the ListView is set to 'multipleChoice' and CheckableLinearLayout is a subclass of LinearLayout which implements the Checkable interface the following way:

public class CheckableLinearLayout extends LinearLayout implements Checkable {

    private Checkable mCheckable;                  

    @Override
    protected void onFinishInflate() {

        super.onFinishInflate();

        // Find checkable view
        for (int i = 0, childCount = getChildCount(); i < childCount; ++i) {
            View v = getChildAt(i);
            if (v instanceof Checkable) {
                mCheckable = (Checkable) v;
                v.setFocusable(false);
                v.setClickable(false);
                break;
            }
        }

    }

    public CheckableLinearLayout(Context context) {
        super(context);
    }

    public CheckableLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean isChecked() {
        return (mChe开发者_C百科ckable != null)
                ? mCheckable.isChecked()
                : false;
    }

    @Override
    public void setChecked(boolean checked) {
        if (mCheckable != null) {
            mCheckable.setChecked(checked);
        }
    }

    @Override
    public void toggle() {
        if (mCheckable != null) {
            mCheckable.toggle();
        }
    }

}

The problem is that I can't check any item, it seems like the buttons only can have the focus. I tried different things about focus but I didn't manage to get it work correctly...

What is the correct way to do what I try to achieve ? Thanks,


Set all the controls inside the row to be "not focusable". It is not the case now because Button does not implement Checkable and thus does not get setFocusable(false) in your layout.

        for (int i = 0, childCount = getChildCount(); i < childCount; ++i) {
            View v = getChildAt(i);
            v.setFocusable(false);
            if (v instanceof Checkable) {
                mCheckable = (Checkable) v;
                v.setClickable(false);
                break;
            }
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消