Render LazyVGrid and LazyHGrid as Compose lazy grids - #16
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Neither grid had an Android renderer. Both now render through Jetpack Compose's lazy grids, following the
List→ComposeListViewinterop precedent rather than inventing a new bridging mechanism.Structure
ComposeGridViewhosts aLazyVerticalGrid/LazyHorizontalGridinside aFrameLayout(as withComposeListView,ComposeViewis final so it is hosted rather than subclassed), sourcing its cells from a Swift-implementedGridViewAdapter.The adapter differs from
ListViewAdapterin one important way: it vends fully realized Android views rather than strings, since grid cells are arbitrary SwiftUI content rather than plain text. Each cell is bridged back into the composition through Compose'sAndroidViewinterop, so cells are real Android views and the view modifiers from the modifier pipeline apply to them normally.Track sizing
A single
GridItem(.adaptive(minimum:))maps toGridCells.Adaptive, fitting as many tracks as will fit. Any other configuration maps toGridCells.Fixedwith one track per item, which covers the common[GridItem(.flexible()), GridItem(.flexible()), ...]usage. Per-item sizing beyond that is not yet honored.Cell extraction
Cells are recovered by recursively flattening group views and identified views, the same approach the picker uses for its rows —
ForEachwraps each element in an identified view, which has to be descended through.One trap worth calling out:
ModifiedContentconforms toGroupView, with its bare content as the only child. A naive "descend into group views" walk therefore passes straight through modifiers and silently discards them — cells rendered as unstyled text with their.padding()and.background()stripped. Modified content is treated as a leaf for this reason.Verification
On the emulator, via a new Grid gallery screen:
LazyHGridwith two rows fills column-major (1, 2 down, then 3, 4 across), matching SwiftUI.