Multiple selection in custom ListView with CAB

前端 未结 3 793
孤城傲影
孤城傲影 2020-11-28 21:43

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

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

What I want to achie

3条回答
  •  青春惊慌失措
    2020-11-28 22:37

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

    A workaround is to use the onItemClickListener.

    List setup:

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

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

    Where MyActionMode is an implementation of ActionMode.Callback:

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

提交回复
热议问题