How to remove all elements from RecyclerView with nice animation

I need to get a list view with extension and collapse animation as a list. I have a method using this, but it does not work in RecyclerView mode. The whole RecyclerView will become visible in a short time, and the animation will begin after that. Refer to the GIF in this link .

Alternatively, I follow this post and add and remove all elements (it will always have less than 5 elements, so performance will not be a question) using
LayoutAnimation achieved as follows.

enter image description here

As you can see, the list item in the list shows the animation. To hide an element, change the properties in the animation file as shown below and apply it after deleting all the data from the RecyclerView

item_remove_animation_drop_down.xml

 <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@integer/anim_duration_medium"> <translate android:fromYDelta="0" android:toYDelta="-20%" android:interpolator="@android:anim/decelerate_interpolator" /> <alpha android:fromAlpha="1" android:toAlpha="0" android:interpolator="@android:anim/decelerate_interpolator" /> <scale android:fromXScale="100%" android:fromYScale="100%" android:toXScale="95%" android:toYScale="95%" android:pivotX="50%" android:pivotY="50%" android:interpolator="@android:anim/decelerate_interpolator" /> </set> 

layout_animation_fall_down.xml

 <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:animation="@anim/item_add_animation_fall_down" android:delay="15%" android:animationOrder="normal" /> 

Method in adapter

 public void runItemRemoveAnimation(final RecyclerView recyclerView) { final LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_drop_down); recyclerView.setLayoutAnimation(controller); recyclerView.getAdapter().notifyDataSetChanged(); recyclerView.scheduleLayoutAnimation(); } 

Code in snippet

 private void onClickSelectPromotionContainer() { selectPromotionContainer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (discountListExpanded) { promoListData.removeAll(promosDataFromServer); discountListAdapter.runItemRemoveAnimation(discountRecyclerView); selectPromotionImage.startAnimation(rotatePlus180); discountListExpanded = false; } else { discountRecyclerView.setVisibility(View.VISIBLE); promoListData.addAll(promosDataFromServer); discountListAdapter.runItemAddAnimation(discountRecyclerView); selectPromotionImage.startAnimation(rotateMinus180); discountListExpanded = true; } } }); } 

But he did not create animations. I think because it deletes all the data that I think. So, is there a way to add a nice animation while cleaning the list, which can be used to convince the user that the list is hiding? Or am I doing something wrong in my code? It should be the other way around from adding an add animation so that it is enjoyable and should be a vertical animation.

+5
source share

Source: https://habr.com/ru/post/1276180/


All Articles