Skip to content

Instantly share code, notes, and snippets.

@arriolac
Created June 27, 2022 20:48
Show Gist options
  • Save arriolac/7961d78646e951994f3cba1fb445dcea to your computer and use it in GitHub Desktop.
Save arriolac/7961d78646e951994f3cba1fb445dcea to your computer and use it in GitHub Desktop.
Example of using Compose in RecyclerView before pooling container was introduced.
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
import androidx.compose.ui.platform.ComposeView
class MyComposeAdapter : RecyclerView.Adapter<MyComposeViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): MyComposeViewHolder {
return MyComposeViewHolder(ComposeView(parent.context))
}
override fun onViewRecycled(holder: MyComposeViewHolder) {
// This is from the previous guidance
// NOTE: You **do not** want to do this with Compose 1.2.0-beta02+
// and RecyclerView 1.3.0-alpha02+
holder.composeView.disposeComposition()
}
/* Other methods */
}
class MyComposeViewHolder(
val composeView: ComposeView
) : RecyclerView.ViewHolder(composeView) {
/* ... */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment