首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android 7设备上,Long-Click触发Listview的上下文菜单和选择

在Android 7设备上,Long-Click触发Listview的上下文菜单和选择
EN

Stack Overflow用户
提问于 2016-10-01 17:14:44
回答 1查看 164关注 0票数 0

我有一个ExpandableListView,我为它实现了选择(短击)和删除(长击)。短列表项单击由onChildClick()处理,长单击由onCreateContextMenu()处理。

代码语言:javascript
复制
@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);
}

上面显示了上下文菜单代码,它很好地处理了长单击。问题是缺乏风格,它截断了一些设备上较长的标题。因此,我使用了一个自定义对话框,而不是标准上下文菜单,如下所示:

代码语言:javascript
复制
@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触发上下文菜单和选择,这显然不是我想要的。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-01 18:55:55

我可以提供我目前的解决方案或解决办法,感觉不太理想,因为它修补了一些我打破的东西,用我的自定义对话框替换上下文菜单。思想是在删除处理启动时静音选择处理,并在对话框的回调中解除其静音。

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

代码语言:javascript
复制
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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39809411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档