选择题ListView和按钮:焦点问题 [英] Multiple choice ListView and buttons: focus issues

查看:96
本文介绍了选择题ListView和按钮:焦点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是很新的Andr​​oid和我试图使用自定义的多种选择列表视图的项目定义如下:

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>

在ListView的选择模式被设置为'multipleChoice'和CheckableLinearLayout是的LinearLayout的一个子类,它实现了可勾选接口方式如下:

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 (mCheckable != 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,

推荐答案

将行内所有的控制是不可作为焦点。这是不是这样的,因为现在按钮不执行可勾选,因此不会获得 setFocusable (假)的布局。

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;
            }
        }

这篇关于选择题ListView和按钮:焦点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆