Practical Android skills: the contextmenu of listview cannot be displayed.

Source: Internet
Author: User
Problem

Listview is used as the layout in the activity. When each list item contains a child view that can obtain the focus by default, it may affect some events of listview:

1. onitemclick
2. onitemlongclick
3. contextmenu

All three events cannot respond correctly.

For contextmenu, you must first register the activity contextmenu in oncreate to listview:

Registerforcontextmenu (mlistview );

To cancel registration in ondestroy:

Unregisterforcontextmenu (mlistview );

A simple solution is to add the corresponding event click or longclick to a specific item when creating each item in the list in the adapter. however, this will cause logic confusion. in addition, when you click item or long-pressed item, there will be no selection of focus. this phenomenon is very strange and must be concealed!

After Google, stackoverflow has a lot of questions about the listview contextmenu. The first thing to note is the basic usage, that is, the previously mentioned registerforcontextmenu. this is where strange events occur:

If the list item of listview contains a child view (such as checkbox) with focus by default, contextmenu cannot be obtained.

In this case, you need to change the focus attribute of the checkbox:

Android: focusable = "false"

Then you can.

In fact, contextmenu is also a long press event, so it is not only a contextmenu, but also an onitemclick and onitemlongclick! If the Android: focusable attribute of checkbox in item is set to false, these problems can be solved.

However, no one can explain why the sauce is purple!

After reading the source code of listview, listview does not control the click, longclick, and contextmenu (contextmenu is also triggered by the long press event). These events are managed by its parent class abslistview.
In abslistview, there is a runnable used to check longclick, which has the following judgment:

View v  = getChildAt(position);if (v != null && !v.hasFocusable()) {      .... do long click handling which will trigger context menu.}

That is to say, when you press an item for a long time, the hasfocusable () return value will be checked. If the return value is false, the long press will be performed. in other places, such as ontouchevent, this check will also be performed. If hasfocusable () is true, it will be returned directly! Note that the items in the list are checked. If the layout of the items in the list is different, the phenomenon may be different!
View. hasfocusable (). If the focusable of a view is true, true is returned, or true is returned when the focusable of a view has a child view is true. simply put, if the focusable of the view itself is true or the focusable of a sub-view is true, true is returned.
Because there is checkbox in listitem, its focusable attribute is true by default. therefore, no contextmenu is displayed. if you set the focusable attribute of the checkbox to false, it will pop up normally.
Can you still find out why you want to design soy sauce? Please give me some advice!
Extension

Currently, the default focusable attributes include button, compoundbutton, seekbar, edittext, imagebutton, autocompletetextview, webview, webtextview, and their subclasses. (How can we find them, to the system's Style File frameworks/base/CORE/RES/value/styles. in XML, the focusable attribute is "true ). theoretically, if you put these widgets in the listview, The onitemclick, onitemlongclick, and contextmenu of the listview will not work. the solution is to set their focusable attribute to false.

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.