Android recyclerview’s item custom animation and defaultitemanimator source code analysis

This is the second article about recyclerview, which talks about how to customize the item animation, but please note that this article does not include the specific implementation method of animation, but just tells you how to customize the animation and how to refer to the source code.

We know that recyclerview will use defaultitemanimator by default, so if we need to customize animation, we should read the source code of this class well, so as to learn not only how to customize, but also the design mode of Android.

First, understand one thing. Defaultitemanimator inherits from simpleitemanimator, and simpleitemanimator inherits from recyclerview.itemanimator. Therefore, if you need to customize the animation, the easiest way is to inherit simpleitemanimator. Secondly, there are four types of animation: add, remove, move and change. Here we only list remove and draw inferences from one instance.

Let's first look at the source code in simpleitemanimator. There are several important methods in simpleitemanimator:

Resolution: this function rewrites recyclerview.itemanimator. The parameters in the interface are viewholder, prelayoutinfo and postlayoutinfo respectively. The first parameter refers to the viewholder of the item, and its view can be obtained through the ItemView of the object. The second parameter refers to the location information before item deletion, and the third refers to the new location information. Next, we will judge whether the viewholder has been removed and whether the position has changed, and then call the abstract method animateremove. If we want to customize the animation, we need to implement it (callback idea).

Parsing: dispatchRemoveStaring is a final method, that is, it can not be rewritten. If we need to deal with some logic at the beginning of Remove, we need to call this method in animateRemove method. This method will execute a onRemoveStaring method, which allows us to rewrite, so the logic should be written in onRemoveStaring. When we call dispatchremovestarting, onremovestarting will be executed.

There are only two here, but there are more than two with other actions... Therefore, when we inherit simpleitemanimator, we need to implement some methods, generally as follows: ① animateremove (add, move and change): these methods will call back when animation occurs. Generally, the animation and properties of each item will be recorded in this method with a list ② endanimation Endanimations: call back when an item or multiple items need to be stopped immediately ③ isrunning: if smooth sliding is required, this method must be rewritten. In many cases, for example, when the network is loaded, sliding Caton is the method with wrong logic ④ run'pending animations: This is the most important method. Because animatedisappearence and other methods return the values returned by animateremove and other methods, and this method determines whether there is a prepared animation to play according to these values. If so, it will call back this method. In this method, we need to deal with the animation of each action (remove, add, move and change)

Therefore, our general steps are: ① create a subclass of simpleitemanimator; ② create an action list for each action; ③ override animateremove and other methods. When an action occurs in the interface, these functions will be called back. Here, record and return true to start the execution of run'pendinganimations; ④ override run'pendinganimations. When the method of ③ returns true, We think that animation needs to be executed. We need to write the logic of animation execution here ⑤ rewrite isrunning to provide animation playback status, generally returning whether the action list is empty ⑥ if necessary, rewrite endanimation, endanimations, onremovefinish and other methods. The specific steps are available, but we don't know how to build it. Don't worry, For our convenience, Google has actually provided defaultitemanimator. We can refer to some of its source code. No one makes more sense than the source code. What we need is enough patience! Some ArrayLists are defined in the defaultitemanimator to store action information, as follows:

Parsing: you can see that the animatorremove method directly adds the viewholder to the list, and then returns true

Analysis: according to the above, runpendinganimations will be executed. You can see that in this method, the action list is traversed, and each item is executed with the animatorremoveimpl method. The methods of other actions are omitted for the time being. Those interested can read them by themselves.

Parsing: you can see that the animatorRemoveImpl method implements the specific logic of the whole animation. How to do it is not in the scope of this article. After we executed the animation, that is, we call dispatchRemoveFinish in the onAnimatorEnd of Listener in animation. Do you still remember this method? It executes onRemoveFinish method. The onremovefinish method can be rewritten for us. Then remove the item from the action list.

Analysis: the isrunning method actually returns the result according to whether the action list is empty. There are other functions that can read the source code by themselves.

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