我有一个ExpandableListView,我为它实现了选择(短击)和删除(长击)。短列表项单击由onChildClick()处理,长单击由onCreateContextMenu()处理。
@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");
}除了运行安卓7的Nexus5X外,这在所有设备上都能很好地工作。在这里,长时间点击就可以通过onChildClick触发上下文菜单和选择,这显然不是我想要的。
如何在仍然使用自定义对话框时阻止项选择。
发布于 2016-10-01 18:55:55
我可以提供我目前的解决方案或解决办法,感觉不太理想,因为它修补了一些我打破的东西,用我的自定义对话框替换上下文菜单。思想是在删除处理启动时静音选择处理,并在对话框的回调中解除其静音。
这是可行的,但我一开始就不想破坏它。所以可能有更好的方法。
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;
}
}https://stackoverflow.com/questions/39809411
复制相似问题