下面列出了怎么用android.widget.ExpandableListView.ExpandableListContextMenuInfo的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v,
ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, 0, Menu.NONE, R.string.context_action_shortcut);
menu.add(Menu.NONE, 1, Menu.NONE, R.string.context_action_launch);
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
ExpandableListView list = getView().findViewById(R.id.expandableListView1);
switch (ExpandableListView.getPackedPositionType(info.packedPosition)) {
case ExpandableListView.PACKED_POSITION_TYPE_CHILD:
MyActivityInfo activity = (MyActivityInfo) list.getExpandableListAdapter().getChild(ExpandableListView.getPackedPositionGroup(info.packedPosition), ExpandableListView.getPackedPositionChild(info.packedPosition));
menu.setHeaderIcon(activity.icon);
menu.setHeaderTitle(activity.name);
menu.add(Menu.NONE, 2, Menu.NONE, R.string.context_action_edit);
break;
case ExpandableListView.PACKED_POSITION_TYPE_GROUP:
MyPackageInfo pack = (MyPackageInfo) list.getExpandableListAdapter().getGroup(ExpandableListView.getPackedPositionGroup(info.packedPosition));
menu.setHeaderIcon(pack.icon);
menu.setHeaderTitle(pack.name);
break;
}
super.onCreateContextMenu(menu, v, menuInfo);
}
/**
* {@inheritDoc}
*
* @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
*/
@Override
public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenuInfo menuInfo) {
if (menuInfo instanceof ExpandableListContextMenuInfo) {
final ExpandableListContextMenuInfo cmi = (ExpandableListContextMenuInfo) menuInfo;
final int type = ExpandableListView.getPackedPositionType(cmi.packedPosition);
final int groupPosition = ExpandableListView.getPackedPositionGroup(cmi.packedPosition);
final int childPosition = ExpandableListView.getPackedPositionChild(cmi.packedPosition);
// System.out.println("OPDSActivity.onCreateContextMenu(): " + type + ", " + groupPosition + ", "
// + childPosition);
switch (type) {
case ExpandableListView.PACKED_POSITION_TYPE_NULL:
onCreateContextMenu(menu);
return;
case ExpandableListView.PACKED_POSITION_TYPE_GROUP:
final Entry entry = getController().adapter.getGroup(groupPosition);
if (entry instanceof Feed) {
onCreateFeedContextMenu(menu, (Feed) entry);
} else if (entry instanceof Book) {
onCreateBookContextMenu(menu, (Book) entry);
}
return;
case ExpandableListView.PACKED_POSITION_TYPE_CHILD:
final Entry group = getController().adapter.getGroup(groupPosition);
final Object child = getController().adapter.getChild(groupPosition, childPosition);
if (child instanceof Link) {
onCreateLinkContextMenu(menu, (Book) group, (Link) child);
} else if (child instanceof Feed) {
onCreateFacetContextMenu(menu, (Feed) group, (Feed) child);
}
return;
}
}
onCreateContextMenu(menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
ExpandableListView list = getView().findViewById(R.id.expandableListView1);
switch (ExpandableListView.getPackedPositionType(info.packedPosition)) {
case ExpandableListView.PACKED_POSITION_TYPE_CHILD:
MyActivityInfo activity = (MyActivityInfo) list.getExpandableListAdapter().getChild(ExpandableListView.getPackedPositionGroup(info.packedPosition), ExpandableListView.getPackedPositionChild(info.packedPosition));
switch (item.getItemId()) {
case 0:
LauncherIconCreator.createLauncherIcon(getActivity(), activity);
break;
case 1:
LauncherIconCreator.launchActivity(getActivity(), activity.component_name);
break;
case 2:
DialogFragment dialog = new ShortcutEditDialogFragment();
Bundle args = new Bundle();
args.putParcelable("activity", activity.component_name);
dialog.setArguments(args);
dialog.show(getFragmentManager(), "ShortcutEditor");
break;
}
break;
case ExpandableListView.PACKED_POSITION_TYPE_GROUP:
MyPackageInfo pack = (MyPackageInfo) list.getExpandableListAdapter().getGroup(ExpandableListView.getPackedPositionGroup(info.packedPosition));
switch (item.getItemId()) {
case 0:
boolean success = LauncherIconCreator.createLauncherIcon(getActivity(), pack);
Toast.makeText(getActivity(), getString(R.string.error_no_default_activity), Toast.LENGTH_LONG).show();
break;
case 1:
PackageManager pm = getActivity().getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(pack.package_name);
if (intent != null) {
Toast.makeText(getActivity(), String.format(getText(R.string.starting_application).toString(), pack.name), Toast.LENGTH_LONG).show();
getActivity().startActivity(intent);
} else {
Toast.makeText(getActivity(), getString(R.string.error_no_default_activity), Toast.LENGTH_LONG).show();
}
break;
}
}
return super.onContextItemSelected(item);
}