I followed a tutorial for animating recyclerview item but the animation is not working iam confused that whether the animation is applied or not.
Activity.Java: The updated code was here i tried with the button click but i dont know how to call the animation method from the onBindHolder form the adapter
private void setupRecyclerView() {
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
final LayoutAnimationController controller =
AnimationUtils.loadLayoutAnimation(this, R.anim.layout_animation);
recyclerView.setLayoutAnimation(controller);
recyclerView.setAdapter(specialistListAdapter);
}
//placed a dummy button onclick to check animation working or not
public void reloadData(View view) {
runLayoutAnimation(recyclerView);
}
private void runLayoutAnimation(final RecyclerView recyclerView) {
final Context context = recyclerView.getContext();
final LayoutAnimationController controller =
AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation);
recyclerView.setLayoutAnimation(controller);
recyclerView.getAdapter().notifyDataSetChanged();
recyclerView.scheduleLayoutAnimation();
}
layout_animation.xml:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/item_list_animation"
android:delay="15%"
android:animationOrder="normal"
/>
item_list_animation.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="-20%"
android:toYDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
/>
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:interpolator="@android:anim/decelerate_interpolator"
/>
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:toXScale="100%"
android:toYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/decelerate_interpolator"
/>
</set>
Thanks in advance
I am late in answering this question but I was facing the same issue while following the same tutorial and after trying a bundle of different things I realized that I didn't added the attribute: android:duration
in the set tag
in item_list_animation.xml file which is same as your case. So the item_list_animation.xml XML becomes now:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime">
<translate
android:fromYDelta="-20%"
android:toYDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator" />
<alpha android:fromAlpha="0"
android:toAlpha="1"
android:interpolator="@android:anim/decelerate_interpolator" />
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:toXScale="100%"
android:toYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/decelerate_interpolator"
/>
</set>
Note: on changing the dataset you need to reapply the animation on the RecyclerView because calling notifyDataSetChanged()
at any time will cancel the animation. You can reapply the animation by simply calling:
adapter.notifyDataSetChanged();
recyclerView.scheduleLayoutAnimation();
If you're using LiveData
& ViewModel
(from Android Architecture Components) make sure you calling recyclerView.setLayoutAnimation(controller);
in viewmodel.yourListLive().observe(){}.
Something like this:
vm.getGamesPageList().observe(this, games -> {
if (games == null)
return;
LayoutAnimationController animationController =
AnimationUtils.loadLayoutAnimation(v.getContext(), ANIM_RES_ID);
v.setLayoutAnimation(animationController);
adapter.submitList(games);
...
});
First of all you need to load the animation in the recycler view either in xml or programatically
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutAnimation="@anim/layout_animation"/>
OR
LayoutAnimationController animation =
AnimationUtils.loadLayoutAnimation(ctx, R.anim.layout_animation);
recyclerview.setLayoutAnimation(animation);
Also you need to call the method in OnBindViewHolder like runLayoutAnimation(YOUR_RECYCLER_VIEW)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With