I was going to write this article on Jianshu, but because the page cannot support both rich text and markdown at the same time, I decided to give up Jianshu after seeing the cool and pleasing effects in Gemini's article. Reading articles is boring, and if there is no beautiful effect, wouldn't I have to fall asleep while reading? The Internet has given us so many choices, so I will definitely choose the best experience. The specific effects can be compared: Speaking of Gemini, I also came across it these two days when I was learning about NestedScrolling. I took a quick look at the information and article views. Great! My great god! Okay, that’s all for the previous part, let’s get to the point, NestedScrolling. Before I learned about NestedScrolling, I read some blogs, including Gemini's segmentfault. I didn't pay much attention when I read it at that time. Later I found that this blog was the clearest introduction to NestedScrolling. As a punishment or as a worship, I manually typed out the blog that I could have CVed over, and by the way, added some of my own additional understanding. Thanks again Gemini After Android released the Lillipop version, Google provided the NestedScrolling mechanism for Android's sliding mechanism for a better user experience. Where can the characteristics of NestedScrolling be reflected? For example, if you use a Toolbar and a ScrollView below, scroll up to hide the Toolbar, and scroll down to show the Toolbar. Logically, this is a NestedScrolling - because in the process of scrolling the View including the entire Toolbar, you also nested and scrolled the ScrollView inside. As shown in the figure: Before this, we know that Android has its own mechanism for distributing touch events. There are mainly three functions: dispatchTouchEvent, onInterceptTouchEvent, onTouchEvent. This distribution mechanism has a loophole: If the child view gets the opportunity to handle the touch event, the parent view will no longer have the opportunity to handle the touch event until the next finger triggers. That is to say, when we slide the child view, if the child view does not need to handle the sliding event, it can only discard the touch event and will not pass it to the parent view for processing. But Google's new NestedScrolling mechanism solves this problem very well. There are four main classes in NestedScrolling that need attention:
The above four classes are all provided in the support-v4 package. Some Views in Lollipop implement NestedScrollingChild or NestedScrollingParent by default. In the v4 package, NestedScrollView implements both NestedScrollingChild and NestedScrollingParent. Generally, it is enough to implement NestedScrollingChild, and the parent View can use the CoordinatorLayout that implements NestedScrollingParent provided by support-design.
Simple logic can achieve nested sliding. The above interfaces are all called by the business logic. How is NestedScrollingChildHelper implemented? Let's take a look at the startNestedScroll method first.
You can see here are some methods that help you implement interaction with NestedScrollingParent. ViewParentCompat is a compatibility class that interacts with the parent View. It determines the API version. If it is on Lollipop, it calls the method that comes with View. Otherwise, if NestedScrollingParent is implemented, it calls the method that implements the interface. The interaction process between child View and parent View is as follows: 1. startNestedScroll First, the child View needs to start the whole process (triggering a touch event by sliding the screen), and find and notify the onStartNestedScroll and onNestedScrollAccepted methods in the parent View that implements NestedScrollingParent through NestedScrollingChildHelper. 2. dispatchNestedPreScroll In the onIterceptTouchEvent and onTouch of the child View (usually in the MontionEvent.ACTION_MOVE event), call this method to notify the parent View of the sliding distance. The third and fourth parameters of this method return the scroll length consumed by the parent View and the window offset of the child View. If the scroll is not consumed, the child View handles the remaining distance. Since the window is moved, if the finger's initial position is recorded, the offset needs to be calculated based on the fourth parameter offsetInWindow to ensure that the calculation of the next touch event is correct. If the parent View accepts the scroll parameter and consumes part of it, the function returns true, otherwise it returns false. This function is usually called before the child View processes the Scroll. 3. dispatchNestedScroll Report the scrolling status to the parent View, including the values consumed and not consumed by the child View. If the parent View accepts the scroll parameters, the function returns true if partial consumption is done, otherwise it returns false. This function is usually called after the child View processes Scroll. 4. stopNestedScroll End the entire nested sliding process. The correspondence between NestedScrollingChild and NestedScrollingParent in the process is as follows:
Generally, the child View initiates the call and the parent View accepts the callback. You need to pay attention to the consumed parameter in dispatchNestedPreScroll:
This parameter is an int array with a length of 2. The first element is the scrolling distance consumed by the parent View in the x-axis direction, and the second element is the scrolling distance consumed by the parent View in the y-axis direction. If both values are not 0, it means that the parent View has consumed the scrolling distance, and the child View's scrolling distance needs to be corrected. Because of this parameter, the idea of handling scrolling events is clearer and will not be confused by a bunch of scrolling parameters as before. A brief flowchart of NestedScrolling that I understand (excluding the logic of Fling events and return values): |
<<: Android quickly implements WeChat payment
>>: 10 Key Steps to Turn Your Mobile App Idea into Reality
People often ask me how to add people. Here is a ...
An inventory of the top ten experiences of the ne...
Hello, this is Science Popularization China. When...
Why is the click-through rate of Baidu's bidd...
Training course video content introduction: All k...
Some time ago, the Russian Ministry of Defense an...
The new coronavirus epidemic is not over yet, and...
In the social media industry, “speed is everythin...
2016 is already halfway through, and the Oculus R...
Qian Sanqiang, a nuclear physicist, is the founde...
The River of Life exhibition area of the Shangh...
At present, we are still in the high season of re...
There is less than a week left before WWDC, and A...
On July 8, Bilibili officially announced the laun...
On June 8, 2016, Qutoutiao 1.0 was launched. On A...