android – RecyclerView Animation on Item Click-ThrowExceptions

Exception or error:

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.

enter image description here

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.

How to solve:


The way I solved this problem was to implement a listener View.OnClickListener to the ViewHolder class which extends RecyclerView.ViewHolder. So we get the following code:

public static class ExampleViewHolder extends RecyclerView.ViewHolder 
    implements View.OnClickListener {

    private int originalHeight = 0;
    private boolean isViewExpanded = false;
    private YourCustomView yourCustomView

    // ..... CODE ..... //


The variables originalHeight and isViewExpanded are used in the animation process. In the constructor I initialize the view to the View.OnClickListener like so:

public ExampleViewHolder(View v) {

     // Initialize other views, like TextView, ImageView, etc. here

     // If isViewExpanded == false then set the visibility 
     // of whatever will be in the expanded to GONE

     if (isViewExpanded == false) {
         // Set Views to View.GONE and .setEnabled(false)


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 the ValueAnimator and the Animation objects. We override the onClick method like so to accomplish this:

public void onClick(final View view) {
    // If the originalHeight is 0 then find the height of the View being used 
    // This would be the height of the cardview
    if (originalHeight == 0) {
            originalHeight = view.getHeight();

    // Declare a ValueAnimator object
    ValueAnimator valueAnimator;
        if (!mIsViewExpanded) {
            mIsViewExpanded = true;
            valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight * 2.0)); // These values in this method can be changed to expand however much you like
        } else {
            mIsViewExpanded = false;
            valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight * 2.0), originalHeight);

            Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out

            // Set a listener to the animation and configure onAnimationEnd
            a.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationStart(Animation animation) {


                public void onAnimationEnd(Animation animation) {

                public void onAnimationRepeat(Animation animation) {


            // Set the animation on the custom view
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer value = (Integer) animation.getAnimatedValue();
                view.getLayoutParams().height = value.intValue();



Now when you touch an individual cardview on the RecyclerView (assuming you have a CardView setup then it should expand out. Make sure to declare your customView properly in your xml file (example if you want the CardView 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:

class CardTapListener implements View.OnClickListener {
    public void onClick(View v) {
        View someView = v.findViewById(;

        if (someView.getVisibility() == View.GONE) {
        else if (someView.getVisibility() == View.VISIBLE){


Attach it to every ViewHolder:

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_layout, viewGroup, false);
    v.setOnClickListener(new CardTapListener());
    return new ItemViewHolder(v);

Don’t forget to collapse views when binding a new item:

public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
    // Collapse (probably opened by user previously) view
    ItemViewHolder itemHolder = (ItemViewHolder) viewHolder;





        android:id="@+id/view_to_expand" />


Leave a Reply

Your email address will not be published. Required fields are marked *