带有复选框的类似 Gmail 的 ListView(并使用 ActionBar) [英] Gmail-like ListView with checkboxes (and using the ActionBar)

查看:15
本文介绍了带有复选框的类似 Gmail 的 ListView(并使用 ActionBar)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重新创建 Google 对 Gmail 应用程序中的 ListView 所做的事情.特别是,我希望每个列表项都包含一个 CheckBox 和两个 TextViews(一个在另一个之上).我需要侦听器何时检查(或单击)CheckBox 以及单击列表项上的任何其他位置.最后,我希望 ActionBar 反映已选择的项目并提供全选、全选等选项(请参阅 ,但他们使用的是 simple_list_item_activated_1 布局,我不确定它的 XML 是什么样的.正如他们的代码所暗示的那样,我创建了一个实现 ListView.MultiChoiceModeListenerModeCallback 类,并将 ListView 的选择模式设置为 CHOICE_MODE_MULTIPLE_MODAL,但我不知道如何获取 CheckBox在我的布局中使用它.

有人成功复制了 Gmail 应用程序的 ListView 行为吗?我已经搜索了很多但找不到任何东西(尽管其他几个人提出了类似的问题,像这样 - 大多数答案只是指向同一个 API 演示).

此外,对于上下文,我正在将 SQLite 数据库中的数据加载到列表中,并且我已经创建了自己的 Cursor 适配器(工作正常).我有一种感觉,我需要在这个类中的 newView() 和 bindView() 方法中设置侦听器,但是我尝试过的所有方法都没有奏效.

有什么想法吗?

解决方案

图中的模式称为动作模式.如果您使用自定义行视图和自定义适配器,则不必使用 CHOICE_MODE_MULTIPLE_MODAL 来启动操作模式.试过一次失败,怀疑是内置适配器用的.

为了在您的代码中自己调用操作模式,请在您的任何视图的任何侦听器方法中调用方法 startActionMode,让它成为复选框、文本视图或类似的东西.然后您将 ModeCallback 作为参数传递给它,并在 ModeCallback 类中执行您想做的任何操作.

不过,我认为这不适用于 3.0 之前的 Android.

这是一个简单的例子.我有一个可扩展的列表活动,并希望在用户选中/取消选中复选框时调出操作模式菜单,这是我的自定义适配器类中的 getChildView 方法:

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);//初始化cb.setTag(groupPosition + "," + childPosition);cb.setFocusable(false);//使整行可选cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {String tag = (String)buttonView.getTag();String[] pos = tag.split(",");如果(已检查){if (mActionMode == null) mActionMode = startActionMode(mMultipleCallback);}别的 {if (mActionMode != null) {//仅在 mActionMode 可用时操作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));返回转换视图;}

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>

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.

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).

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.

Any ideas?

解决方案

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.

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.

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

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

这篇关于带有复选框的类似 Gmail 的 ListView(并使用 ActionBar)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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