In my app design I need to change layout manager of recycler-view from linear-horizontal to grid layout manager
I need to make this transition smooth. Can anyone suggest me how can I make it possible.
In my app design I need to change layout manager of recycler-view from linear-horizontal to grid layout manager
I need to make this transition smooth. Can anyone suggest me how can I make it possible.
In order to animate the changing of layout manager you will need to apply a layout-animation on the RecyclerView
and for that you need to follow the steps:
1) Create an item animation file to animate the appearing of items
item_animation.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime">
<translate
android:fromYDelta="-30%"
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="115%"
android:fromYScale="115%"
android:toXScale="100%"
android:toYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/decelerate_interpolator"
/>
</set>
2) Then create an XML in anim folder for layout animation and apply it the item animation as shown:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/item_animation"
android:animationOrder="normal"
android:delay="15%" />
3) Now when you will change the Layout Manager(say from Grid to Linear layout) then simply set this animation to the RecyclerView in order animate the appearing of the items of the 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();
}
// Changing the layout manager followed by applying the animation
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
runLayoutAnimation(recyclerView);