cardlong-clickandroid-jetpack-compose-lazy-column

Add longclick to a card in lazycolumn in kotlin


I have a lazycolumn with a card in Jetpack Compose - I added a clickable modifier to it and it works

Card(colors = CardDefaults.cardColors(
            containerColor = Color(0xFF7DCEA0), Color.Black
        ),
            elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
            modifier = Modifier
                .padding(horizontal = 6.dp, vertical = 6.dp)
                .offset { IntOffset(swipeableState.offset.value.roundToInt(), 0) }
                
                .clickable {
                    
                    mContext.startActivity(Intent(mContext, DCInfo::class.java))
                }



                .border(1.dp, SolidColor(Color(0xFF1C536F)), shape = RoundedCornerShape(15.dp))
                .fillMaxWidth(), shape = RoundedCornerShape(CornerSize(15.dp))
        )

Now I would add a longclick also - when I add .longclick {} it does not work

= unsolved reference

I have added import androidx.compose.foundation.gestures.* already

Any help?


Solution

  • To add a long click event to a card in a LazyColumn using Jetpack Compose, you can use the Modifier.pointerInput() method to detect a long press on the card, and then use a callback function to handle the event. Try this:

    Card(colors = CardDefaults.cardColors(
                containerColor = Color(0xFF7DCEA0), Color.Black
            ),
                elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
                modifier = Modifier
                    .padding(horizontal = 6.dp, vertical = 6.dp)
                    .offset { IntOffset(swipeableState.offset.value.roundToInt(), 0) }
                    .pointerInput(Unit){
                         detectTapGestures(
                             onLongPress = {
                                 // perform some action here..
                                 mContext.startActivity(Intent(mContext, DCInfo::class.java))
                             }
                         )
                    }
                    .border(1.dp, SolidColor(Color(0xFF1C536F)), shape = RoundedCornerShape(15.dp))
                    .fillMaxWidth(), shape = RoundedCornerShape(CornerSize(15.dp))
            )