在我的应用程序中,我使用了 RV。这是它的重载适配器:
private async void SetContent(PhotoViewHolder vh, int position) {
await SetContentAsync(vh, position);
}
private async Task SetContentAsync(PhotoViewHolder vh, int position)
{
string SongName = "";
string ArtistName = "";
Bitmap bitmap = null;
byte[] data = null;
RequestOptions requestOptions = null;
try
{
reader.SetDataSource(mp3Obj[position].Mp3Uri);
}
catch
{
Toast.MakeText(ctx, "ERROR 77s9", ToastLength.Short).Show();
}
// await Task.Run(() => // cause problems with the reload
// {
SongName = reader.ExtractMetadata(MediaMetadataRetriever.MetadataKeyTitle);
ArtistName = reader.ExtractMetadata(MediaMetadataRetriever.MetadataKeyArtist);
data = reader.GetEmbeddedPicture();
if (data != null)
{
try
{
bitmap = BitmapFactory.DecodeByteArray(data, 0, data.Length);
requestOptions = new RequestOptions();
requestOptions.InvokeDiskCacheStrategy(DiskCacheStrategy.None);
// requestOptions.SkipMemoryCache(true);
requestOptions.CircleCrop();
requestOptions.CenterInside();
requestOptions.FitCenter();
requestOptions.OptionalCircleCrop();
ConvertBitmapToBackground(bitmap, vh, data); // Set As Backgorund, blurry and black ( just sets the variable)
}
catch
{
Toast.MakeText(ctx, "ERROR 034c", ToastLength.Short).Show();
}
}
//});
((Activity)ctx).RunOnUiThread(() =>
{
vh.SongName.SetTypeface(tf, TypefaceStyle.Normal);
vh.AristName.SetTypeface(tf, TypefaceStyle.Normal);
vh.SongName.Text = SongName;
vh.AristName.Text = ArtistName;
try
{
if (data != null)
{
Glide
.With(ctx)
.Load(data)
.Apply(requestOptions)
.Into(vh.CoverArt);
}
else // because recycler items inherit their shit and if it is altered it just shows views were there shouldnt be any ...
{
vh.CoverArt.SetImageResource(Resource.Drawable.btn_musicalnote);
vh.dr = null;
}
}
catch {
Toast.MakeText(ctx, "ERROR 034c", ToastLength.Short).Show();
}
});
}
如您所见,数据被解码的部分过去是在异步方法中,不会阻塞 UI 线程。但是,这会导致加载时出现问题。在繁重的工作下,物品的信息错误,物品翻倍,有时甚至被绊倒。通过删除异步任务并将其全部同步完成,问题得到了解决。但是,这使整个应用程序变得更加滞后。
如何在 RV 中正确使用 asnyc 任务,而不会弄乱项目?