【问题标题】:Asynchronous Images downloading and display them in Gridview for android Xamarin interrupt the position in Getview异步图像下载并在 Gridview 中显示它们用于 android Xamarin 中断 Getview 中的位置
【发布时间】:2016-01-18 15:18:36
【问题描述】:

在主 Activity 上,我创建了一个在路径列表中循环并下载图像的函数

      async void downloadAsync()
      {
        foreach (string item in string_List)
        {
            if (!File.Exists(Path))
                {
                    webClient = new WebClient();
                    var url = new Uri(item);
                    byte[] imageBytes = null;

                    imageBytes = await webClient.DownloadDataTaskAsync(url);

                    //Save the Image using writeAsync
                    FileStream fs = new FileStream(Path, FileMode.OpenOrCreate);
                    await fs.WriteAsync(imageBytes, 0, imageBytes.Length);

                    //Close file connection
                    fs.Close();
                }

          }     
       }

在get view函数的grid view Adapter上放上加载imageview的代码

    public override View GetView(int position, View view, ViewGroup parent)
    {
       // retrieve the view
        View vw = view;
        ImageView picture;

        if (vw == null)
        {
         // code for create the image view
        }

        if (File.Exists(Path))
        {    
           Bitmap bitmap = null;
           bitmap =BitmapFactory.DecodeFile(path);
           picture.SetImageBitmap(bitmap);
        }
    }

当我在网格上滚动以显示图像时,我发现图像在不正确的位置,直到下载完成,然后一切都在正确的位置调整。

【问题讨论】:

    标签: android gridview bitmap xamarin async-await


    【解决方案1】:

    这是因为你的BitmapFactory.DecodeFile和视图在同一个任务下运行,所以在“下载”图片时会出错

    你需要在 Async 版本的 DecodeFile 下运行它

    请尝试这样:

    bitmap = await BitmapFactory.DecodeFileAsync(path);
    picture.SetImageBitmap(bitmap);
    

    【讨论】:

    • 感谢您的帮助,但我仍然遇到同样的问题,即当我想在 Gridview 上滚动时,它被称为 getview(覆盖函数),它是 interrput 并获取位置参数的错误值
    • 所以我认为最好的方法是获取 List 中的所有图像。之后,在此列表中循环并逐个添加。
    • 我明白了,但正如我告诉你的,从 GridView 适配器调用的 get view 函数的参数位置返回 false 值,即当你向下滚动而不是将位置读取为 16 时,例如将其返回为零。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多