Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event Handling on a Jetpack Compose Card

I have a music player app - on my screen, I have cards displaying information about tracks. When I tap on one card, the player should play its track.

@Composable
fun TrackCard(track: Track) {
    Card(
        modifier = Modifier.padding(5.dp).fillMaxWidth(),
        contentColor = Color.Black
    ) {
        Column(modifier = Modifier.padding(10.dp)) {
            ArtistName(text = track.artist.name)
            Headline(text = track.title)
            AlbumName(text = track.album.title)
        }
    }
}

I tried it with adding a Clickable on its Modifier like this: clickable(onClick = {}) In order to let the track play, I need another param for my TrackCard, the player itself. As I want to keep UI code separate, I was wondering if I could add an Event Listener and access play the track from there? (Also I would like to catch the different type of events like a long pressed touch etc. for different event handling as well)

The track cards get initialized like this:

@Composable
private fun ResultList(model: PlayerModel) {
    with(model) {
            LazyColumn(state = rememberLazyListState()) {
                items(tracks) {
                    TrackCard(it)
                }
            }
        }
    }
}

How would I accomplish this?

like image 295
Remo Avatar asked Apr 23 '21 19:04

Remo


People also ask

Is jetpack compose reactive?

Jetpack Compose is a modern toolkit designed to simplify UI development. It combines a reactive programming model with the conciseness and ease of use of the Kotlin programming language. It is fully declarative, meaning you describe your UI by calling a series of functions that transform data into a UI hierarchy.

When to Use remember jetpack compose?

remember can be used to store both mutable and immutable objects. Note: remember stores objects in the Composition, and forgets the object when the composable that called remember is removed from the Composition.

What is LazyColumn in jetpack compose?

A LazyColumn is a vertically scrolling list that only composes and lays out the currently visible items. It's similar to a Recyclerview in the classic Android View system.

How do you use navigation in jetpack compose?

If you want to use the Navigation component with Compose, you have two options: Define a navigation graph with the Navigation component for fragments. Define a navigation graph with a NavHost in Compose using Compose destinations. This is possible only if all of the screens in the navigation graph are composables.

What is card in jetpack compose?

A Card is a container that can be used as a parent view for declaring multiple UI elements inside it. It has an elevation property that applies a shadow effect around it. In this article, we will show you how you could implement a simple Card in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

How to handle permission requests in jetpack compose?

Using the accompanist library to handle permission requests is way much more effortless. The accompanist is a collection of many libraries that provide Jetpack Compose with developers’ features. Permission handling is effective if we check the permissions state when the app starts.

Is there a clickable component in jetpack compose?

For instance a component like Button in Jetpack Compose which provides an onClick listener is using Clickable under the hood. If you are interested in exploring other components in Jetpack Compose you can check out the series of articles that I have written. How to make a Scrollable list in Jetpack Compose?

How to back up jetpack compose bottom sheet?

As Jetpack compose bottom sheets are not backed by navigation library, system back press closes the current screen instead of the bottom sheet. To handle these real-time use-cases, compose provides BackHandler function. First, let’s take a look at the function signature:


2 Answers

You can add a click event to a Card like this. Put the code you would want to include for the click inside the clickable function

Card(
modifier = Modifier.padding(5.dp).fillMaxWidth().clickable{ },
        contentColor = Color.Black       
    )
like image 176
Narendra_Nath Avatar answered Nov 12 '22 01:11

Narendra_Nath


It depends by your MediaPlayer implementation, but you can decouple the card and the media player composables using something like:

val (source, setSource) = remember { mutableStateOf (
    "mediaToPlay")
}

//MediaPlayer(source)
LazyColumn() {
    items(listTrack){
        TrackCard(it, onClickStartSource = {setSource(it.source)})
    }
}

with:

@Composable
fun TrackCard(track: Track, onClickStartSource : () -> Unit) {

    Card(
        modifier = Modifier
            .padding(5.dp)
            .fillMaxWidth(),
         onClick = {onClickStartSource},
            //your code
    ){ /* ... */ }

Also you can use the combinedClickable modifier to get the different click events:

Card(
    modifier = Modifier
        .padding(5.dp)
        .fillMaxWidth()
        //.clickable(onClick = onClickStartSource)
        .combinedClickable(
            onLongClick = { /*....*/ },
            onDoubleClick ={ /*....*/ }),
    contentColor = color
)
like image 42
Gabriele Mariotti Avatar answered Nov 12 '22 00:11

Gabriele Mariotti