简体   繁体   English

我们如何在 Jetpack Compose 的底部表单中使用 onLongClick 侦听器?

[英]How we can use onLongClick listener for bottom sheet in jetpack compose?

I am try to learn jetpack compose, and I want to apply bottom sheet for on long click listener, I have example of code below, and I try to use bottom sheet for onLongClick listener, but I have some confused about how can I apply for row data?我正在尝试学习 jetpack compose,我想为长按监听器应用 bottom sheet,我有下面的代码示例,我尝试为 onLongClick 监听器使用 bottom sheet,但我对如何申请有一些困惑行数据? I had some example but I do not have an idea about this problem?我有一些例子,但我不知道这个问题?

@Composable
fun BSDataScreen() {
    val modalBottomSheetState = rememberModalBottomSheetState(initialValue = 
 ModalBottomSheetValue.Hidden)
    val scope = rememberCoroutineScope()

    ModalBottomSheetLayout(
        sheetContent = {

            SheetScreen()
        },
        sheetState = modalBottomSheetState,
        sheetShape = RoundedCornerShape(topStart = 15.dp, topEnd = 15.dp),
        sheetBackgroundColor = Color.White,
      
    ) {
        Scaffold(

            backgroundColor =  Color.White,
        ) {
            DataScreen(
                scope = scope, state = modalBottomSheetState)}}}

@Composable
fun DataScreen(
    scope: CoroutineScope,
    state: ModalBottomSheetState
  ) {


    val listOfData = listOf(
        Data( painterResource(R.drawable.image1)),
        Data(painterResource(R.drawable.image2)),
        Data(painterResource(R.drawable.image3),)

    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)

    ) {

        LazyColumn(
            modifier = Modifier

        ) {

            items(listOfData.size) { index ->
                DataListItem(listOfData[index])}}}}


@Composable
fun DataListItem(data: Data) {

    val context = LocalContext.current

    Column(
        modifier = Modifier.padding(5.dp)
    ) {

        Row(

            modifier = Modifier
                .fillMaxWidth()
                .padding(5.dp)
                .combinedClickable(
                    onLongClick= {
                scope.launch {
                    state.show()
                }},)

        ) {

            Image(
                painter = data.painter,
                contentDescription = null,)}}}
      

You can create a lambda onClick: (Data) -> Unit and hoist state from DataListItem to DataScreen for each item您可以创建一个 lambda onClick: (Data) -> Unit 并为每个项目从DataListItem提升 state 到DataScreen

@Composable
fun DataListItem(data: Data, onLongClick: (Data) -> Unit) {

    val context = LocalContext.current

    Column(
        modifier = Modifier.padding(5.dp)
    ) {

        Row(modifier = Modifier
                .fillMaxWidth()
                .padding(5.dp)
                    .combinedClickable(
                    onClick = {

                    },
                    onLongClick = {
                      onLongClick(data)
                    }
                )
        ) {
        }
    }
}

And in DataScreen而在数据屏

  LazyColumn(
    modifier = Modifier

) {

    items(listOfData.size) { index ->
        DataListItem(listOfData[index]) { data: Data->
            // you can call bottom sheet or pass data if required
            scope.launch {
                state.show()
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何在 jetpack compose 中使用底板? - How we can use bottom sheet in jetpack compose? 我们如何在 jetpack compose 中使用颜色网格视图? - How we can use color grid view in jetpack compose? 我们如何在jetpack compose中为每行使用切换按钮? - How we can use toggle button for per row in jetpack compose? 我们如何在 android jetpack compose 中使用行项目的意图? - How we can use intent for row items in android jetpack compose? 如何在用xml编写的标准底部表单对话框中使用jetpack compose的lazyColumn? - How to use lazyColumn of jetpack compose in the standard bottom sheet dialog written in xml? 如何在同一个类的onLongClick方法中的多个按钮上使用OnLongClick侦听器 - How can i use OnLongClick Listener on more than 1 buttons in a onLongClick Method in same class 如何在jetpack compose中为多个文本应用底页? - How to apply bottom sheet for multiple text in jetpack compose? 我们如何在“jetpack compose”中将选项卡布局的“选项卡指示器”移动到顶部? 默认情况下,它显示在选定选项卡的底部 - How can we move "tab indicator" of Tab layout to top in "jetpack compose"? By default it is appearing at bottom of selected tab Jetpack Compose Bottom Sheet 暗淡阴影不可见 - Jetpack Compose Bottom Sheet dim shadow not visible Jetpack Compose:底页未按预期显示 - Jetpack Compose: Bottom Sheet not showing up as expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM