我正在设置一个这样的ArrayAdapter:
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(R.layout.row_product_item, null);
}
TextView txtProductName = (TextView)view.findViewById(R.id.txt_product_name);
TextView txtProductRemaining = (TextView)view.findViewById(R.id.txt_product_remaining);
TextView txtProductPrice = (TextView)view.findViewById(R.id.txt_product_sale_price);
String productName = getItem(position).productName;
String productRemaining = Number.floatToStringAsNumber(getItem(position).productRemaining);
String productPrice = Number.floatToStringAsPrice(getItem(position).productSellPrice, true);
txtProductName.setText(productName);
txtProductRemaining.setText(productRemaining);
txtProductPrice.setText(productPrice);
/**
* Coloring
*/
if (getItem(position).productRemaining <= 0){
txtProductRemaining.setTextColor(Color.parseColor("#ff7f7f"));
}
return view;
}
每当最后一行的评估返回true时,它都会绘制每个txtProductRemaining。我只需要绘制评估项目。为什么要绘制整个元素集?
答案 0 :(得分:4)
ListView
重复使用视图,因此一旦一个项目被着色,其他视图也会在加载时被着色。以下是您需要做的以避免这种情况:
if (getItem(position).productRemaining <= 0){
txtProductRemaining.setTextColor(Color.parseColor("#ff7f7f"));
}
else {
txtProductRemaining.setTextColor(Color.BLACK));. // Or whatever default color you prefer
}
参考:https://developer.android.com/reference/android/widget/ArrayAdapter.html#getView(int,android.view.View,android.view.ViewGroup)