android 在列内使用lazyColum在Jetpack合成中有错误

uxhixvfz  于 2023-03-16  发布在  Android
关注(0)|答案(1)|浏览(172)

我有一个lazyColumn,我想在列内使用,但我得到下面的错误和应用程序崩溃:

Nesting scrollable in the same direction layouts like LazyColumn and Column(Modifier.verticalScroll()) is not allowed. If you want to add a header before the list of items please take a look on LazyColumn component which has a DSL api which allows to first add a header via item() function and then the list of items via items()

lazyColumn代码,我在此代码中有一个列表:

@Composable
fun UpScreenSection(
    modifier: Modifier,
    state: ProfileState,
    viewModel: ProfileViewModel
) {
    Spacer(modifier = Modifier.size(24.dp))

    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(24.dp)
    ) {
        if (!state.items.isNullOrEmpty()) {
            Box(
                modifier = modifier
                    .fillMaxSize()
            ) {
                LazyColumn(modifier = modifier.fillMaxSize()) {
                    items(state.items) { item ->
                        ProfileListItems(item = item, onItemClick = {
                            //TODO Navigate to specific screen
                            when (it.id) {
                                1 -> {
                                }
                                2 -> {
                                }
                                3 -> {
                                }
                                4 -> viewModel.navigate(ReferAFriendDestination.route())
                            }
                        })
                    }
                }
            }
        }
    }
    Spacer(modifier = Modifier.size(24.dp))
}

上述代码用于以下可组合代码中:

@Composable
fun ProfileContentSection(
    modifier: Modifier = Modifier,
    viewModel: ProfileViewModel
) {
    val context = LocalContext.current
    val scrollState = rememberScrollState()
    Box(
        modifier = modifier
            .fillMaxSize()
    ) {
        val state = viewModel.state.value

        Column(
            modifier = modifier
                .fillMaxSize()
                .verticalScroll(state = scrollState)
        ) {
            AccountNameSection(modifier = modifier, viewModel = viewModel)
            UpScreenSection(modifier, state, viewModel)   // used above block codes
            DownScreenSection(modifier, context)
        }
        if (state.error.isNotBlank())
            SimpleSnackbar(
                text = state.error,
                modifier = modifier.align(Alignment.BottomCenter)
            )

        if (state.isLoading)
            Loading(modifier = modifier.align(Alignment.BottomCenter))

    }
}

我如何修复此错误?
注:我希望有一个可滚动的屏幕,其中有一个列表,为小型设备

z0qdvdin

z0qdvdin1#

我找到了解决办法。我必须把LayzyColumn中的每个视图都移到它里面。
示例:

@Composable
fun SampleScreen(){
    LazyColumn{
        item {
            // other views
        }

         items(state.items){listItem ->
          //Load list data 
         }

          item {
           //other views
          }
     }
}

有了这段代码,我将有一个屏幕,有一个可滚动的看法。

相关问题