这是我的recyclerview适配器。我正在夸大观点。现在我想在每个视图中选择容器。它们工作正常,但当我向下滚动到列表时,选择丢失了。请帮我在每个视图上存储选择容器。感谢
这是我的recyclelerviewAdapter onBind方法;
@Override
public void onBindViewHolder(final NewProductsView holder, final int position) {
final int posi = position;
final Product product = products.get(position);
if (positiono.contains(position)) {
holder.cat_con.setVisibility(View.VISIBLE);
int i = positiono.indexOf(position);
holder.cat.setText(categories.get(i).getName());
} else {
holder.cat_con.setVisibility(View.GONE);
}
holder.product_name.setText(product.getName());
holder.price1.setText("SAR " + product.getSwDrycleanPrice());
holder.price2.setText("SAR " + product.getSwWashingPrice());
holder.price3.setText("SAR " + product.getSwPress());
holder.checkBox.setChecked(product.isSelected());
holder.checkBox.setTag(product);
if (product.isSelected()) {
holder.checkBox.setSelected(product.isSelected());
} else {
holder.checkBox.setSelected(!product.isSelected());
}
if (product.isSeriver1()) {
setColored(posi, holder);
}else {
setColorTransparent(holder);
}
if (product.isSeriver2()) {
setColored(posi, holder);
}else {
setColorTransparent(holder);
}
if (product.isSeriver3()) {
setColored(posi, holder);
}else {
setColorTransparent(holder);
}
holder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Product emp = (Product) cb.getTag();
emp.setSelected(cb.isChecked());
products.get(posi).setSelected(cb.isChecked());
Toast.makeText(
v.getContext(),
"Selected Employees: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG).show();
}
});
holder.service_con1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!holder.isFav) {
product.setSeriver1(true);
products.get(posi).setSeriver1(true);
products.get(posi).setSeriver2(false);
products.get(posi).setSeriver3(false);
setColor(holder, 0);
holder.isFav = true;
} else {
holder.isFav = false;
}
}
});
holder.service_con2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!holder.isFav) {
product.setSeriver2(true);
products.get(posi).setSeriver1(false);
products.get(posi).setSeriver2(true);
products.get(posi).setSeriver3(false);
setColor(holder, 1);
holder.isFav = true;
} else {
holder.isFav = false;
}
}
});
holder.service_con3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!holder.isFav) {
product.setSeriver3(true);
products.get(posi).setSeriver1(false);
products.get(posi).setSeriver2(false);
products.get(posi).setSeriver3(true);
setColor(holder, 2);
holder.isFav = true;
} else {
holder.isFav = false;
}
}
});
}
这是setColor方法:
public void setColor(NewProductsView holder, int i) {
switch (i) {
case 0:
holder.service1.setTextColor(activity.getResources().getColor(R.color.white));
holder.price1.setTextColor(activity.getResources().getColor(R.color.white));
holder.service_con1.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));
holder.service2.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
holder.price2.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
holder.service_con2.setBackgroundColor(activity.getResources().getColor(R.color.transparent));
holder.service3.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
holder.price3.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
holder.service_con3.setBackgroundColor(activity.getResources().getColor(R.color.transparent));
break;
case 1:
holder.service1.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
holder.price1.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
holder.service_con1.setBackgroundColor(activity.getResources().getColor(R.color.transparent));
holder.service2.setTextColor(activity.getResources().getColor(R.color.white));
holder.price2.setTextColor(activity.getResources().getColor(R.color.white));
holder.service_con2.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));
holder.service3.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
holder.price3.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
holder.service_con3.setBackgroundColor(activity.getResources().getColor(R.color.transparent));
break;
case 2:
holder.service1.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
holder.price1.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
holder.service_con1.setBackgroundColor(activity.getResources().getColor(R.color.transparent));
holder.service2.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
holder.price2.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
holder.service_con2.setBackgroundColor(activity.getResources().getColor(R.color.transparent));
holder.service3.setTextColor(activity.getResources().getColor(R.color.white));
holder.price3.setTextColor(activity.getResources().getColor(R.color.white));
holder.service_con3.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));
break;
}
}
答案 0 :(得分:0)
您应该在绑定到适配器的arraylist中更新它,而不是直接在任何视图上执行setTextColor。
更改任何值后,用户notifyDataSetChanged();
将正常工作。
原因:始终在滚动回收者视图时,每当它们聚焦时,它都会回收视图。
实施例:
每当使用setColor()
方法设置新颜色时,也要将该颜色保存在arraylist中,并在onBindViewHolder
方法中动态设置。
如果我误解了,请告诉我。