使用 CAB 在自定义 ListView 中进行多项选择 [英] Multiple selection in custom ListView with CAB

查看:19
本文介绍了使用 CAB 在自定义 ListView 中进行多项选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读并尝试了几天后,我放弃并寻求帮助.

After reading and try'n'error for days, I´m giving up and ask for help.

<编辑 >我正在使用 ActionBarSherlock.</编辑>

< edit > I am using ActionBarSherlock. < /edit >

我想要达到的目标:每行都有一个自定义布局的 ListView,用户可以在其中选择多个列表项.选定的列表项应具有不同的背景颜色.当至少选择了一项时,应显示上下文操作栏 (CAB).它应该或多或少类似于 GMail 应用程序中的多选电子邮件.唯一的区别是在 gmail 应用程序中,选择是通过单击一行的复选框来完成的,而我不想有一个复选框,但无论用户单击何处,都应该选择一行.

What I want to achieve: A ListView with a custom layout for each row, where the user can select multiple list items. A selected list item should have a different background color. When there is at least one item selected, a contextual action bar (CAB) should be shown. It should look more or less like the multiple selection of emails in the GMail app. The only difference is that in the gmail app the selection is done by clicking the checkbox of a row, whereas I don´t want to have a checkbox, but a row should be selected no matter, where the user clicks.

我的尝试:按照本教程,使用 Checkable 行布局切换检查状态时更改背景颜色的一些逻辑,除了我无法在 ListView 上注册像 OnItemClickListener 这样的单击侦听器来显示 CAB 之外,一切正常.为每一行视图提供点击监听器都没有帮助,因为这阻止了更改所选项目的背景颜色.我也尝试将 MultiChoiceModeListener 添加到 ListView 中

What I tried: Following this tutorial, using a Checkable row layout with some logic to change the background color when the check state was toggled, I got everything working except that I could not register a click listener like OnItemClickListener on the ListView to show the CAB. Neither providing a click listener for each row View helped because this prevented to change the background color of the selected items. I also tried adding a MultiChoiceModeListener to the ListView like that

    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new MultiChoiceModeListener() { //.. });

结果相同,背景颜色没有变化.

With the same result, no background color change.

我在寻找什么:如何执行此操作的提示或教程或示例代码.如果您需要一些代码片段来提供帮助,请告诉我.

What I am looking for: A hint or a tutorial or sample code how to do this. If you need some code snippets to help, let me know.

推荐答案

Using ActionBarSherlock 如果您想支持 API 级别 <11.

Using ActionBarSherlock the MultiChoiceModeListener used in Luksprog´s answer is not yet available if you want to support API level < 11.

一种解决方法是使用 onItemClickListener.

A workaround is to use the onItemClickListener.

列表设置:

listView = (ListView) timeline.findViewById(android.R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemsCanFocus(false);
listView.setAdapter(new ListAdapter(getActivity(), R.layout.cleaning_list_item, items));

ListFragment 或 ListActivity 的监听器:

Listener of ListFragment or ListActivity:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    SparseBooleanArray checked = listView.getCheckedItemPositions();
    boolean hasCheckedElement = false;
    for (int i = 0; i < checked.size() && !hasCheckedElement; i++) {
        hasCheckedElement = checked.valueAt(i);
    }

    if (hasCheckedElement) {
        if (mMode == null) {
            mMode = ((SherlockFragmentActivity) getActivity()).startActionMode(new MyActionMode());
            mMode.invalidate();
        } else {
            mMode.invalidate();
        }
    } else {
        if (mMode != null) {
            mMode.finish();
        }
    }
}

其中 MyActionMode 是 ActionMode.Callback 的实现:

Where MyActionMode is an implementation of ActionMode.Callback:

private final class MyActionMode implements ActionMode.Callback { /* ... */ }

这篇关于使用 CAB 在自定义 ListView 中进行多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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