Last active
July 14, 2022 03:01
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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