https://kotlinlang.org logo
#compose
Title
# compose
s

shikasd

01/20/2020, 6:53 PM
Hi, I've been playing around trying to implement recycler "view" on Compose and this far for now: https://gist.github.com/ShikaSD/30ce3afc9376767f9ac090457733f627 Currently it only scrolls down and displays items correctly if you do it slowly 🙂 I have encountered an issue that layout in compose cannot add children synchronously. E.g. if recycler has some space at the bottom, it cannot be sure how many items it should lay out at once, as you cannot add and measure new items in the middle of layout pass. Currently my workaround is to keep a window of visible elements and increase/decrease it when items go out of the visible area. However, I add elements one by one (as I don't know how much space they will take) which is a bit slow (and require multiple passes to fill the screen for the first time). Do you have any suggestions on how to approach this better?
👍 1
k

Klaas Kabini

01/20/2020, 7:21 PM
This is a complex and challenging component to implement on your own and besides to get it right you will need to do a lot of hard work to cover a lot of edge cases. You can wait for ScrollingList implementation that will come with future releases of compose if you not in a hurry. Otherwise you will have to first understand the detailed implementation of how the Recycler works in the current recyclerview before you can attempt to roll out your own recycler view. Check the talks done by Romain Guy and Adam Powell on recycler views where they discuss how they are implemented.
z

Zsolt

01/20/2020, 7:43 PM
Naively I'd assume that since there are no actual objects to keep in pools and recycle, it's enough to know what Composables are in the visible window and just invoke rendering those, and much of the magic of RecyclerView doesn't need to be understood / applied here.
👍 1
Checking the above shared gist, it looks like it's one small step away from working properly.
a

Adam Powell

01/20/2020, 8:59 PM
You probably want to start from
WithConstraints
rather than
Layout
, it lets you determine the composition of children at layout time
It has a few limitations right now that we're looking to lift or expose via a different composable, such as not being able to do wrap content
s

shikasd

01/20/2020, 9:52 PM
Not sure it will help much. The problem is that
WithConstraints
works quite similar to
Layout
, it operates based on external constraints only. I am looking for something which allows adding more nodes into composition based on layout results (like lazy composable maybe?)
a

Adam Powell

01/21/2020, 1:13 AM
ah, you're right. I'm thinking of the other component in progress that's meant to help with this 🙂 should be coming soon
s

shikasd

01/21/2020, 1:32 AM
I actually managed to find a workaround after modifying
WithConstraints
. Essentially, you can use
subcompose
to create new nodes whenever needed and measure/layout them straight away.
🎉 2
👍 3
m

Manuel Wrage

01/21/2020, 9:38 AM
Can you show the newer version with the modified WithConstraints function?
s

shikasd

01/21/2020, 11:43 AM
Now it is highly unstable (I encountered like 5 different crashes scrolling down so far), so will try to fix that first.
m

Manuel Wrage

01/21/2020, 11:44 AM
I already tried tons of different thinks to make a Recyclerview like component. I hope you have more luck 👍
My subcomposition approaches were either to slow or had tons of inconsistencies like missing LayoutNode's
s

shikasd

01/21/2020, 11:50 AM
Yeah, I had like 30 problems related to that in 3 hours I spent on this
@Adam Powell , can you elaborate on this "new component" you have mentioned?
@Manuel Wrage In regards to other version, I pushed it to my pokedex fork here: https://github.com/ShikaSD/compose-pokedex/commit/d0b0997df6c7dfa9537f00cdd9387cf18d45b798 It suffers from everything you mentioned (missing LayoutNode's and crashes), but I hope I am going in the right direction :D
👍 1
a

Adam Powell

01/22/2020, 1:39 AM
not much to elaborate on, we need a variant of Layout/WithConstraints that does just-in-time composition so that you can write the layout code to fill available space the way ListView/RecyclerView do. Exact shape of the API is TBD but it's probably going to look a lot like Layout/WithConstraints
👍 1
if you're working with the underlying apis those use already you're probably not far off