How to add recyclerview item(s) remove animation

Elsen Almasli picture Elsen Almasli · Nov 8, 2018 · Viewed 12.1k times · Source

When I use this, it removes one element with animation

{
    notificationItems.remove(0);
    adapterForNotification.notifyItemRemoved(0);                        
    adapterForNotification.notifyItemRangeRemoved(0,count-1);
}

But, when I use this, it removes all element without animation

count = adapter.getItemCount();
for(int i = 0 ; i < count; ++i){
    notificationItems.remove(0);
    adapterForNotification.notifyItemRemoved(0);
    adapterForNotification.notifyItemRangeRemoved(0,count-1)
}

Answer

Zain picture Zain · Jan 25, 2019

As I can understand, you are able to remove items, but you need to add sort of animation while removing; please take a look here

and will post here again if someone drop by here

It's old, but wish this helps someone else as it's already not answered yet; I have done it by deleting a single item at a time by simulating a swipe animation on this item, and post a delay before deleting the next item, and so on to the way down to the last item of the RecyclerView

Step No.1:

In your activity that holds the clear all button and the RecyclerView instance: Create a method of single item delete

private void deleteItem(View rowView, final int position) {

    Animation anim = AnimationUtils.loadAnimation(requireContext(),
            android.R.anim.slide_out_right);
    anim.setDuration(300);
    rowView.startAnimation(anim);

    new Handler().postDelayed(new Runnable() {
        public void run() {
            if (myDataSource.size() == 0) {
                addEmptyView(); // adding empty view instead of the RecyclerView
                return;
            }
            myDataSource.remove(position); //Remove the current content from the array
            myRVAdapter.notifyDataSetChanged(); //Refresh list
        }

    }, anim.getDuration());
}

Step No.2:

Create the method that will delete all RecyclerView list items >> call it in your button click callback.

boolean mStopHandler = false;

private void deleteAllItems() {

    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {

            if (myDataSource.size() == 0) {
                mStopHandler = true;
            }

            if (!mStopHandler) {
                View v = myRecyclerView.findViewHolderForAdapterPosition(0).itemView;
                deleteItem(v, 0);
            } else {
                handler.removeCallbacksAndMessages(null);
            }

            handler.postDelayed(this, 250);
        }
    };
    requireActivity().runOnUiThread(runnable);
}

Also it's important to handle configuration change in manifest, activity section, as if the configuration changes while clearing your recycler view list, an exception will be raised

<activity
    android:name=".activities.MainActivity"
    android:configChanges="orientation|screenSize|keyboard"
    android:label="@string/app_name"
    ...
</activity>