gpt4 book ai didi

android - 如何在未显示上下文菜单时停止将长按作为短按处理

转载 作者:行者123 更新时间:2023-11-29 22:05:31 25 4
gpt4 key购买 nike

我有一个已注册上下文菜单的 ListView 。对于列表中的某些项目,上下文菜单不适用。在这些情况下,我只是不在 onCreateContextMenu 方法中扩充菜单。

不幸的是,这意味着当长按不显示上下文菜单的项目时,Android 会将其视为短按(大概是因为上下文菜单通常会返回 true,表示长按事件已被处理)。

这会导致 ListView 中的行为不一致 - 当您长按某些项目时,它们会显示上下文菜单 - 其他项目则不会,然后执行默认的点击行为。我如何确保即使不显示上下文菜单的项目也会消耗长按,以便不调用 onItemClick 方法?

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Playable playable = (Playable) info.targetView.getTag(R.id.playable);
if (playable != null && !(playable instanceof AutoRadioStation) && !(playable.getId().equals(Playlist.AUTOMATIC_PLAYLIST))) {
v.setTag(R.id.playable, playable); // This copies the tag so that it is contained within the view used for the menu.
Drawable stationImage = (Drawable) ((ImageView) info.targetView.findViewById(R.id.artwork)).getDrawable().getConstantState().newDrawable();

menu.setHeaderTitle(playable.getName());
menu.setHeaderIcon(stationImage);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.saved_context_menu, menu);
}
}

最佳答案

我有一个类似的问题,我最终使用了 Dialog而不是上下文菜单。

我的 Activity 实现 OnItemLongClickListener ,如果满足条件,我会从 onLongItemClick() 显示它。

关于android - 如何在未显示上下文菜单时停止将长按作为短按处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10850376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com