Updated 7 December 2023
In this blog, we are going to learn about “How to create a Recycler view in jetpack compose”. Jetpack compose is a modern tool kit building native UIs for Android using Kotlin. Directly in jetpack compose does not support recycler view like the android app. but we can achieve this create lazy rows and columns. so now we are starting to create a recycler view in jetpack compose.
First of all, we are creating a Data class with the all field for using the employee’s details.
1 2 3 4 5 6 |
data class EmployDetails(val id: Int, val title: String, val sex: String, val age: Int, val description: String, val ImageId: Int = 0) |
After create data class we are creating a new Details class. Write an object declaration and create a list with information about each employees to showing the recyclerview.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
object Details { val EmployDetailsList = listOf( EmployDetails( id = 1, title = "Rohan", sex = "Male", age = 24, description = " Don't judge each day by the harvest you reap but by the seeds that you plant.” - ...", ImageId = R.drawable.rohan ), EmployDetails( id = 2, title = "Roy", sex = "male", age = 25, description = " Don't judge each day by the harvest you reap but by the seeds that you plant.” - ...", ImageId = R.drawable.roy ), EmployDetails( id = 3, title = "Vishal", sex = "Male", age = 29, description = " Don't judge each day by the harvest you reap but by the seeds that you plant.” - ...", ImageId = R.drawable.vishal ), EmployDetails( id = 4, title = "Nikhil", sex = "Male", age = 27, description = " Don't judge each day by the harvest you reap but by the seeds that you plant.” - ...", ImageId = R.drawable.nikhil ), } |
We are showing employees details so now we are create a composble function “EmployeeCard”. In this function we are create a cardview wirth all employees details like- name,age,photo and description.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
@Composable fun EmployeeCard(emp: EmployDetails) { Card( modifier = Modifier .padding(horizontal = 8.dp, vertical = 8.dp) .fillMaxWidth(), elevation = 2.dp, backgroundColor = Color.LightGray, shape = RoundedCornerShape(corner = CornerSize(16.dp)) ) { Row(modifier = Modifier.padding(20.dp)) { Column(modifier = Modifier.weight(1f), Arrangement.Center) { Text( text = emp.title, style = TextStyle( color = Color.Black, fontSize = 22.sp, fontWeight = FontWeight.Bold, ) ) Text( text = "Age :- "+emp.age.toString(), style = TextStyle( color = Color.Black, fontSize = 15.sp ) ) Text( text = "Sex :- "+emp.sex, style = TextStyle( color = Color.Black, fontSize = 15.sp ) ) Text( text = "Description :- "+emp.description, style = TextStyle( color = Color.Black, fontSize = 15.sp ) ) } Image(painter = painterResource(emp.ImageId), contentDescription = "Profile Image", contentScale = ContentScale.FillHeight, modifier = Modifier .padding(8.dp) .size(110.dp) .clip((CircleShape) )) } } } |
In this view we have created a cardview with the some text field and circuler imageview show the employee profile picture in the recycler view.
Now we are creating a composble function to display the employee card. In this function we have created a “Lazy column” .If you need to display a large number of items (or a list of an unknown length), using a layout such as Column
can cause performance issues, since all the items will be composed and laid out whether or not they are visible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Composable fun DetailsContent() { val employees = remember { Details.EmployDetailsList } LazyColumn( contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp) ) { items( employees ) { EmployeeCard(emp = it) } } } |
We have created a “DetailsContent()” composble function to display the list. There is also a variant of the items()
extension function called itemsIndexed()
, which provides the index. so in this function item length is the “employees” list length.
In this preview shows a list of employee details in recyclerview, not very nice looking. But there are easy steps to style employee cards and show them in recyclerview.
In this blog, we have learned about the ‘Recycler view’ in jetpack compose. we have learned how to implementation of the Lazy column and act on Android. this is the easy way to create a recycler view in jetpack compose.
For more information regarding the compose gesture in the android visit the link.
Thanks for reading this blog. You can also check other blogs from here.
Always be ready for learning 🙂
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.