Android Compose:应用“verticalScroll(rememberScrollState())”时,LazyVerticalGrid 会引发错误

问题描述 投票:0回答:1

我的代码:

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() })
}

布局实现是这样的,所以上面没有可滚动的内容
这是布局,如您所见,上面和下面都没有可滚动的组件。 我总是会收到:

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.

我添加了

verticalScroll(rememberScrollState()) 
因为滚动非常断断续续,但我无法让它工作,如果我评论它,应用程序将崩溃

我也尝试设置固定高度(如建议的那样),但它不会改变任何东西

android android-jetpack-compose
1个回答
0
投票

惰性布局(包括网格)已经包含滚动功能。我认为该错误是由于您将 2 个滚动状态“相互叠加”造成的。没有它,它应该仍然可以垂直滚动。

如果这仍然不能解决您的问题,我建议使用其他非惰性布局作为调试工具,看看错误是否仍然存在,如果是,则查看它的根源。

© www.soinside.com 2019 - 2024. All rights reserved.