English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Quick Solution to Android ListView Item Click Event Failure

During the normal development process, our ListView may not only display simple text or buttons, but more often display complex layouts. In this case, we need to write our own layouts and custom adapters, usually inheriting from BaseAdapter, with example code below. When writing the click event of ListView, if the onItemClick method in OnItemClickListener is not executed, the click event of the item in ListView will be disabled. The click event of the View in the item can be handled in the getView method. The main reason for the failure of the entire item click is often due to the existence of child controls such as ImageButton, Button, CheckBox, etc. in your custom item (which can also be said to be child controls of Button or Checkable subclasses). At this time, these child controls will capture the focus, so it is often the child controls that change when clicking the item, and the item itself does not respond to the click.

At this point, descendantFocusability can be used to solve this, where descendantFocusability corresponds to the following attributes:3item

This attribute defines the relationship between ViewGroup and its sub-controls when a view obtains the focus.

The value of the attribute has three kinds:

beforeDescendants: ViewGroup will prioritize its subclass controls and obtain the focus

afterDescendants: ViewGroup only gets the focus when its subclass controls do not need to obtain the focus

blocksDescendants: ViewGroup will override its subclass controls and directly obtain the focus

We usually use the third one, that is, add the attribute android:descendantFocusability="blocksDescendants" to the root layout of the Item layout.
In the situation I encountered, the item layout has an ImageButton button. Because this component has strong event capturing capabilities, it is almost the same as the root button; therefore, the listviewitem click fails. Use the above method to solve the problem; of course, you can also change the ImageButton to ImageView to solve this problem.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3When reporting via email, please replace # with @ and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like