Gmail的类似的ListView与复选框(并用动作条)复选框、类似、动作、Gmail

2023-09-12 01:37:19 作者:最后只剩我

我试图重新谷歌有什么用做在Gmail应用程序中的ListView。我特别想有每个列表项包括CheckBox和两个TextViews(一个在另一个之上)。我需要监听时,复选框被选中(或点击)和列表项被点击在其他任何地方上。最后,我想在动作条,以反映项目选择,并提供选项,如全选,不选,等等(见的这个截图)。

I'm trying to recreate what Google did with the ListView in the Gmail app. In particular, I would like to have each list item include a CheckBox and two TextViews (one on top of the other). I need listeners for when the CheckBox is checked (or clicked) and when anywhere else on the list item is clicked. Lastly, I'd like the ActionBar to reflect that items are selected and provide options like Select All, Select None, etc (see this screenshot).

到目前为止,这是我想出了布局。

So far, here's the layout I came up with.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <CheckBox android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal" />

    <LinearLayout android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="6dp"
        android:focusable="true"
        android:clickable="true" >

        <TextView android:id="@+id/titleTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp" />

        <TextView android:id="@+id/dateTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp" />

    </LinearLayout>

</LinearLayout>

这显示一切正常,但我需要如何设置侦听器的两个视图指针( @ + ID /复选框和 @ + ID / linearLayout1 ) 。我已经看过了List16 API演示,但他们所使用的simple_list_item_activated_1布局,我不知道什么是XML的,看起来像。由于他们的code暗示,我创建了一个 ModeCallback 类实现 ListView.MultiChoiceModeListener ,然后我设置ListView的选择模式CHOICE_MODE_MULTIPLE_MODAL,但我不知道该怎么让复选框在我的布局,这个工作。

This displays everything properly, but I need pointers on how to set up the listeners for the two Views (@+id/checkBox and @+id/linearLayout1). I have looked at the List16 API demo, but they're using the simple_list_item_activated_1 layout and I'm not sure what the XML for that looks like. As their code suggests, I created a ModeCallback class that implements ListView.MultiChoiceModeListener and I set the ListView's choice mode to CHOICE_MODE_MULTIPLE_MODAL, but I don't know how to get the CheckBox in my layout to work with this.

有没有人成功复制的Gmail应用程序的ListView的行为?我已经搜查了不少,无法拿出任何东西(尽管其他几个人问类似的问题,like这个 - 大多数的答案只是点回到这个相同的API演示)

Has anyone successfully copied the Gmail app's ListView behavior? I've searched quite a bit and could not come up with anything (despite several others asking similar questions, like this one - most answers just point back to this same API demo).

另外,对于环境,我从SQLite数据库到列表中加载数据,我已经创建了自己的光标适配器(即正常工作)。我有我需要建立在这个类听众的NewView的()和b​​indView()方法的感觉,但一切我试过没有奏效。

Also, for context, I'm loading data from a SQLite database into the list and I've created my own Cursor adapter (which works fine). I have a feeling I need to set up listeners in this class in the newView() and bindView() methods, but everything I've tried hasn't worked.

任何想法?

推荐答案

在图中的模式被称为动作模式。如果您使用的是自定义行视图和自定义适配器,你不必使用CHOICE_MODE_MULTIPLE_MODAL开始行动模式。我试了一次,失败了,我怀疑这是用内置的适配器。

The mode in the figure is called action mode. If you are using your custom row view and custom adapter, you don't have to use CHOICE_MODE_MULTIPLE_MODAL to start action mode. I tried it once and failed, and I suspect it is used for built-in adapter.

为了自己打电话的操作模式,在code,调用方法startActionMode的任何视图的任何监听器方法,让它成为复选框,TextView的或类似的东西。然后,通过ModeCallback作为参数进去,做任何操作,您想要做的ModeCallback类。

In order to call the action mode by yourself in your code, call method startActionMode in any listener method of any of your view, let it be checkbox, textview or something like that. You then pass the ModeCallback as parameters into it, and do whatever operation you want to do in ModeCallback class.

我不认为这人会工作pre-3.0的Andr​​oid虽然。

I don't think this one would work on pre-3.0 Android though.

下面是一个简单的例子。我有一个扩展列表活动,想调出操作模式菜单,当用户选中/取消选中该复选框,并在这里是我的getChildView方法在我的自定义适配器类:

Here is a brief example. I have a expandable list activity and would like to call out the action mode menu when user check/uncheck the checkbox, and here is my getChildView method in my custom adapter class:

public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater =  (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.childrow, null);
        }
        CheckBox cb = (CheckBox)convertView.findViewById(R.id.row_check);
        cb.setChecked(false);   // Initialize
        cb.setTag(groupPosition + "," + childPosition);
        cb.setFocusable(false); // To make the whole row selectable
        cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String tag = (String)buttonView.getTag();
                String[] pos = tag.split(",");
                if (isChecked) {
                    if (mActionMode == null) mActionMode = startActionMode(mMultipleCallback);                      
                }
                else {
                    if (mActionMode != null) {  // Only operate when mActionMode is available

                        mActionMode.finish();
                        mActionMode = null;
                    }
                }
            }
        });

        TextView tvTop = (TextView)convertView.findViewById(R.id.row_text_top);
        TextView tvBottom = (TextView)convertView.findViewById(R.id.row_text_bottom);
        tvTop.setText(mChildren.get(groupPosition).get(childPosition));
        tvBottom.setText(mChildrenSize.get(groupPosition).get(childPosition));
        return convertView;
    }