Detailed: Android nested sliding mechanism (nestedscrolling)

Source: Internet
Author: User

Start with Android 5.0 Lollipop to provide a set of APIs to support embedded sliding effects. Forward compatibility is also provided in the latest support V4 package. With the embedded sliding mechanism, many very complex sliding effects can be achieved. The most coordinatorlayout component in the Android Design Support Library is the use of this mechanism to implement the Toolbar and unwind functions as shown in:

Nestedscrolling provides a set of parent view and child view sliding interaction mechanisms. To accomplish such an interaction, the parent view needs to implement the Nestedscrollingparent interface, and the child view needs to implement the Nestedscrollingchild interface.

Implement Nestedscrollingchild

First of all, Nestedscrollingchild. If you have a view that you can swipe and need to be used as a sub-View of the embedded slide, you must implement this interface. In this View, a Nestedscrollingchildhelper helper class is included. The implementation of the Nestedscrollingchild interface is basically called the corresponding function of the helper class, since the logic of the interaction between child and Parent has been implemented in the helper class. The logic of the original View's handling of the Touch event, and the implementation of sliding, generally does not need to change.

What needs to be done is that if you want to get ready to start sliding, you need to tell the Parent that you are ready to go into the sliding state and call Startnestedscroll (). Before you swipe, ask if your Parent needs to swipe, that is, call Dispatchnestedprescroll (). If the parent is sliding a certain distance, you need to recalculate the sliding distance margin left to you after the parent has slipped. Then, you make the rest of the slide yourself. Finally, if the sliding distance is still remaining, ask if the Parent needs to continue to slide your remaining distance, which is called Dispatchnestedscroll ().

Above are some basic principles, with the above fundamental ideas, you can refer to this article, which has a detailed analysis of the principle. If it is still unclear, here are the corresponding code to refer to.

Implement Nestedscrollingparent

As a parent View that can embed nestedscrollingchild, you need to implement Nestedscrollingparent, an interface method that has roughly one by one correspondence with Nestedscrollingchild. Also, there is a Nestedscrollingparenthelper helper class to silently help you implement the logic of interacting with child. The swipe action is initiated by the child, and the Parent will receive a swipe callback and respond.

From the child analysis above, we can see that the swipe starts with the call Startnestedscroll (), the Parent receives the Onstartnestedscroll () callback, decides whether it is necessary to work with the child to handle the slide, if necessary, Also callback onnestedscrollaccepted ().

Before each swipe, child first asks if the parent needs to slide, i.e. dispatchnestedprescroll (), which is called back to the parent's Onnestedprescroll (), and the parent can "hijack" the child in the callback. Slide, that is, before the child.

After the child has slipped, it calls Onnestedscroll (), callback to the parent's Onnestedscroll (), where the child slides, leaving the parent to handle, that is, after the child slips.

Finally, the swipe ends and the call to Onstopnestedscroll () indicates the end of this processing.

In fact, in addition to the above Scroll related calls and callbacks, there are Fling related calls and callbacks, the processing logic is basically the same.

Friendship recommendation:

    • Technology Blog: Programmer's Choice: several new open source tools in mobile development 0
    • Technology Blog: How to prevent apk decompile view source code? Love encryption protects developers ' ideas
    • Technology Blog: Love encryption Brief analysis: APK anti-compilation Tool summary

Detailed: Android nested sliding mechanism (nestedscrolling)

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.