Skip to content

Instantly share code, notes, and snippets.

@patrick-elmquist
Last active May 5, 2021 11:44
Show Gist options
  • Select an option

  • Save patrick-elmquist/3ee57efed2f871e60d8723aabf7bd1ef to your computer and use it in GitHub Desktop.

Select an option

Save patrick-elmquist/3ee57efed2f871e60d8723aabf7bd1ef to your computer and use it in GitHub Desktop.
Enter animation demo: Re run LayoutAnimation
private void runLayoutAnimation(final RecyclerView recyclerView) {
final Context context = recyclerView.getContext();
final LayoutAnimationController controller =
AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_fall_down);
recyclerView.setLayoutAnimation(controller);
recyclerView.getAdapter().notifyDataSetChanged();
recyclerView.scheduleLayoutAnimation();
}
@sanjay-lakshmanan

Copy link
Copy Markdown

Can you provide the kotlin version of this code. I am facing problem while passing the context

@patrick-elmquist

patrick-elmquist commented May 5, 2021

Copy link
Copy Markdown
Author

Hi, the demo code at Github has actually been re-written in Kotlin as of Part 3 of the tutorial, so you could check out ListDemoFragment for some ideas:
https://github.com/patrick-elmquist/Demo-RecyclerViewEnterAnimation/blob/master/app/src/main/java/com/patrickiv/demo/enteranimationdemo/fragment/ListDemoFragment.kt#L70

Doing it now, I would probably add an extension function instead, like this:

private fun RecyclerView.runLayoutAnimation() {
    val animation = R.anim.layout_animation_fall_down
    layoutAnimation = AnimationUtils.loadLayoutAnimation(context, animation)
    adapter?.notifyDataSetChanged()
    scheduleLayoutAnimation()
}

and invoke it like recyclerView.runLayoutAnimation().

Gonna update the blog posts with Kotlin as well when I get the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment