使用ListView.CHOICE_MODE_MULTIPLE_MODAL时以编程方式启动CAB

时间:2014-05-19 09:58:19

标签: android listview

我的问题是,我无法在方向更改后FragmentListView以编程方式启动CAB。

这是我的实施。一切正常但我想用选定的行再次启动CAB。但不知道如何开始它。我试过StartActionMode,但它没有用。 ListView是自定义Listview。任何帮助都会得到满足。

//ListView
mListView = getListView();
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
mListView.setMultiChoiceModeListener(new ModeCallback());

//ModeCallBack

private class ModeCallback implements ListView.MultiChoiceModeListener {
    ArrayList<Long> idSelection;
    ActionMode mMode;

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.menu_contextual, menu);
        mode.setTitle("Select Items");
        idSelection = new ArrayList<Long>();
        mMode = mode;
        isInActionMode = true;
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return true;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_discard:
            deleteNotes();
            // mode.finish();
            break;
        case R.id.action_select_all:
            selectAllNotes();
            break;
        default:
            break;
        }
        return true;
    }

    public void onDestroyActionMode(ActionMode mode) {
        if (idSelection != null)
            idSelection.clear();
        isInActionMode = false;
    }

    public void onItemCheckedStateChanged(ActionMode mode, int position,
            long id, boolean checked) {

        if (checked)
            idSelection.add(Long.valueOf(id));
        else
            idSelection.remove(Long.valueOf(id));

        final int checkedCount = getListView().getCheckedItemCount();
        switch (checkedCount) {
        case 0:
            mode.setSubtitle(null);
            break;
        case 1:
            mode.setSubtitle("One item selected");
            break;
        default:
            mode.setSubtitle("" + checkedCount + " items selected");
            break;
        }
    }

    private void selectAllNotes() {
        // Log.i(TAG, "Inside selectAllNotes");
        for (int i = 0; i < getListView().getCount(); i++) {
            getListView().setItemChecked(i, true);
        }
    }

    private void deleteNotes() {
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    String whereClause = NoteStorage.KEY_ROWID + " IN  ";
                    StringBuffer ids = new StringBuffer();
                    ids.append("(");
                    for (int i = 0; i < idSelection.size(); i++) {
                        if (i != 0)
                            ids.append(",");
                        ids.append(idSelection.get(i));
                    }
                    ids.append(")");
                    // Log.i(TAG, ids.toString());
                    whereClause = whereClause + ids;
                    getActivity().getContentResolver().delete(
                            MyContentProvider.CONTENT_URI_NOTES,
                            whereClause, null);
                    mCallback.onListNoteDelete();
                    // clipboardStorage.deleteEntries(idSelection);
                    Toast msg = Toast.makeText(getActivity(),
                            idSelection.size() + " note(s) deleted",
                            Toast.LENGTH_LONG);
                    msg.setGravity(Gravity.CENTER, 0, 0);
                    msg.show();
                    if (mMode != null)
                        mMode.finish();
                    break;
                case DialogInterface.BUTTON_NEGATIVE:
                    break;
                }
            }
        };
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Delete selected item(s)?")
                .setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("No", dialogClickListener).show();

    }

}`

1 个答案:

答案 0 :(得分:4)

通过选择列表中的项目,您可以在使用CHOICE_MODE_MULTIPLE_MODAL时启动操作模式。

mListView.setItemChecked(selectedItemPosition, true);

您可以存储所有选定项目的位置并在循环内调用它。