Icyrockton
03/28/2022, 4:53 AMFunkyMuse
03/28/2022, 6:07 AMIcyrockton
03/28/2022, 6:29 AMLazyColumn
,which is bad performanceIcyrockton
03/28/2022, 7:36 AMrecompose
all the time is related to NavController
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Content()
}
}
}
}
}
@Composable
fun Content() {
val navController = rememberNavController()
val swipeRefreshState = rememberSwipeRefreshState(isRefreshing = false)
LaunchedEffect(swipeRefreshState.isRefreshing) {
if(swipeRefreshState.isRefreshing){
delay(2000L)
swipeRefreshState.isRefreshing = false
}
}
SwipeRefresh(swipeRefreshState, onRefresh = { swipeRefreshState.isRefreshing = true} , modifier = Modifier.fillMaxSize()){
LazyColumn(modifier = Modifier
.fillMaxSize()
.offset(y = with(LocalDensity.current) { swipeRefreshState.indicatorOffset.toDp() })
){
items((1..100).map { "text ${it}" }){
Item(it,navController)
}
}
}
}
@Composable
fun Item(data: String, navController: NavHostController) {
Log.d(BuildConfig.APPLICATION_ID, "Item: ${data} recompose")
Box(modifier = Modifier
.fillMaxWidth()
.height(80.dp)
.padding(horizontal = 10.dp, vertical = 10.dp)
.clickable { navController.navigate("do some route!!!!") } /* <<<< recompose all the time , because we using navController, it's not Stable Class */
.background(Color.Yellow), contentAlignment = Alignment.Center)
{
Text(text = data)
}
}
Icyrockton
03/28/2022, 8:05 AMModifier.offset { IntOffset(0,swipeRefreshState.indicatorOffset.toInt()) }
rather than offset(y = with(LocalDensity.current) { swipeRefreshState.indicatorOffset.toDp() })
.
the Modifier.offset
lambad version will not interfering with content size measurement.Icyrockton
03/28/2022, 9:09 AMFunkyMuse
03/28/2022, 10:12 AM