androidandroid-jetpack-compose

Android Compose: LazyVerticalGrid throws error when "verticalScroll(rememberScrollState())" is applied


My code:

class AppDrawer(private val context: Context){
    lateinit var listener: () -> Unit
    private val pm: PackageManager = context.packageManager
    private var appPrerender: List<AppPrerender>?=null;
    init {
        CoroutineScope(Dispatchers.Default).launch { // In a CoroutineScope due to performance reasons
            appPrerender = pm.getInstalledApplications(PackageManager.GET_META_DATA).toList()
                .filter {
                    pm.getLaunchIntentForPackage(it.packageName) != null
                }
                .map {
                    AppPrerender(it.loadLabel(pm).toString(), it.loadIcon(pm).toBitmap(100, 100).asImageBitmap(), it.packageName) {
                        context.startActivity(
                            pm.getLaunchIntentForPackage(it.packageName)
                        )
                    }
                }
        }
    }
    @Composable
    fun Compose() {
            LazyVerticalGrid(GridCells.Fixed(6),
                Modifier
//                    .verticalScroll(rememberScrollState())
            ) {

                if (appPrerender != null) {
                    items(appPrerender!!, key = { it.packageName }) {
                        it.Render()
                    }
            }
        }
    }
}
class AppPrerender(private val name: String, private val icon: ImageBitmap, val packageName: String, private val launch: ()->Unit){
    @Composable
    fun Render(){
        App(name, icon, launch)
    }
}

@Composable
fun App(name: String, icon: ImageBitmap, launch: ()->Unit){
    Image(icon, contentDescription = name, Modifier.padding(0.dp, 20.dp).clickable { launch() })
}

The layout implementation is at so no scrollable content above
This is the layout, as you can see, no scrollable components above nor below. I always seam to recive the:

FATAL EXCEPTION: main (Ask Gemini)
                 Process: com.myxoz.home, PID: 32177
                 java.lang.IllegalStateException: Vertically scrollable component was measured with an infinity maximum height constraints, which is disallowed. One of the common reasons is nesting layouts like LazyColumn and Column(Modifier.verticalScroll()). If you want to add a header before the list of items please add a header as a separate item() before the main items() inside the LazyColumn scope. There are could be other reasons for this to happen: your ComposeView was added into a LinearLayout with some weight, you applied Modifier.wrapContentSize(unbounded = true) or wrote a custom layout. Please try to remove the source of infinite constraints in the hierarchy above the scrolling container.

I added the verticalScroll(rememberScrollState()) cause the scrolling was very choppy, but i can't get it to work, if i comment it in, the app will crash

I also tried to set a fixed height (as recomended) but it doesnt seam to change anything


Solution

  • Lazy layouts (including the grid) already contain the scroll ability. I think the error is due to you putting 2 scroll states "on top of one another". Without it, it should still be scrollable vertically.

    If this still does not solve your problem, I would suggest to use other non-lazy layouts as a debug tool, see if the error persists, and if so look where it originates.