Advertisement

Responsive Advertisement

Recyclerview or LazyColumn in Jetpack compose and manage click on every item (Jetpack Compose)

  • Step 1 :- First of all you have to take item for show repeatidly

@Composable
fun CustomItem(person: Person) {
Row(
modifier = Modifier
.background(Color.LightGray)
.fillMaxWidth()
.padding(24.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(12.dp)
) {
Text(
text = "${person.age}",
color = Color.Black,
fontSize = Typography.headlineLarge.fontSize,
fontWeight = FontWeight.Bold
)
Text(
text = person.firstName,
color = Color.Black,
fontSize = Typography.headlineMedium.fontSize,
fontWeight = FontWeight.Normal
)
Text(
text = person.lastName,
color = Color.Black,
fontSize = Typography.headlineMedium.fontSize,
fontWeight = FontWeight.Normal
)
}
}

  • Step 2 :-

    Take data model 

    data class Person(
    val id: Int,
    val firstName: String,
    val lastName: String,
    val age: Int
    )

  • Step 3 :-

    You have to take in LazyColumn

    items(items = personList) { person ->
    Card(
    modifier = Modifier
    .padding(8.dp)
    .fillMaxWidth()
    .clickable {
    Log.d(
    "PersonClick",
    "Clicked on: ${person.id} ${person.lastName}"
    )
    },
    shape = ShapeDefaults.Medium,
    elevation = CardDefaults.cardElevation(7.dp)
    ) {

    CustomItem(person = person)
    }


    }

  • Step 4 :-

    Screenshots are :-




Post a Comment

0 Comments