In my adapter I'm checking if value is bigger or equal to zero, I change the layout color to green, if not it stays red. The problem is when there's a lot of items in the recyclerview colors stop changing, or they all appear green (even if value is smaller than zero). What's causing the problem and how can I fix it? Thanks in advance. this is my adapter:
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import java.text.DecimalFormat;
import java.util.ArrayList;
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.RVViewHolder> {
private Context mContext;
private ArrayList<Item> itemList;
public RVAdapter(Context context, ArrayList<Item> arrayList) {
mContext = context;
itemList = arrayList;
}
public static class RVViewHolder extends RecyclerView.ViewHolder {
TextView valueTv, memoTv, categoryTv, dateTv;
LinearLayout indicatorLL, infoLL;
CardView itemLL;
public RVViewHolder(View itemView) {
super(itemView);
// Calling FindViewByID()
valueTv = itemView.findViewById(R.id.tv_value);
memoTv = itemView.findViewById(R.id.tv_memo);
categoryTv = itemView.findViewById(R.id.tv_category);
dateTv = itemView.findViewById(R.id.tv_date);
indicatorLL = itemView.findViewById(R.id.layout_indicator);
infoLL = itemView.findViewById(R.id.layout_item_info);
itemLL = itemView.findViewById(R.id.item_layout);
}
}
@Override
public RVViewHolder onCreateViewHolder(ViewGroup parent, int i) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.item_layout, parent, false);
return new RVViewHolder(view);
}
@Override
public void onBindViewHolder(RVViewHolder holder, int position) {
//holder.infoLL.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_transition_anim));
//holder.indicatorLL.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_transition_anim));
Item item = itemList.get(position);
DecimalFormat format = new DecimalFormat("#,###,###,###.##");
Double value = item.getValue();
holder.valueTv.setText(format.format(value));
if (value > 0 || value == 0) {
Resources res = mContext.getResources();
//holder.indicatorLL.setBackground(res.getDrawable(R.drawable.item_income_indicator_bg, mContext.getTheme()));
holder.indicatorLL.setBackgroundTintList(ColorStateList.valueOf(res.getColor(R.color.green, mContext.getTheme())));
}
holder.memoTv.setText(item.getMemo().toString());
holder.categoryTv.setText(item.getCategory().toString());
holder.dateTv.setText(item.getDate().toString());
holder.itemLL.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_transition_anim));
}
@Override
public int getItemCount() {
return itemList.size();
}
}
It might be because being a recyclerview the viewholders get recycled and your code only changes colour in one direction i.e. to green
try
Resources res = mContext.getResources();
if (value > 0 || value == 0) {
holder.indicatorLL.setBackgroundTintList(ColorStateList.valueOf(res.getColor(R.color.green, mContext.getTheme())));
} else {
holder.indicatorLL.setBackgroundTintList(ColorStateList.valueOf(res.getColor(R.color.red, mContext.getTheme())));
}