Android onitemlongclick + oncreatecontextmenu implements long-pressed listitem to bring up different menus

Source: Internet
Author: User

a previous project requires an effect: For a listactivity, the menus popped up by different items are different. As shown in, we need to bring up menus with different titles by pressing different items:
For A Long press event, you can use the setonitemlongclick function of listview to associate the event. However, the listview display menu cannot be called in the onitemlongclick function of the response.
there are two menu types in Android: Options menu and context menu. (There are also three types: Options menu, context menu, and sub menu) Options menu is associated with activity, and context menu is associated with view. We need to click the menu that appears in the control to belong to the context menu. However, the context menu can only be associated through setoncreatecontextmenulistener.
to achieve this effect, you need to understand what happens after the listview item is long pressed:
first, the onitemlongclick function is triggered to execute the onitemlongclick operation, then, the onitemlongclick return value is used to determine whether the oncreatecontextmenulistener function is executed. If onitemlongclick returns false, the oncreatecontextmenulistener function is executed. Otherwise, true is returned and the oncreatecontextmenulistener function is ignored.
therefore, we can determine the onitemlongclick and prepare the data needed in oncreatecontextmenulistener, and then call the data display function in oncreatecontextmenulistener.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.