original text :Layout animations on RecyclerView
translate : stay RecyclerView Use layout animation on (Layout animation) by Days spent online
This paper can be combined with Android LayoutAnimation Use and expansion Read together .
Automatically Material Design Since its appearance , I was surprised by the mesh spread animation demonstrated in some videos . This is a diagonal animation , Give Way activity Spread out from top to bottom, from left to right . Very beautiful .
I've been trying all the ways to get that effect . One way is , Use RecyclerView::notifyItemInserted() Method , This is the method mentioned by many people . However, this method does not provide many methods to control the animation sequence , So it doesn't seem like a good way . The other is in onBind() Use animation for each element when necessary , This is indeed feasible . But then the code is fragile and too intrusive ( We are in adapter Animation added in ). It's hard to make it work properly .
Layout animation
Last , The solution is actually simpler than expected . I have to admit that I seldom use Layout animation (layout animation), So I didn't think of this immediately . But in the process of finding the answer , I found this great code : gist from Musenkishi , It showed me the solution . The problem here is RecyclerView It's not used by default layout animation, But this code can make it look like GridView Use like that GridLayoutAnimation. As we mentioned gist That's true :
|
|
Configure layout animation
The good thing about layout animation is that we can use xml To define and deploy them , Therefore, our code will not be interspersed by animation . We just need the corresponding layout animation definition xml.
|
|
We can customize the animation according to our preferences :
- columnDelay / rowDelay: Percentage of delay time of row and column elements in animation . So we can get the next row to the next column view One animation after another , Instead of animating together .
- animation: view: The animation that appears on the screen , I use an animation that slides out from the bottom .
- animationOrder: It can be normal, reverse perhaps random.
- direction: Appoint item how Display based on column delay , Value for :top_to_bottom, left_to_right,bottom_to_top,right_to_left.
Here is slide The animation xml Code :
|
|
Adjust the timing of the animation
If you execute the current code , You'll find that app While opening, the layout animation is also executing , So you don't actually see any effect . about Lollipop There's nothing you can do with the previous equipment , There is no effective way to know when the entry animation is completed ( At least I don't know ). But from Lollipop Start , We can use onEnterAnimationComplete To check . So in onCreate in , If SDK Version older than Lollipop,RecyclerView Settle directly :
|
|
stay Lollipop Or update the device ,onEnterAnimationComplete Will be called . This is settled RecyclerView With the timing of requesting a new layout animation :
|
|
summary
You can easily adjust this layout animation to produce other entry animations . You can try to animate and see what you can get .
The code of this example is in Github Of Materialize your App repository .
More information