I am trying to implement my own recyclerview Animation - I would like to achieve this without using any external libraries. Here is what the theoretical animation should look like.
The user clicks an item on the List and an animation occurs which opens up another View.
On a high level with minimal code, possibly just pseudo code what would the process be in order to create some animation like that?
Also I would like to note that the animation should be able to be done in reverse as well if the user clicks the same item or another item
I am not that familiar with the RecyclerView
class and would like to learn more about it and any animations associated with it.
Solution:
The way I solved this problem was to implement a listener
View.OnClickListener
to theViewHolder
class whichextends RecyclerView.ViewHolder
. So we get the following code:The variables
originalHeight
andisViewExpanded
are used in the animation process. In the constructor I initialize the view to theView.OnClickListener
like so:Now that the constructor has been taken care of we want to configure what happens when the user clicks on an individual
RecyclerView
item. The classes that will be useful here would be theValueAnimator
and theAnimation
objects. We override theonClick
method like so to accomplish this:Now when you touch an individual cardview on the
RecyclerView
(assuming you have aCardView
setup then it should expand out. Make sure to declare your customView properly in your xml file (example if you want theCardView
to expand down when you touch it then properly assign the customView underneath the other views and set the visibility to gone when you declare it and then when the animation starts like so in the code above then set the visibility to Visible and enable the view.Hope this can help someone out.
An easier alternative for @AndyRoid's answer is to use
android:animateLayoutChanges="true"
property. This way you don't need to write any animation code; however, this is not a way to go if you need to have a control over animation.You still need to create an
OnClickListener
:Attach it to every
ViewHolder
:Don't forget to collapse views when binding a new item:
view_holder_layout.xml: