Skip to content

Instantly share code, notes, and snippets.

@arriolac
Created June 27, 2022 22:17
Show Gist options
  • Save arriolac/f76041a8493b7032b63d83ff21baec42 to your computer and use it in GitHub Desktop.
Save arriolac/f76041a8493b7032b63d83ff21baec42 to your computer and use it in GitHub Desktop.
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
class ComposeItemRow @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : AbstractComposeView(context, attrs, defStyle) {
// …
// State hoist animatable object so that it can be explicitly stopped
val animatable = Animatable(Color.Gray)
@Composable
override fun Content() {
key(index) {
ItemRow(index, animatable)
}
}
}
class MainAdapter : RecyclerView.Adapter<MainAdapter.ViewHolder>() {
// …
override fun onViewDetachedFromWindow(holder: ViewHolder) {
super.onViewDetachedFromWindow(holder)
// Stop the animation when the view gets detached
viewLifecycleOwner.lifecycleScope.launch {
holder.itemRow.animatable.stop()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment