父级或子级可组合触发器同时单击

mik*_*elo 5 android callback kotlin android-jetpack-compose

我有一个父级可组合项

Row(
    modifier = modifier
        .width(tableWidth)
        .fillMaxHeight(),
) {
    cells.forEachIndexed { it1, cell ->
        Column(
            modifier = Modifier
                .height(tableHeight),
        ) {
            for (i in cell.indices) {
                Box(
                    modifier = Modifier
                        .width(sideBarWidth)
                        .height(headerHeight)
                        .clickable {
                          //event here
                        }
                ) {
                    eventContent(
                        studentScore =  cell,
                        listIndex = it1,
                        index = i,
                        onEvent = onEvent // event here
                    )
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和一个子级,它是 eventContent 可组合项,如上所示。

我希望父可组合项或子可组合项触发两个单击事件。我怎样才能做到这一点?

z.g*_*g.y 2

您可以简单地创建一个lambda回调,在其中可以将您需要的所有内容设置为其参数。

@Composable
fun MyComposable(onEventCallback: (List, Int, Cell) -> Unit) {

    ...
    ...
    ...

      Box {
          eventContent(
              studentScore =  cell,
              listIndex = it1,
              index = i,
              onEvent = { selectedCell ->
                   onEventCallback(cells, i, selectedCell) // just pass everything on a single call
              }
          )
      }

    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

您可以简单地从调用站点执行单独的操作

 MyComposable { list, index, cell ->
       doSomethingOnTheList(list)
       doSomethingOnIndexAndCell(index, cell)
 }
Run Code Online (Sandbox Code Playgroud)