gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:01:59 25 4
gpt4 key购买 nike

我的问题是,方向更改后,我无法在 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();

}

}`

最佳答案

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

mListView.setItemChecked(selectedItemPosition, true);

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

关于android - 使用 ListView.CHOICE_MODE_MULTIPLE_MODAL 时以编程方式启动 CAB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23734805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com