我终于开始实施 NathanZ 解决方案的一个版本。似乎没有太多关于将 contextMenu 转换为 DialogFragment 的内容,因此我将在此处粘贴大部分解决方案。
实现 onLongItemClick 侦听器还意味着我能够拥有一个不需要列表视图中的菜单的长按事件。不幸的是,因为您不能将菜单传递给对话框,所以我不得不重用现有的 ListViewElement 类型来存储列表视图中每个“菜单”项的 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;
}