Hi, I've been playing around trying to implement r...
# compose
s
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
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
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
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
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
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
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
Can you show the newer version with the modified WithConstraints function?
s
Now it is highly unstable (I encountered like 5 different crashes scrolling down so far), so will try to fix that first.
m
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
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
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