长按在Android 7设备上触发Listview的上下文菜单和选择

时间:2016-10-01 17:14:44

标签: android expandablelistview android-contextmenu

我有一个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");
}

除了运行Android 7的Nexus 5X之外,这在所有设备上运行良好。此处,长按会触发上下文菜单和onChildClick的选择,这显然不是我想要的。

如何在使用自定义对话框的同时阻止项目选择。

1 个答案:

答案 0 :(得分:0)

我可以提供当前的解决方案或解决方案,因为它通过用我的自定义对话框替换上下文菜单来修补我破坏的东西。想法是在启动删除处理时将选择处理静音,并在对话框的回调中取消静音。

这样可行,但我不想在一开始就打破它。所以可能有更好的方法。

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;
    }
}