开发者

Android: How to create a ContextMenu for a custom View

I'd like to show a ContextMenu for a custom View which is not part of an AdapterView.

I called

myActivity.registerForContextMenu(myView);

and the ContextMenu already shows up.开发者_StackOverflow社区

But I don't know how to access the data of myView in

@Override
onContextItemSelected(MenuItem item) //...

I thought I could create a custom menuInfo in

@Override
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) //...

This is from the docs: "menuInfo - Extra information about the item for which the context menu should be shown. This information will vary depending on the class of v."

Can anyone give me a hint?


Your custom view class should override getContextMenuInfo() and return a custom object implementing ContextMenuInfo (not very hard considering the interface has no members!) and which contains the extra per-view data you want the menu-click handler to get.

You can then get at this object from onContextItemSelected(MenuItem item) by calling item.getMenuInfo() and casting the value returned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜