Skip to content

Instantly share code, notes, and snippets.

@arriolac
Last active July 14, 2022 03:01
Show Gist options
  • Save arriolac/425011ff5c88378677af4c273541fdbf to your computer and use it in GitHub Desktop.
Save arriolac/425011ff5c88378677af4c273541fdbf to your computer and use it in GitHub Desktop.
Example usage of ComposeView in RV (Compose version 1.2.0-beta02+ and RecyclerView version 1.3.0-alpha02)
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
class MyComposeAdapter : RecyclerView.Adapter<MyComposeViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): MyComposeViewHolder {
return MyComposeViewHolder(ComposeView(parent.context))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind("$position")
}
// NOTE: No need to explicitly call .disposeComposition()
}
class MyComposeViewHolder(
val composeView: ComposeView
) : RecyclerView.ViewHolder(composeView) {
fun bind(input: String) {
composeView.setContent {
MdcTheme {
Text(input)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment