将远程json文件加载到Android应用中的简单方法?视频视图

问题描述

我想以与Swift对应的方式运行。在Swift版本中,我将json加载到解析器中,该解析器以id(从最高到最低)的顺序加载列表,并显示缩略图和标题(由json提供),并在单击时显示附加ID的视频。 / p>

如果您能提供任何资源或示例,我目前看不到与SwiftUI(特别是列表视图)设置类似的东西。

这是SwiftUI文件之一

import SwiftUI
import Combine

struct VideoList: View {
@Environment(\.presentationMode) private var presentationMode
@ObservedObject private(set) var viewModel: ViewModel
@State private var isRefreshing = false

var btnBack : some View { Button(action: {
    self.presentationMode.wrappedValue.dismiss()
    }) {
        HStack {
        Image("Home") // set image here
            .aspectRatio(contentMode: .fit)
            .foregroundColor(.white)
        }
    }
}

var body: some View {
    NavigationView {
        List(viewModel.videos.sorted { $0.id > $1.id},id: \.id) { video in
            NavigationLink(
            destination: VideoDetails(viewModel: VideoDetails.ViewModel(video: video))) {
                VideoRow(video: video)
                
            }
        }
        .onPullToRefresh(isRefreshing: $isRefreshing,perform: {
            self.viewModel.fetchVideos()
        })
        .onReceive(viewModel.$videos,perform: { _ in
            self.isRefreshing = false
        })
    }
    .onAppear(perform: viewModel.fetchVideos)
    .navigationViewStyle(StackNavigationViewStyle())
    .navigationBarBackButtonHidden(true)
            .navigationBarItems(leading: btnBack)

}
}

这是它正在加载的json片段

{
"videos": [
    {
        "id": 97,"name": "name","thumbnail": "https://videodelivery.net//thumbnails/thumbnail.jpg","description": "October 11th,2020","video_link": "https://videodelivery.net//manifest/video.m3u8"
    },{
        "id": 96,

解决方法

https://riptutorial.com/android/example/29241/try-offline-disk-cache-first--then-go-online-and-fetch-the-image

这是缓存缩略图的一个很好的例子,请遵循以下步骤,唯一的变化是:您在适配器的getView方法中编写picasso.load。只需设置imageview为Picasso。现在,您可以在列表视图时传递视频URL通过在onItemCLickListener内使用一个意图然后在目标活动中进行轻拍,您可以拥有一个媒体播放器,您可以在其中使用该网址将其设置在媒体播放器上并播放视频。

此解决方案也适用于列表。

此外,由于Picasso默认使用内存缓存,因此我们也必须使用OKHTTP磁盘缓存,因为否则Picaso只会检查内存缓存中的缩略图,并且会失败。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...