我的代码如下 我的问题是当活动第一次打开时长按listview不能正常工作。然后再次我在listview中持有它会工作。为什么这发生任何人都有任何想法。 在此先感谢
editListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
editListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
editListView.setMultiChoiceModeListener(new ListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int position, long l, boolean b) {
actionMode.setTitle(editListView.getCheckedItemCount() + " Selected");
// Toggle the state of item after every click on it
mAdapterEdit.toggleSelection(position);
}
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
actionMode.getMenuInflater().inflate(R.menu.menu_group_edit, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
if (menuItem.getItemId() == R.id.delete_action) {
SparseBooleanArray selected = mAdapterEdit.getSelectedIds();
short size = (short) selected.size();
for (byte I = 0; I < size; I++) {
if (selected.valueAt(I)) {
GroupContactDAO selectedItem = mAdapterEdit
.getItem(selected.keyAt(I));
mAdapterEdit.remove(selectedItem);
}
}
actionMode.finish();
return true;
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
}
});
return false;
}
});
答案 0 :(得分:0)
问题是您在setChoiceMode()
内调用setMultiChoiceModeListener()
和OnItemLongClickListener
,这意味着在您对列表项执行长按之前,这些方法不会被调用。
将editListView.setChoiceMode()
和editListView.setMultiChoiceModeListener()
放在OnItemLongClickListener
之外:
editListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
editListView.setMultiChoiceModeListener(new ListView.MultiChoiceModeListener() {
// ...
}
editListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
// ...
}