How to add recyclerview item(s) remove animation
Asked Answered
J

4

12

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)
}
Jacklighter answered 8/11, 2018 at 13:23 Comment(0)
A
15

As I can understand, you are able to remove items, but you need to add sort of animation while removing.

It can be done by deleting a single item at a time with a single animation for each item.

For instance by simulating a swipe animation on an item at a time, and post a delay before deleting the next item, and so on to the way down to the last item of the RecyclerView

Steps:

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>

Ake answered 25/1, 2019 at 22:33 Comment(1)
Hint, instead of myRVAdapter.notifyDataSetChanged() we'd use the UI lighter myRVAdapter.notifyItemRemoved(position)Ake
R
4

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);
}
Rashidarashidi answered 8/11, 2018 at 13:31 Comment(8)
I read when use notifyDataSetChanged animation disapear. and I apply this also animation disapear. What can i do for this?Jacklighter
Try with notifyItemRangeRemoved(0, notificationItems.size() - 1).Rashidarashidi
Aplication terminatedJacklighter
Share the stacktrace.Rashidarashidi
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.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5817)Jacklighter
I try. it clear all, myfriend. but there isn't animationJacklighter
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?Rashidarashidi
yes I want when I click remove button each item remove invidual animationJacklighter
M
0
count = adapter.getItemCount();

for (int i = 0 ; i < count; ++i){
    notificationItems.remove(0);
}
adapterForNotification.notifyItemRangeRemoved(0, count-1)
Mabel answered 8/11, 2018 at 13:54 Comment(0)
R
0

I did this by accessing the views of the list items directly from the adapater of the recyclerview, animating them, and notifying the adapter after the last animation has played.

fun deleteMultipleAnimated(){
    val oldSize = myList.size
    
    myList.removeAt(3)
    myList.removeAt(4)

    val newSize = myList.size

    for(i in newSize until oldSize){

        val listItemView = myRecycler.findViewHolderForAdapterPosition(i) as MyAdapater.MyViewHolder            

        if(i == oldSize-1){ 

            //only the last animation notifies the adapter
            listItemView.myView.animate().scaleX(0f).setDuration(250).withEndAction{
                myAdapter.notifyItemRangeRemoved(newSize, oldSize)}
        }else{

            //every other view is animated without an end action
            listItemView.myView.animate().scaleX(0f).duration = 250
        }
    }
}    
Rodriques answered 16/8, 2020 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.