swift 分页在LazyVGrid布局上不起作用,但没有LazyVGrid,分页工作正常,我该怎么解决这个问题?

rsaldnfx  于 2023-06-28  发布在  Swift
关注(0)|答案(1)|浏览(113)

分页在LazyVGrid布局上不起作用,但没有LazyVGrid,分页工作正常。我真的需要使用LazyVGrid来创建我的列表。我该怎么解决这个问题?
我的准则

VStack(alignment: .center) {
     List {
        LazyVGrid(columns: gridItemLayout, spacing: 20) {
          ForEach(viewModel.tvShowList, id: \.id) { tvShow in
              VStack {
                KFImage.url(URL(string: Constant.POSTER_URL + tvShow.posterPath)!)
                  .placeholder {
                      Image("placeholder")
                        .scaledToFill()
                        .frame(width: 160, height: 100, alignment: .center)
                        .clipped()
                      }
                        .resizable()
                        .shadow(radius: 10)
                        .padding(.trailing, 20)
                        .scaledToFit()
                        .frame(width: 160, alignment: .center)
                         Text("\(tvShow.name)")
                           .multilineTextAlignment(.center)
                           .lineLimit(1)
                           .frame(maxWidth: .infinity)
                           .font(.system(size: 14))
                           .font(.headline)
                       }
                        .listRowBackground(Color.white)
                        .listRowSeparator(.hidden)
                       }
                        .id(UUID())
                        .frame(maxWidth: .infinity)
               if (viewModel.page < viewModel.totalPage) {
                       HStack {
                          Spacer()
                          ProgressView()
                          .onAppear {
                            viewModel.page += 1
                            viewModel.getTvShowTopRated()
                       }
                           Spacer()
                }
          }
   }
}
                 .listStyle(GroupedListStyle())
                 .padding([.leading, .trailing], 15)    }
mbskvtky

mbskvtky1#

试试这个,而不是列表,你需要使用ScrollView

VStack(alignment: .center) {
 ScrollView {
    LazyVGrid(columns: gridItemLayout, spacing: 20) {
      ForEach(viewModel.tvShowList, id: \.id) { tvShow in
          VStack {
            KFImage.url(URL(string: Constant.POSTER_URL + tvShow.posterPath)!)
              .placeholder {
                  Image("placeholder")
                    .scaledToFill()
                    .frame(width: 160, height: 100, alignment: .center)
                    .clipped()
                  }
                    .resizable()
                    .shadow(radius: 10)
                    .padding(.trailing, 20)
                    .scaledToFit()
                    .frame(width: 160, alignment: .center)
                     Text("\(tvShow.name)")
                       .multilineTextAlignment(.center)
                       .lineLimit(1)
                       .frame(maxWidth: .infinity)
                       .font(.system(size: 14))
                       .font(.headline)
                   }
                    .listRowBackground(Color.white)
                    .listRowSeparator(.hidden)
                   }
                    .id(UUID())
                    .frame(maxWidth: .infinity)
           if (viewModel.page < viewModel.totalPage) {
                   HStack {
                      Spacer()
                      ProgressView()
                      .onAppear {
                        viewModel.page += 1
                        viewModel.getTvShowTopRated()
                   }
                       Spacer()
            }
      }
   }
}
             .listStyle(GroupedListStyle())
             .padding([.leading, .trailing], 15)    }

希望这个帮助!

相关问题