Recyclerview单品选择器
我为我的RecyclerView商品添加了选择器背景,但我一次只能选择单件商品。即如果选择了一个项目,则不能选择其余的项目。我怎样才能做到这一点?Recyclerview单品选择器
这里是我的适配器类
public class VisaTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { Context context;
private List<VisaType> visaTypeList;
int selected_position;
public VisaTypeAdapter(Context context, List<VisaType> visaTypeList) {
this.context = context;
this.visaTypeList = visaTypeList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.visa_type_row, parent, false);
return new jp.workjapan.adapters.VisaTypeAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final VisaType a = visaTypeList.get(position);
final jp.workjapan.adapters.VisaTypeAdapter.ViewHolder viewHolder = (jp.workjapan.adapters.VisaTypeAdapter.ViewHolder) holder;
viewHolder.visa_eng.setText(a.getName_en());
viewHolder.visa_jp.setText(a.getName_jp());
final long id = a.getId();
if (id == AppSingleTon.visaType) {
viewHolder.visa_layout.setSelected(true);
} else {
viewHolder.visa_layout.setSelected(false);
}
viewHolder.visa_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (viewHolder.visa_layout.isSelected()) {
viewHolder.visa_layout.setSelected(false);
} else {
viewHolder.visa_layout.setSelected(true);
AppSingleTon.visaType = id;
selected_position = position;
}
}
});
}
}
回答:
这是我的样品溶液:
public class VisaTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { Context context;
private List<VisaType> visaTypeList;
int selected_position;
public VisaTypeAdapter(Context context, List<VisaType> visaTypeList) {
this.context = context;
this.visaTypeList = visaTypeList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.visa_type_row, parent, false);
return new jp.workjapan.adapters.VisaTypeAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final VisaType a = visaTypeList.get(position);
final jp.workjapan.adapters.VisaTypeAdapter.ViewHolder viewHolder = (jp.workjapan.adapters.VisaTypeAdapter.ViewHolder) holder;
viewHolder.visa_eng.setText(a.getName_en());
viewHolder.visa_jp.setText(a.getName_jp());
final long id = a.getId();
if (id == AppSingleTon.visaType) {
viewHolder.visa_layout.setSelected(true);
} else {
viewHolder.visa_layout.setSelected(false);
}
if (position == selected_position) {
viewHolder.visa_layout.setSelected(true);
} else {
viewHolder.visa_layout.setSelected(false);
}
viewHolder.visa_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selected_position = getAdapterPosition();
notifyDataSetChanged();
}
});
}
}
希望它可以帮助你...... 感谢
以上是 Recyclerview单品选择器 的全部内容, 来源链接: utcz.com/qa/261535.html