SwiftUI not maintaining horizontal scroll position in nested list

Viewed 171

I've been looking into this after solving this issue on my Android App using savedInstanceState and some custom adapter work. I have a nested view (vertical with horizontal scrollviews contained within) and noticed while scrolling that the horizontal lists will reset to an index of 0 once the view is off the screen and more than likely recycled.

I have looked into ScrollViewReader but could not figure out how to listen to the currently viewable index range ( in order to reset it later ). The other option seems to be using GeometryReader to do some view calculation, but that seems a bit off while using LazyGrids.

An example of the view that I am describing:

var body: some View {
    GeometryReader { geometry in
        ZStack(alignment: .top){
            VStack {
                HStack {
                        // header stuff
                    }
                    ScrollView(showsIndicators: false) {

                        // Vertical list
                        LazyVGrid(columns: gridItemLayout, spacing: 17) {
                            ForEach(parentData, id: \.key) { parent in
                                Text(parent.title)
                                ZStack {
                                    Image("image")
                                    ScrollViewReader { scrollView in
                                        ScrollView(.horizontal, showsIndicators: false) {
                                            
                                            // Horizontal lists
                                            LazyHGrid(rows: gridItemLayout, spacing: 20){
                                                ForEach(data, id: \.key) { data in
                                                    Button(action: {
                                                        //action
                                                    }) {
                                                        FeaturedCellView(data: data).equatable()
                                                    }
                                                }
                                            }.fixedSize(horizontal: true, vertical: false)
                                        }
                                    }
                                }
                            }.fixedSize(horizontal: false, vertical: true)
                        }
                    }
                }
            }
        }
    }

Examples of the views saving states can be found in apps like Netflix, or even in the App Store's horizontal grids.

I was hoping someone had solved this before and if so what you did.

Thanks for your help!

0 Answers
Related