我正在制作一个可在手机上播放音乐文件的应用程序。它在回收者视图中列出所有歌曲及其艺术家名称,歌曲名称和专辑封面(如果有)。
一切正常,直到我尝试加载专辑封面为止。 在开始时(在我滚动之前),一切看起来都很好,在滚动之后,封面开始出现在不应放置的位置。可以这么说,我刚刚看过歌曲X的专辑封面,然后滚动一下之后,歌曲y的旁边出现了歌曲X的相同封面。 这特别令人困扰,因为它确实可以正确加载专辑名称和其他所有内容。只是混淆了位图。这是我初始化回收站视图的方式:
private async Task InitRecView()
{
await Task.Run(() =>
{
// Instantiate the adapter and pass in its data source:
LinearLayout lnBg = (LinearLayout)FindViewById(Resource.Id.background_recView);
mAdapter = new PhotoAlbumAdapter(GetSortedListWithAllSongs(), this, dbSeekObj, seekObj, mAudioManager, this, lnBg);
// Get our RecyclerView layout:
mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
// Plug the adapter into the RecyclerView:
mRecyclerView.SetAdapter(mAdapter);
mRecyclerView.SetItemViewCacheSize(15);
mRecyclerView.DrawingCacheEnabled = true;
mRecyclerView.DrawingCacheQuality = DrawingCacheQuality.High;
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.SetLayoutManager(mLayoutManager);
});
}
这是我的适配器,我在其中检索数据(问题可能发生在“ SetContentAsync()”中
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.From(parent.Context).
Inflate(Resource.Layout.CardView, parent, false);
PhotoViewHolder vh = new PhotoViewHolder(itemView, mp3Obj, act, reader, db, seekObj, audioManager, lnBg, ctx);
return vh;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
PhotoViewHolder vh = holder as PhotoViewHolder;
SetContent(vh, position);
}
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;
try
{
reader.SetDataSource(mp3Obj[position].Mp3Uri);
}
catch { }
await Task.Run(() => // cause problems with the reload
{
SongName = reader.ExtractMetadata(MediaMetadataRetriever.MetadataKeyTitle);
ArtistName = reader.ExtractMetadata(MediaMetadataRetriever.MetadataKeyArtist);
data = reader.GetEmbeddedPicture();
if (data != null)
{
bitmap = BitmapFactory.DecodeByteArray(data, 0, data.Length);
}
});
((Activity)ctx).RunOnUiThread(() =>
{
vh.SongName.SetTypeface(tf, TypefaceStyle.Normal);
vh.AristName.SetTypeface(tf, TypefaceStyle.Normal);
vh.SongName.Text = SongName;
vh.AristName.Text = ArtistName;
if (bitmap != null)
{
ConvertBitmapToBackground(bitmap, vh); // Set As Backgorund, blurry and black ( just sets the variable)
CutImageIntoForm(bitmap, vh); // Set as the musical note button
}
});
}
这是视图持有者,除了初始化视图之外,什么也没发生:
public PhotoViewHolder(View itemView, List<MP3objectSmall> mp3Obj, Activity act, MediaMetadataRetriever reader, DataBase db, List<SeekObj> seekObj, AudioManager audioManager, LinearLayout lnBg, Context ctx) : base(itemView)
{
// Locate and cache view references:
SongName = itemView.FindViewById<TextView>(Resource.Id.textView);
AristName = itemView.FindViewById<TextView>(Resource.Id.textView2);
lnContainer = itemView.FindViewById<LinearLayout>(Resource.Id.linlay_cardview);
CoverArt = itemView.FindViewById<ImageButton>(Resource.Id.musical_note);
this.mp3Obj = mp3Obj;
this.act = act;
this.reader = reader;
this.db = db;
this.seekObj = seekObj;
this.ctx = ctx;
this.audioManager = audioManager;
this.lnBg = lnBg;
lnContainer.Click += delegate
{
int pos = AdapterPosition;
ClickEvent(pos, AristName.Text, SongName.Text, CoverArt, lnBg);
};
}
如果有人可以在这里帮助我。为什么我的回收者视图会混淆位图,并将它们放置在错误的布局中和/或将它们加倍?
谢谢!
编辑:
我注意到,相同项目的重新出现总是在相同间隔内发生。对我来说,每第24个项目都包含该项目之前第24个位置的图像。我还读到这是因为回收者视图是回收者所说的视图,但是我不了解的是如何确定某物品是否被回收以及何时回收,仅使用新数据...
答案 0 :(得分:0)
由于使用了异步方法,因此也就显示了旧数据也就不足为奇了。在viewHolder中创建clear
方法,然后在onViewRecycledMethod中调用它:
public class TextRecyclerAdapter extends RecyclerView.Adapter<TextRecyclerAdapter.TextViewHolder> {
...
@Override
public void onViewRecycled(@NonNull TextViewHolder holder) {
//clear your view here
holder.clear();
}
....
public static class TextViewHolder extends RecyclerView.ViewHolder {
...
public void clear(){
//clear your view here
_textView.setText("");
}
private TextView _textView;
...
}
更新 也可能会发生这种情况,因为异步调用中的旧数据要晚于新数据。您需要通过使用取消令牌来取消旧请求,以循环使用。