3

I want to create a table view with freezed header and side bar in jetpack compose. I use the code below:

    val vState = rememberLazyListState()

    var hState = rememberLazyListState()

    ConstraintLayout(modifier = modifier.fillMaxSize()) {
        val (settingsText, headers, sideBar, cellsCon) = createRefs()

        Column(
            modifier = Modifier
                .width(headerWidth)
                .height(headerHeight)
                .constrainAs(settingsText) {
                    top.linkTo(parent.top)
                    start.linkTo(parent.start)
                },
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(text = "Settings", modifier = Modifier.wrapContentSize())
        }

        LazyRow(
            state = hState.also {
               hState.firstVisibleItemScrollOffset
            },
            modifier = Modifier
                .fillMaxWidth()
                .height(headerHeight)
                .constrainAs(headers) {
                    top.linkTo(parent.top)
                    start.linkTo(settingsText.end)
                    end.linkTo(parent.end)
                    width = Dimension.fillToConstraints
                },
        ) {
            repeat(scores.size) {
                item {
                    Surface(
                        border = BorderStroke(.5.dp, Color.DarkGray),
                        contentColor = Color.Transparent,
                        modifier = Modifier.width(sideBarWidth)
                    ) {
                        Box(
                            modifier = Modifier
                                .width(headerWidth)
                                .background(Color.LightGray),
                            contentAlignment = Alignment.Center
                        ) {
                            HeaderLabel(score = scores[it], onEvent = onEvent)
                        }
                    }
                }

            }
        }

        LazyColumn(
            state = vState.also {
                 vState.firstVisibleItemScrollOffset
              }
            ,
            modifier = Modifier
            .width(headerWidth)
            .constrainAs(sideBar) {
                top.linkTo(settingsText.bottom)
                start.linkTo(parent.start)
                bottom.linkTo(parent.bottom)
                height = Dimension.fillToConstraints
            }
        ) {
            items(students.size) {
                Box(
                    modifier = Modifier
                        .height(headerHeight)
                ) {
                    BasicSidebarLabel(student = students[it], index = it, onEvent = onEvent)
                }
            }
        }


        LazyRow(
            state = hState.also {
               hState.firstVisibleItemScrollOffset
            },
            modifier = Modifier
            .constrainAs(cellsCon) {
                top.linkTo(headers.bottom)
                start.linkTo(sideBar.end)
                bottom.linkTo(parent.bottom)
                end.linkTo(parent.end)
                width = Dimension.fillToConstraints
                height = Dimension.fillToConstraints
            }) {
            cells.forEachIndexed { it, cell ->
                item {
                    LazyColumn(
                        state = vState.also {
                           vState.firstVisibleItemScrollOffset
                    }
                    ) {
                        repeat(cell.size) { index ->
                            item {
                                Box(
                                    modifier = Modifier
                                        .width(sideBarWidth)
                                        .height(headerHeight)
                                ) {
                                    eventContent(
                                        studentScore = cells[it][index],
                                        listIndex = it,
                                        index = index,
                                        onEvent = onEvent
                                    )
                                }
                            }
                        }

                    }
                }
            }

        }

The code above creates a table where header and sidebar are freezed on some scrolling direction. The problem is that scrolling is very laggy with this approache. Can someone help me, how to get this functionality to work properly?

mikelantzelo
  • 182
  • 1
  • 9
  • Looks like there is no out-of-the-box support for this. You may want to take a look at this solution: https://stackoverflow.com/a/71217075/1172181 – Luis Sep 09 '22 at 14:54

0 Answers0