长按触发Android 7设备上的列表视图的上下文菜单和选择
问题描述:
我有一个ExpandableListView
我实现了选择(短按)和删除(长按)。短列表项点击由onChildClick()
处理,长点击由onCreateContextMenu()
处理。长按触发Android 7设备上的列表视图的上下文菜单和选择
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
mDeleteItemGroup = ExpandableListView.getPackedPositionGroup(info.packedPosition);
mDeleteItemChild = ExpandableListView.getPackedPositionChild(info.packedPosition);
menu.setHeaderTitle("some title");
MenuInflater inflater = mActivity.getMenuInflater();
inflater.inflate(R.menu.menu_my_view_context, menu);
}
上面显示了处理长按好的上下文菜单代码。问题在于缺乏适合性,它会在某些设备上截断较长的标题。所以我使用了标准上下文菜单的自定义对话框改为如下:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
mDeleteItemGroup = ExpandableListView.getPackedPositionGroup(info.packedPosition);
mDeleteItemChild = ExpandableListView.getPackedPositionChild(info.packedPosition);
String title = "some title";
ConfirmDeletePopupFragment confirmDeletePopupFragment = ConfirmDeletePopupFragment.newInstance(title);
confirmDeletePopupFragment.setTargetFragment(this, 0);
confirmDeletePopupFragment.show(getActivity().getSupportFragmentManager(), "tag");
}
这工作得很好,除了上一台Nexus 5X所有的设备运行Android 7.在这里,长按同时触发的上下文菜单和选择通过onChildClick
,这显然不是我想要的。
如何在仍然使用我的自定义对话框时阻止选择项目。
答
我可以提供我当前的解决方案或解决方案,感觉不是最佳的,因为它通过用我的自定义对话框替换上下文菜单来修补我弄坏的东西。想法是在开始删除处理时静音选择处理,并在对话框的回调中取消静音。
这个工程,但我宁愿不放在首位。所以可能有更好的方法。
public class MyListFragment extends ExpandableListFragment implements ConfirmDeletePopupFragment.DialogListener {
(...)
private boolean mMuteSelection = false;
(...)
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
mMuteSelection = true;
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
mDeleteItemGroup = ExpandableListView.getPackedPositionGroup(info.packedPosition);
mDeleteItemChild = ExpandableListView.getPackedPositionChild(info.packedPosition);
String title = "some title"
ConfirmDeletePopupFragment confirmDeleteWeakPopupFragment = ConfirmDeletePopupFragment.newInstance(title);
confirmDeletePopupFragment.setTargetFragment(this, 0);
confirmDeletePopupFragment.show(getActivity().getSupportFragmentManager(), "tag");
}
(...)
@Override
public boolean onChildClick(ExpandableListView arg0, View arg1, int group, int child, long arg4) {
super.onChildClick(arg0, arg1, group, child, arg4);
if (!mMuteSelection) {
(handle selection)
}
return false;
}
(...)
@Override
public void onDeleteConfirm(boolean delete) {
if (delete) {
(handle deletion)
}
mMuteSelection = false;
}
}
当然,一个标志可以用于静音选择,而上下文菜单处理事件,但似乎是修补了其他出错的东西。 – jerry