Reputation: 2768
I have a RecyclerView Adapter which adds data to RecyclerView, it works fine until I scroll up and down and see duplicate TextViews.
So my RecyclerView Loads like this
The issue is where you see tag0
and tag1
- If I scroll down and go back up, a duplicate set of tag0
and tag1
are added - as shown in the following screenshot
It keeps happening over and over again everytime I scroll
My RecyclerView Adapter Code is the following (short version)
class VideoAdapter : RecyclerView.Adapter
{
Context _context;
private List<VideoDisplayModel> _AllLinks;
public VideoAdapter(List<VideoDisplayModel> AllLinks)
{
_AllLinks = AllLinks;
}
public override int ItemCount
{
get
{
return _AllLinks.Count;
}
}
public override long GetItemId(int position)
{
return position;
}
public override int GetItemViewType(int position)
{
return position;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View listitem = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.VideoRecyclerRow, parent, false);
TextView title = listitem.FindViewById<TextView>(Resource.Id.titleVideo);
ImageView image = listitem.FindViewById<ImageView>(Resource.Id.imageVideo);
LinearLayout tagsLayout = listitem.FindViewById<LinearLayout>(Resource.Id.tagsVideoLayout);
VideoRecylerViewHolder view = new VideoRecylerViewHolder(listitem)
{
Title = title,
Thumb = image,
TagsLayout = tagsLayout
};
_context = parent.Context;
return view;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
var viewHolder = holder as VideoRecylerViewHolder;
var currentLink = _AllLinks[position];
viewHolder.Title.Text = currentLink.Title;
for(int i = 0; i < 2; i++)
{
TextView Txt = new TextView(_context);
Txt.Text = "test"+i.ToString();
Txt.SetBackgroundResource(Resource.Drawable.roundtext);
Txt.SetPadding(30, 10, 30, 10);
Txt.SetTextColor(global::Android.Graphics.Color.ParseColor("#373944"));
Txt.SetTextSize(Android.Util.ComplexUnitType.Dip, 12);
viewHolder.TagsLayout.AddView(Txt);
}
}
I don't understand, what am I doing wrong - Any idea?
Cheers
Upvotes: 3
Views: 430
Reputation: 3034
The RecyclerView recycles the same layout, so if you added a view before, it will add another one.. Make sure you clear the parent before adding the view:
viewHolder.TagsLayout.removeAllViews(); //THIS
for(int i = 0; i < 2; i++) {
...
viewHolder.TagsLayout.AddView(Txt);
}
Upvotes: 4
Reputation: 11477
I don't understand, what am I doing wrong - Any idea?
Yes, OnBindViewHolder
gets called multiple times
, its the basic fundamental of recyclerView
, (it recycles the views).
If you create views dynamically create and add views its gonna be called created multiple times once you
scroll
.
for(int i = 0; i < 2; i++){
TextView Txt = new TextView(_context);
Txt.Text = "test"+i.ToString();
Txt.SetBackgroundResource(Resource.Drawable.roundtext);
Txt.SetPadding(30, 10, 30, 10);
Txt.SetTextColor(global::Android.Graphics.Color.ParseColor("#373944"));
Txt.SetTextSize(Android.Util.ComplexUnitType.Dip, 12);
viewHolder.TagsLayout.AddView(Txt);
}
This loop is getting executed multiple times !!
So, before the for loop use :-
holder.listitem.removeAllViews();
Upvotes: 3