Atul Gupta
05/10/2024, 11:48 AMn-1
item slightly up and down(n-1
item is partially visible at the bottom and doing up and down doesn’t insert or remove any visible items in the viewport) then nth
item reattaches at each scroll(event nth item is not visible in the UI as it is below n-1
item)
Attached the code and video in the stack-traceAtul Gupta
05/10/2024, 11:49 AMpackage com.example.composelistrecomposition
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.example.composelistrecomposition.ui.theme.ComposeListRecompositionTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeListRecompositionTheme {
Greeting()
}
}
}
}
private val randomList = buildList {
repeat(4) {
add(
if (it % 2 == 0) {
AChild1(
it,
"${it}_c1"
)
} else {
AChild2("${it}_c2")
}
)
if (it % 2 == 0) {
add(
AChild3("${it}_c3")
)
}
}
}
@Composable
private fun Greeting() {
LazyColumn(
modifier = Modifier.fillMaxSize(),
contentPadding = PaddingValues(vertical = 16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp, alignment = Alignment.Bottom),
) {
items(
items = randomList,
contentType = {
it.type
},
key = {
it.uId
}
) { item ->
AbstractLayout(item)
}
}
}
@Composable
private fun AbstractLayout(item: UiModel) {
when (item) {
is AChild1 -> {
Text(text = item.toString().repeat(20), modifier = Modifier.border(1.dp, Color.Red))
}
is AChild2 -> {
// put debug here
Column(modifier = Modifier.border(1.dp, Color.Blue)) {
val context = LocalContext.current
LaunchedEffect(key1 = Unit) {
Toast.makeText(context, "LaunchedEffect called for ${item.uId}", Toast.LENGTH_SHORT)
.show()
Log.d("ListTag", "LaunchedEffect called for ${item.uId}")
}
Text(text = "This should not recompose")
Spacer(modifier = Modifier.height(10.dp))
Text(text = item.toString().repeat(10))
}
}
is AChild3 -> {
Surface(
modifier = Modifier
.fillMaxWidth()
.height(100.dp), color = Color.Green
) {
}
}
}
}
Compose bom version: 2024.04.01
Stylianos Gakis
05/10/2024, 11:59 AMAtul Gupta
05/10/2024, 12:00 PMremember
(not shown in the code snippet) block so that also gets retrigger at each scroll delta and I don’t want to re do the calculationAtul Gupta
05/10/2024, 12:07 PMStylianos Gakis
05/10/2024, 12:23 PMAtul Gupta
05/10/2024, 12:28 PMagrosner
05/10/2024, 12:40 PMagrosner
05/10/2024, 12:41 PMAtul Gupta
05/10/2024, 4:13 PMArkadii Ivanov
05/10/2024, 6:30 PMrememberSaveable
might help.Atul Gupta
05/10/2024, 6:45 PMrememberSaveable
yes this might help but I want to highlight the issue that prefetching is not working correctly.Arkadii Ivanov
05/10/2024, 6:53 PMArkadii Ivanov
05/10/2024, 6:54 PMArkadii Ivanov
05/10/2024, 6:55 PMAtul Gupta
05/10/2024, 7:23 PMArkadii Ivanov
05/10/2024, 8:04 PMAtul Gupta
05/10/2024, 9:09 PMkey
is different in all cases(actually LazyList
throws when you use same id twice). For content type I am using the class name of the class
BTW what is WAI 😅Stylianos Gakis
05/10/2024, 9:25 PM