Source code analysis of Android drop-down refresh control swiperefreshlayout

Swiperefreshlayout is the official drop-down refresh control of Android. It is simple to use and has a beautiful interface. Unfamiliar friends can search and understand it at will. There is no nonsense here and go directly to the topic.

First of all, let's give a flow chart, marking the functions of several main methods, which can be combined for a look.

The principle of this pull-down refresh control is not difficult. It is basically to monitor the movement of the finger, obtain the coordinates of the finger, determine the operation through calculation, and then call back the corresponding interface. Swiperefreshlayout inherits from ViewGroup. According to Android's event distribution mechanism, touch events should be passed to ViewGroup first, and whether to intercept events is determined according to the return value of onintercepttouchevent. Then start with onintercepttouchevent:

There are many kinds of interception. If one of the five conditions is met, false will be returned directly. If the touch event conflicts during use, you can analyze it from here. It is not detailed here. Take a quick look at action_ Record the finger coordinates in down, action_ Calculate the moving distance in move and judge whether it is greater than the threshold. If yes, set the misbeingdragged flag bit to true and action_ In up, set misbeingdragged to false. The last one returned is misbeingdragged.

Swiperefreshlayout is generally used by nested scrollable views. During normal scrolling, the previous conditions will be met. At this time, it will not be intercepted. The judgment of subsequent actions will be entered only when scrolling to the top. During finger pressing and lifting, misbeingdragged is true, that is to say, intercept. The next step is how to handle it. Take a look at ontouchevent:

Some code is omitted here. The previous lines are similar to those above, and are returned directly when one of the conditions is met; There are also several lines in switch that deal with multi touch, which are omitted. Take a look at action_ The finger movement distance is calculated in move. At this time, the misbeingdragged should be true under normal circumstances. When the distance is greater than zero, the movespinner will be executed. In action_ Finishspinner will be executed in up. You can guess from here that the logic of refreshing is mainly in these two methods.

Before looking at these two methods, you should know two important member variables: one is mcircleview, which is an instance of circleimageview, which inherits ImageView and mainly draws the background of progress circle; The other is mprogress, which is an instance of materialprogressdrawable. It inherits from drawable and implements the animatable interface. It mainly draws progress circles. Swiperefreshlayout draws animation by calling its method. Next, let's take a look at movespinner:

Showarrow is the display arrow. The middle one is mainly the style of math and setting progress circle. Setprogressrotation is executed in the penultimate line, and a pile of calculated rotation is passed in. This pile of calculations is mainly the optimization effect. For example, it grows faster at the beginning of moving, and it grows slower after exceeding the refresh distance. After this method is passed in, mprogress will draw the progress circle according to it, so the main animation should be in this method. The last line executes settargetoffsettopandbottom. Let's take a look:

It is relatively simple to adjust the position of the progress circle and record it. Finally, let's take a look at finishspinner:

The logic is also very simple. When the moving distance exceeds the set value, setrefreshing (true, true) will be executed. After updating the values of some member variables in this method, animateoffsettocorrectposition will be executed. It is known from the name that the animation is executed to move the progress circle to the correct position (that is, the head). If the moving distance does not exceed the set value, animateoffsettostartposition will be executed. Let's take a look at the two methods animateoffsettocorrectposition and animateoffsettostartposition:

The logic is basically the same. After some settings are made, the startanimation of mcircleview will be executed in the end, but the passed in values and listeners are different.

If you want to perform a refresh operation, the passed in value is the head height, and the listener is:

After the animation is completed, that is, after the progress circle moves to the head, it will execute mprogress. Start(); What is executed here is the animation of the progress circle when refreshing. Next, notice that if the mlistener is not empty, the onrefresh method will be executed. This mlistener actually executes the listener set by setonrefreshlistener, so the refresh is completed here. If the operation is to return to the initial position, the value passed in is the initial height (i.e. above the top), and the listener is

After moving to the initial position, startscaledownanimation will be executed, that is, the disappeared animation. Here, the whole refresh process is over.

In this way, I have basically gone through the process of swiperefreshlayout, but there are still many small problems to consider in order to implement such a control. Here, I mainly clarify my ideas and know how to solve problems. In addition, it can be seen from the source code that the customization of swiperefreshlayout is relatively poor, and I don't know whether Google wants to use this unified style of drop-down refresh in the future.. Of course, some third-party pull-down refreshes have good customization and are not difficult to use. But some people (such as me) prefer to use official controls and don't want to use third-party tools as a last resort. Next time, I'll write an article about implementing custom styles with swiperefreshlayout~

In the follow-up, there is an article on modifying the source code of swiperefreshlayout to customize the pull-down refresh effect of high imitation wechat circle of friends. If you are interested, you can have a look https://www.oudahe.com/p/16765/

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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