Is there any special way to create recyclerView in Compose Jetpack? Or it is the same as usual?
Directly in jetpack compose does not support recycler view like the android app.
Jetpack Compose is designed to work with the established View-based UI approach. If you're building a new app, the best option might be to implement your entire UI with Compose. But if you're modifying an existing app, you might not want to fully migrate your app all at once.
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.
Update March 2021: Starting from 1.0.0-beta01
LazyColumn
for a vertical listLazyRow
for a horizontal listFor example:
@Composable
fun LazyRowItemsDemo() {
LazyRow {
items((1..1000).toList()) {
Text(text = "Item $it")
}
}
}
Examples from JetNews app have static data. It's worth to mention that according to the recent Google presentation (see especially from 18:30), we should consider ScrollingList
, which is intended for list with undefined number of elements (e.g. downloaded from the web), what was traditionally handled by RecyclerView
. Then, it should look like this:
@Composable
fun NewsFeed(stories: List<StoryData>) {
ScrollingList(stories) { story ->
StoryWidget(story)
}
}
or we can do similar thing with LiveData
or RxJava Observable
/Flowable
:
@Composable
fun NewsFeed(stories: LiveData<List<StoryData>>) {
ScrollingList(stories.observe()) { story ->
StoryWidget(story)
}
}
In such case, we are re-using StoryWidget
(or any other widget of our choice) in the every step of the iteration with data emitted dynamically through the lambda expression.
In jetnews
sample project for list/recyclerview
they are using VerticalScroller
with Column
and using forEach
to populate items below @Composable
function is example
@Composable
private fun TabWithTopics(tabname: String, topics: List<String>) {
VerticalScroller {
Column {
HeightSpacer(16.dp)
topics.forEach { topic ->
TopicItem(
getTopicKey(
tabname,
"- ",
topic
), topic
)
TopicDivider()
}
}
}
}
For class and method check this link
https://github.com/android/compose-samples/blob/master/JetNews/app/src/main/java/com/example/jetnews/ui/interests/InterestsScreen.kt
For more information you can download/clone jetnews
sample from check here's link
https://github.com/android/compose-samples/tree/master/JetNews
For latest Jetpack alpha release update the below:
@Composable
fun LazyRowItemsDemo() {
LazyRowFor(items = (1..1000).toList()) {
Text(text = "Item $it")
}
}
Hope it's helpful for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With