[英]Recyclerview Adapter not working properly
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).问题是当 recyclerview 颜色中的很多项目停止更改时,或者它们都显示为绿色(即使值小于零)。 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();
}
}
You should add else statement
because views are recycle when you scroll您应该添加else statement
因为滚动时视图会被回收
if (value > 0 || value == 0) {
holder.indicatorLL.setBackgroundTintList(ColorStateList.valueOf(res.getColor(R.color.green, mContext.getTheme())));
} else {
// Set default color
holder.indicatorLL.setBackgroundTintList(ColorStateList.valueOf(res.getColor(R.color.red, mContext.getTheme())));
}
It might be because being a recyclerview the viewholders get recycled and your code only changes colour in one direction ie to green这可能是因为作为一个回收者,viewholders 被回收了,你的代码只会在一个方向上改变颜色,即绿色
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())));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.