具有自定义视图的多项选择列表? [英] Multiple choice list with custom view?

查看:28
本文介绍了具有自定义视图的多项选择列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过来自 ApiDemos 的示例 com.example.android.apis.view.List11.在该示例中,每一行都采用视图 android.R.simple_list_item_multiple_choice.每个这样的视图都有一个 TextView 和一个 CheckBox.

I've seen example com.example.android.apis.view.List11 from ApiDemos. In that example, each row takes the view android.R.simple_list_item_multiple_choice. Each such view has a TextView and a CheckBox.

现在我希望每个视图有 2 个 TextView 和 1 个 CheckBox,有点类似于 List3 示例.我尝试像这样创建自定义布局文件 row.xml:

Now I want each view to have 2 TextViews and 1 CheckBox, somewhat similar to the List3 example. I tried creating a custom layout file row.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />
    <TextView
        android:id="@+id/text_name"
        android:textSize="13px"
        android:textStyle="bold"
        android:layout_toLeftOf="@id/checkbox"
        android:layout_alignParentLeft="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text_phone"
        android:textSize="9px"
        android:layout_toLeftOf="@id/checkbox"
        android:layout_below="@id/text_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" /> 
</RelativeLayout>

然后在ActivityonCreate()中,我喜欢这样:

Then in Activity's onCreate(), I do like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Query the contacts
    mCursor = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);
    startManagingCursor(mCursor);

    ListAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.row,
            mCursor, 
            new String[] { Phones.NAME, Phones.NUMBER}, 
            new int[] { R.id.text_name, R.id.text_phone });
    setListAdapter(adapter);
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}

结果看起来像我想要的,但看起来列表不知道它的哪个项目被选中.此外,我需要准确地单击 CheckBox.在 List11 示例中,我只需要单击项目行.

The result kind of looks like what I want, but it looks like the list doesn't know which item of it is selected. Also, I need to click exactly on the CheckBox. In the List11 example, I only need to click on the item row.

那么我需要怎么做才能用我的自定义视图为每一行制作一个多选列表?非常感谢.

So what do I need to do to make a multiple choice list with my custom view for each row? Many thanks.

推荐答案

您必须制作自己的 RelativeLayout 来实现 Checkable 接口并引用 RelativeLayoutcode>CheckBox 或 CheckedTextView(如果是多选模式,则为列表).

You have to make your own RelativeLayout that implements the Checkable interface and have a reference to the CheckBox or to the CheckedTextView (or a list if it's multiple choice mode).

看看这个帖子:http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/

这篇关于具有自定义视图的多项选择列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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