How to add recyclerview item(s) remove animation

15,652

Solution 1

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>

Solution 2

You shouldn't be using both notifyItemRemoved() and notifyItemRangeRemoved(). Only use one at a time.

If you want to remove one item:

notificationItems.remove(index);
adapterForNotification.notifyItemRemoved(index);

If you want to remove all items:

int origCount = notificationItems.size();
notificationItems.clear();
adapterForNotification.notifyItemRangeRemoved(0, origCount - 1);

If you want to remove a range of items:

notificationItems.subList(startIndex, endIndex).clear();
adapterForNotification.notifyItemRangeRemoved(startIndex, endIndex);

EDIT:

If you want to remove each item one by one and show the removal animation for each, try this:

for (int i = 0; i < notificationItems.size(); i++) {
    notificationItems.remove(i);
    adapterForNotification.notifyItemRemoved(i);
}
Share:
15,652
Elsen Almasli
Author by

Elsen Almasli

Updated on July 19, 2022

Comments

  • Elsen Almasli
    Elsen Almasli almost 2 years

    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)
    }
    
  • Elsen Almasli
    Elsen Almasli over 5 years
    I read when use notifyDataSetChanged animation disapear. and I apply this also animation disapear. What can i do for this?
  • TheWanderer
    TheWanderer over 5 years
    Try with notifyItemRangeRemoved(0, notificationItems.size() - 1).
  • Elsen Almasli
    Elsen Almasli over 5 years
    Aplication terminated
  • TheWanderer
    TheWanderer over 5 years
    Share the stacktrace.
  • Elsen Almasli
    Elsen Almasli over 5 years
    java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state:7 android.support.v7.widget.RecyclerView{425ba5d VFED..... .F....ID 16,0-696,1084 #7f0a012d app:id/notification_recyclerview}, adapter:Adapters.RecyclerViewAdapterForNotification@db406ca, layout:android.support.v7.widget.LinearLayoutManager@9f4a03b‌​, context:********.NotificationActivity@557b5b1 at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHo‌​lderForPositionByDea‌​dline(RecyclerView.j‌​ava:5817)
  • Elsen Almasli
    Elsen Almasli over 5 years
    I try. it clear all, myfriend. but there isn't animation
  • TheWanderer
    TheWanderer over 5 years
    Honestly, I'm not really sure what you want. When you clear the entire RecyclerView, there isn't anything to animate anymore. The default removal animation is to hide the removed item and then move the following items up to fill the empty space. If the list is empty, there are no subsequent items. Do you want to remove each item individually in quick succession?
  • Elsen Almasli
    Elsen Almasli over 5 years
    yes I want when I click remove button each item remove invidual animation