【问题标题】:Long-Click Triggers both Listview's Context Menu and Selection on Android 7 Device长按在 Android 7 设备上触发 Listview 的上下文菜单和选择
【发布时间】:2016-10-01 17:14:44
【问题描述】:

我有一个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 进行的选择,这显然不是我想要的。

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

【问题讨论】:

  • 当然,在上下文菜单处理事件时,可以使用一个标志来使选择静音,但这似乎是在修补其他出错的地方。

标签: android expandablelistview android-contextmenu


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-10
    • 2010-12-16
    • 1970-01-01
    • 2014-05-04
    • 2022-11-13
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    相关资源
    最近更新 更多