选择多个自定义的ListView与CAB多个、自定义、CAB、ListView

2023-09-12 01:27:28 作者:浅夏琉璃

阅读和try'n'error几天后,I'm放弃,并寻求帮助。

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.

我的尝试: 继本教程,使用可检查排布置采用了一些逻辑来改背景颜色时的选中状态被切换,我得到了一切,除了工作,我可以不上的ListView注册一个点击监听器像OnItemClickListener显示CAB。无论是提供了一个点击监听器为每一行查看帮助,因为这prevented更改所选项目的背景色。 我也尝试添加一个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.

我要寻找什么:提示或教程或样品code如何做到这一点。如果你需要一些code片段的帮助,让我知道。

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.

推荐答案

使用ActionBarSherlock的 MultiChoiceModeListener 在Luksprog's答案使用,如果你想支持尚不可用的API水平&LT; 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 { /* ... */ }