Unable to respond to the onItemClick () method in the onItemClickListener of ListView

Source: Internet
Author: User

In Android software design and implementation, we usually use the listview control. The system has some preset adapters that can be used, such as simpleadapter and arrayadapter, however, there are always some situations where we need to implement some effects through the custom listview. At this time, we usually encounter the situation that the custom listview cannot select the entire listviewitem, that is, the onitemclick () method in onitemclicklistener of listview cannot be responded to. Why? I have also checked a lot of information on the Internet before, but I have not found any valuable articles. Some of them are suggested to set controls that need to respond to click events in the getview method of the adapter. However, the final results are not very satisfactory, and I think this is a clever method and is not recommended. Then I checked the viewgroup source code and found the following constant declaration:

/*** This view will get focus before any of its descendants. */public static final int focus_before_descendants = 0 × 20000;/*** this view will get focus only if none of its descendants want it. */public static final int focus_after_descendants = 0 × 40000;/*** this view will block any of its descendants from getting focus, even * if they are focusable. */public static final int focus_block_descendants = 0 × 60000;

/*** This view will get focus before any of its descendants .*/

Public static final int focus_before_descendants = 0 × 20000;
/*** This view will get focus only if none of its descendants want it .*/

Public static final int focus_after_descendants = 0 ×40000;
/*** This view will block any of its descendants from getting focus, even * if they are focusable .*/

Public static final int focus_block_descendants = 0 × 60000;

We can see that the variable defined by a line of code means that "the current view will shield the focus status of all its child controls, even if these child controls can focus ", in fact, this variable indicates that the current view will not care whether its sub-control can focus itself to take over all the focuses. Usually, the control that can obtain the focus has a button by default, all controls inherited by checkable, which means that if your custom listviewitem contains a button or checkable subclass control, the default focus is handed over to the child control, the basis for selecting a listview item is that it can obtain the focus, that is, we can set the focusable attribute of all controls contained in the items in the listview to false, in this way, the listview item automatically obtains the focus permission and can be selected, and the onitemclick () method in onitemclicklistener is returned.
Setting the focusable attribute of the Child control of layout to false is cumbersome. You can set Android: descendantfocusability = "blocksdescendant" for the root control of item layout, in this way, item layout shields all child controls from obtaining the focus permission and does not need to re-set the focusable attribute for each control in item layout. In this way, you can smoothly respond to onitenclick () in onitemclicklistener () method

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.