我有一个ListView子类,当上下文操作栏(CAB)处于活动状态时,允许在其上进行选择。 CAB被设置为onItemLongClick
事件的回调函数:
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(context_menu, menu);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
return true;
}
这很好,ListView按预期工作,并且当前选定的项目在触摸时保持高亮显示。
当我关闭 CAB 时,我希望 ListView 返回正常状态(即触摸模式)。问题在于,无论我尝试使用什么方法来清除它,最后选择的项都会无限期地保持高亮显示:
public void onDestroyActionMode(ActionMode mode) {
//Unselect any rows
ListView lv = getListView();
lv.clearChoices(); // Has no effect
lv.setChoiceMode(ListView.CHOICE_MODE_NONE); // Has no effect on the highlighted item
lv.setFocusable(false); // Has no effect
lv.setSelection(0); // Has no effect
mActionMode = null;
}
有什么建议吗?
StackOverflowErrors
。调用setItemChecked()
会再次触发onDestroyActionMode()
。 - CommonsWaregetChildCount()
不同于getCount()
。getChildCount()
包括头部和尾部,并且仅包括列表中可见的视图(从0开始,无论您在列表中滚动到哪个位置)。setItemChecked()
接受一个position
参数,该参数与子项索引表示的位置不同。 - Joelv.clearChoices()
是必需的,即使没有它,代码对我来说也可以正常运行。 - Or B