Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView with CardView Items, notifyItemChanged and shadow problem

Is there any way to disable the CardView elevation animation from a recyclerview when an item is changed but to keep the original shadow and item animator?

I've tried to disable the CardView state animator android:stateListAnimator="@null" with no results.

Here's a way to reproduce the issue:

package com.w.cardviewrecyclerviewanim;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {

    RecyclerView items;
    Adapter adapter = new Adapter();
    GridLayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        items = findViewById(R.id.items);
        layoutManager = new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false);
        items.setLayoutManager(layoutManager);
        items.setAdapter(adapter);
    }

    class Holder extends RecyclerView.ViewHolder{
        CardView cardView;

        public Holder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.card_view);
        }

        void bind (){
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    adapter.notifyItemChanged(getAdapterPosition());
                }
            });
            cardView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    adapter.notifyItemRangeChanged(0, adapter.getItemCount());
                    return true;
                }
            });
        }
    }

    class Adapter extends RecyclerView.Adapter<Holder>{

        @NonNull
        @Override
        public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
            LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
            return new Holder(layoutInflater.inflate(R.layout.item, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(@NonNull Holder holder, int position) {
            holder.bind();
        }

        @Override
        public int getItemCount() {
            return 30;
        }
    }
}

And here's the item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="160dp"
    >

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"

        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"

        app:cardCornerRadius="8dp"
        app:cardElevation="5dp"
        android:stateListAnimator="@null"
        >

    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

I have a simple RecyclerViewer that has 30 items and each item has a CardView with 10dp margin and a cardElevation of 5dp just to have some nice shadow. When an item gets clicked the recyclerview adapter receives a notifyItemChanged(getAdapterPosition()) and on long click all items receives a change event via a notifyItemRangeChanged(0, adapter.getItemCount()) call to the adapter;

With the DefaultItemAnimator whenever an item gets "changed" the CardView shadow gets animated bigger during the transition animation. I want to keep the transition as I'm doing some updates in the item but I don't want to have the shadow/elevation animation. You can see the outcome here: https://gph.is/g/ajmNGen

Think that on long click the application moves into a "multi select" mode where all the items have big changes in layout and I want this change to be animated (by the DefaulItemAnimator) and I would like to keep the shadow as set originally.

Any help is really appreciated guys as I'm pulling my hair while dealing with this.

like image 626
Jean-Arthur Deda Avatar asked Oct 17 '25 02:10

Jean-Arthur Deda


1 Answers

Set a constraint layout inside cardView, then you can clearly see the shadow:-

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="6dp"
    tools:context=".MainActivity">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>
like image 191
Sandeep Malik Avatar answered Oct 18 '25 20:10

Sandeep Malik