Sliding conflict resolution of Android listview

Sliding conflict resolution of Android listview

In the process of Android development, sometimes the child control and parent control have to slide, especially when the child control is listview. For example, there is a listview in a Scrollview. This kind of sliding conflict is common. This situation is also quite common. Sometimes it is like this. We can't help it. However, we know how to deal with such things by understanding the event distribution

There are two points to note:

Generally speaking, the ontouchevent of view returns true, that is, it consumes click events, and the onintercepttouchevent of ViewGroup returns false, that is, it does not intercept click events, which can be seen from the Android source code. However, the parent class abslistview of listview overrides onintercepttouchevent and returns true. Note that it does not necessarily return true here, but I think this can be ignored first.

The call order of ontouchevent and onintercepttouchevent. The click event is passed from the parent control to the child control. If the parent control does not intercept, it will be intercepted by the child control. If the parent control intercepts, it will be handled by the ontouchevent of the parent control. If the ontouchevent of the control that finally handles the click event returns false, it will directly call the ontouchevent of its parent control, and so on

In fact, the solution is also very simple: rewrite the onintercepttouchevent function of the parent control and return true as needed when moving. For example, sliding left and right returns true, and false in other cases. In this way, when sliding left and right, because onintercepttouchevent returns true, the parent control can handle it. In other cases, the event will be passed to listview, which can handle sliding up and down.

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>