我有一个已为上下文菜单注册的列表视图。对于列表中的某些项目,上下文菜单不适用。在这些情况下,我只是不在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);
}
}
答案 0 :(得分:1)
我遇到了类似的问题,最后我使用的是Dialog
而不是上下文菜单。
我的活动实施OnItemLongClickListener
,如果条件满足,我会从onLongItemClick()
展示。
答案 1 :(得分:1)
我终于开始实施一个版本的NathanZ解决方案了。关于将contextMenu转换为DialogFragment似乎没有太多东西,所以我会在这里粘贴我的大部分解决方案。
实现onLongItemClick侦听器也意味着我能够在listview中有一个不需要菜单的长按事件。不幸的是因为你无法将菜单传递给对话框,我不得不重复使用现有的ListViewElement类型来为listview中的每个“menu”项存储id和文本字符串。
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int item, long position) {
Playable playable = (Playable) view.getTag(R.id.playable);
//Switch haptic feedback off by default so if we don't handle the long click we don't vibrate
parent.setHapticFeedbackEnabled(false);
if (playable == null) {
// This must be a message bar so the only option is to update all saved content
updateAll();
parent.setHapticFeedbackEnabled(true);
} else {
if (!(playable instanceof AutoRadioStation) && !(playable.getId().equals(Playlist.AUTOMATIC_PLAYLIST))) {
Drawable drawable = (Drawable) ((ImageView) view.findViewById(R.id.artwork)).getDrawable().getConstantState().newDrawable();
showContextDialog(playable, drawable);
parent.setHapticFeedbackEnabled(true);
}
}
return true;
}
private void showContextDialog(Playable playable, Drawable drawable) {
FragmentManager fm = getActivity().getSupportFragmentManager();
final List<ListViewElement> array = new ArrayList<ListViewElement>();
array.add(new ListViewElement(R.id.menu_share, null, getString(R.string.share), true));
array.add(new ListViewElement(R.id.menu_delete, null, getString(R.string.delete), true));
ContextMenuDialog dialog = new ContextMenuDialog(drawable, playable.getName(), array, playable);
dialog.setOnItemClickListener(this);
dialog.show(fm, "Context Menu");
}
//Callback from the ContextMenuDialog class
@Override
public void onItemClickDialogFragment(int option, Playable playable) {
switch (option) {
case R.id.menu_delete :
// Perform delete actions
break;
case R.id.menu_share :
// Perform share actions
break;
}
}
public class ContextMenuDialog extends DialogFragment implements OnItemClickListener {
private Drawable drawableIcon;
private String title;
private List<ListViewElement> values;
private Playable playable;
private DialogFragmentOnItemClickListener listener;
public interface DialogFragmentOnItemClickListener {
void onItemClickDialogFragment(int option, Playable playable);
}
public void setOnItemClickListener(DialogFragmentOnItemClickListener listener) {
this.listener = listener;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create the dialog without a title since the layout includes a customized title
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialogStyle);
}
public ContextMenuDialog(Drawable drawableIcon, String title, List<ListViewElement> values, Playable playable) {
this.drawableIcon = drawableIcon;
this.title = title;
this.values = values;
this.playable = playable;
}
public ContextMenuDialog(int drawableResource, String title, List<ListViewElement> values, Playable playable) {
this.drawableIcon = getResources().getDrawable(drawableResource);
this.title = title;
this.values = values;
this.playable = playable;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.context_menu, container, true);
TextView titleView = (TextView) view.findViewById(R.id.context_menu_title);
titleView.setText(title);
ImageView icon = (ImageView) view.findViewById(R.id.context_menu_artwork);
icon.setImageDrawable(drawableIcon);
ListView listView = (ListView) view.findViewById(R.id.context_menu_listview);
ContextMenuAdapter adapter = new ContextMenuAdapter(getActivity(), R.layout.context_menu_list_item, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
return view;
}