Grux
Grux

Reputation: 584

RecyclerView view data resets after scrolling

I'm trying to bold new items added to the recyclerview, and set the item to normal typeface after clicking it. Everything works however my problem arrives after the user scrolls, all items are set back to bold. When I create the adapter, the items are shown the way they should, with new items as bold and clicked items as normal, until the user scrolls and items scrolled out of view are back to bold. How can I get it to where the user scrolls and doesn't reset the typeface?

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    if (articles.get(position).isNewArticle()) {
        holder.txtHeader.setTypeface(null, Typeface.BOLD);
    }
    holder.txtHeader.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                articles.get(holder.getAdapterPosition()).setNewArticle(false);
                holder.txtHeader.setTypeface(null, Typeface.NORMAL);
                notifyItemChanged(holder.getAdapterPosition());
           }
    });
}

Upvotes: 0

Views: 701

Answers (1)

Ready Android
Ready Android

Reputation: 3632

On scroll new holder item will be created where you are setting only for newArtical case not for normal, check with below one:

@Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if (articles.get(position).isNewArticle()) {
            holder.txtHeader.setTypeface(null, Typeface.BOLD);
        }else{
            holder.txtHeader.setTypeface(null, Typeface.NORMAL);
        }
        holder.txtHeader.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                articles.get(holder.getAdapterPosition()).setNewArticle(false);
                holder.txtHeader.setTypeface(null, Typeface.NORMAL);
                notifyItemChanged(holder.getAdapterPosition());
            }
        });
    }

Upvotes: 4

Related Questions