开发者_开发技巧I have a basic ListActivity that supports context menus via onCreateContextMenu() and onContextItemSelected(). If either of these methods fail and the context menu is not generated, the current default functionality seems to be to fire off onListItemClick(). TO me, if a user intentionally holds down their finger to get to the context menu, only to then have the item open, is very confusing and could lead them to simply abandoning this gesture in your app if it happens.
What I'd like to do is on failure fire off a Toast with a helpful message, then cancel the onListItemClick so that the list item doesn't open. I have the Toast working, but trying to figure out if it's possible to cancel the click action from inside on of onCreateContextMenu() or onContextItemSelected() where the error will occur.
Thanks!
So I have a solution to this that I am using and it seems to work fine. I created a boolean field, mIgnoreClick, and set that to false in onCreate(). Then in onCreateContextMenu(), if an error occurs generating the context menu, I set mIgnoreClick to true and send a Toast to the user alerting them that this functionality is currently unavailable. Finally, in onListItemClick(), I check the state of mIgnoreClick; if false, the item is displayed, if true, nothing is done and mIgnoreClick is set back to false so that the user can click on listitems and have them open as needed.
Thus, if the menu wasn't generated properly, I do nothing; they never intended to open the item, so this behavior meets my needs.
Paul
精彩评论