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

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

问题描述

我想重新创建Google在Gmail应用中使用ListView所做的事情。特别是,我想让每个列表项包括一个复选框和两个TextViews(一个在另一个顶部)。我需要监听器,当复选框被选中(或点击),当列表项上的其他任何地方被点击。最后,我想ActionBar反映选择的项目,并提供选项,如全选,选择无等等(见,但他们使用simple_list_item_activated_1布局,我不知道什么样的XML。如他们的代码建议,我创建了一个实现 ListView.MultiChoiceModeListener ModeCallback 类,我将ListView的选择模式设置为CHOICE_MODE_MULTIPLE_MODAL,但我不知道如何获取CheckBox在我的布局中使用这个。



有没有人成功复制Gmail应用程序的ListView行为?我搜索了很多,不能提出任何东西(尽管有几个人提出类似的问题, like this one - most answers只是指向同一个API演示)。



我从一个SQLite数据库中的数据加载到列表中,我已经创建了自己的Cursor适配器(工作正常)。我有一个感觉,我需要在这个类中的newView()和bindView()方法中设置监听器,但我尝试的一切都没有工作。



任何想法?

解决方案

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



为了在你的代码中自己调用操作模式,调用方法startActionMode在任何你的视图的任何监听器方法,让它是checkbox,textview或类似的东西。然后将ModeCallback作为参数传递给它,并在ModeCallback类中做任何你想做的操作。



我不认为这个工作在3.0之前Android。



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

  public查看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); //使整行可选
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){//只在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));
return convertView;
}


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天全站免登陆