Quick solution to invalidation of item click event in listview in Android

In the normal development process, our listview may not only display text or buttons, but also display complex layouts. In this case, we have to write our own layouts and custom adapters, which are generally inherited from baseadapter. See the following for the example code. When writing the click event of the listview, the onitemclicklistener and onItemClick method are not executed, resulting in the invalidation of the item click event in the listview, and the view click event in the item can be processed in the getview method. Most of the reasons for the click invalidation of the whole item are due to the existence of items such as imagebutton and button in the item defined by yourself, Check@R_418_2419 @And other sub controls (also known as button or checkable sub controls)]. At this time, these sub controls will get the focus, so often when you click item, the sub controls change, and the click of item itself does not respond.

At this time, you can use descendantfocusability to solve the problem. There are three attributes corresponding to descendantfocusability

This property defines the relationship between the ViewGroup and its child controls when a gets the focus for the view.

Property has three values:

Beforedescendants: ViewGroup takes precedence over its subclass controls to get focus

Afterdescendants: ViewGroup gets focus only when its subclass control does not need to get focus

Blocksdecedents: ViewGroup overrides subclass controls to get focus directly

Generally, we use the third one, that is, add the attribute of Android: descendantfocusability = "blocksdecedents" to the root layout of the item layout. In my case, the item layout has an imagebutton button, because this component has strong event grabbing ability and the root button is not much worse; Therefore, after running, the listview with EM click fails. Use the above method to solve the problem; Of course, changing the imagebutton to ImageView can also solve this problem.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>