Skip to content

Instantly share code, notes, and snippets.

View arriolac's full-sized avatar
🌍

Chris Arriola arriolac

🌍
View GitHub Profile
@arriolac
arriolac / RvComposePrePoolingContainer.kt
Created June 27, 2022 20:48
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,
@arriolac
arriolac / RvComposePrePoolingContainerViewCompositionStrategy.kt
Last active July 14, 2022 18:32
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.ViewCompositionStrategy
class MyComposeViewHolder(
val composeView: ComposeView
) : RecyclerView.ViewHolder(composeView) {
init {
// 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) {
// …
@arriolac
arriolac / ItemRow.kt
Last active July 11, 2022 17:02
A Composable within an RV
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun ItemRow(index: Int, state: LazyListState) {
DisposableEffect(Unit) {
println("ItemRow $index composed")
onDispose { println("ItemRow $index DISPOSED") }
}
Column(Modifier.fillMaxWidth()) {
@arriolac
arriolac / ItemRowKey.kt
Created July 14, 2022 22:43
Using key in RV when there is remembered state
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun ItemRow(index: Int) {
Column(Modifier.fillMaxWidth()) {
Text("Row #${index + 1}", Modifier.padding(horizontal = 8.dp))
key(index) {
LazyRow {
// ...
@arriolac
arriolac / LazyListStateRv.kt
Created July 14, 2022 22:44
Using LazyListState to correctly remember state in an RV
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun ItemRow(index: Int, state: LazyListState) {
Column(Modifier.fillMaxWidth()) {
Text("Row #${index + 1}", Modifier.padding(horizontal = 8.dp))
LazyRow(state = state) {
// ...
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun SurveyAnswer(answer: Answer) {
Row {
Image(answer.image)
Text(answer.text)
RadioButton(false, onClick = { /* … */ })
}
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun SingleChoiceQuestion(answers: List<Answer>) {
Column {
answers.forEach { answer ->
SurveyAnswer(answer = answer)
}
}
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun SingleChoiceQuestion(answers: List<Answer>) {
Column {
if (answers.isEmpty()) {
Text("There are no answers to choose from!")
} else {
answers.forEach { answer ->
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun SingleChoiceQuestion(answers: List<Answer>) {
answers.forEach { answer ->
SurveyAnswer(
answer = answer,
isSelected = false,
)