我正在使用section recyclerview添加到购物车屏幕。并且想要从类别中的各种项目中一次只选择一个复选框。下面是代码和屏幕图像。请给我解决方案,从产品类别中一次只选择一个复选框。
以下是json回复,
{
"Status":"Success",
"StatusCode":"200",
"Message":"data fetch successfully.",
"Data":{
"1":{
"OptionGroupName":"Base",
"OptionGroupId":"1",
"OptionCount":2,
"Options":[
{
"OptionGroupId":"1",
"OptionGroupName":"Base",
"ProductId":"54",
"OptionId":"1",
"OptionName":"Soft",
"OptionPrice":"25",
"IsActive":"1"
},
{
"OptionGroupId":"1",
"OptionGroupName":"Base",
"ProductId":"54",
"OptionId":"2",
"OptionName":"Hard",
"OptionPrice":"15",
"IsActive":"1"
}
]
},
"2":{
"OptionGroupName":"Sauce",
"OptionGroupId":"2",
"OptionCount":3,
"Options":[
{
"OptionGroupId":"2",
"OptionGroupName":"Sauce",
"ProductId":"54",
"OptionId":"3",
"OptionName":"Shezwan",
"OptionPrice":"10",
"IsActive":"1"
},
{
"OptionGroupId":"2",
"OptionGroupName":"Sauce",
"ProductId":"54",
"OptionId":"4",
"OptionName":"Chilly",
"OptionPrice":"20",
"IsActive":"1"
},
{
"OptionGroupId":"2",
"OptionGroupName":"Sauce",
"ProductId":"54",
"OptionId":"5",
"OptionName":"Soya",
"OptionPrice":"29",
"IsActive":"1"
}
]
}
}
}
这是java代码;
public class ProductCartAdapter extends SectionedRecyclerViewAdapter<RecyclerView.ViewHolder> {
private List<ProductCart> allData;
public ProductCartAdapter(List<ProductCart> data) {
this.allData = data;
}
@Override
public int getSectionCount() {
return allData.size();
}
@Override
public int getItemCount(int section) {
return allData.get(section).getItemList().size();
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int section) {
String sectionName = allData.get(section).getName();
SectionViewHolder sectionViewHolder = (SectionViewHolder) holder;
sectionViewHolder.tvProductName.setText(sectionName);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int section, final int relativePosition, final int absolutePosition) {
final ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
itemViewHolder.tvProductName.setText(allData.get(section).getItemList().get(relativePosition).getOptionName());
itemViewHolder.tvProductPrice.setText("+ $" + allData.get(section).getItemList().get(relativePosition).getOptionPrice());
**// what I have to code here for selecting only one checkbox (not from addons only from base and sauce)**
itemViewHolder.cbProduct.setTag(section);
itemViewHolder.cbProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer pos = (Integer) itemViewHolder.cbProduct.getTag();
if (allData.get(section).getItemList().get(relativePosition).isSelected()) {
allData.get(pos).getItemList().get(relativePosition).setSelected(false);
} else {
allData.get(pos).getItemList().get(relativePosition).setSelected(true);
}
}
});
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, boolean header) {
View v = null;
if (header) {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_section_cart, parent, false);
return new SectionViewHolder(v);
} else {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_cart_item, parent, false);
return new ItemViewHolder(v);
}
}
// SectionViewHolder Class for Sections
public class SectionViewHolder extends RecyclerView.ViewHolder {
final TextView tvProductName;
public SectionViewHolder(View itemView) {
super(itemView);
tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
}
}
// ItemViewHolder Class for Items in each Section
public class ItemViewHolder extends RecyclerView.ViewHolder {
final TextView tvProductName, tvProductPrice;
CheckBox cbProduct;
public ItemViewHolder(final View itemView) {
super(itemView);
tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
tvProductPrice = (TextView) itemView.findViewById(R.id.tvProductPrice);
cbProduct = (CheckBox) itemView.findViewById(R.id.cbProduct);
}
}
}
答案 0 :(得分:1)
b
答案 1 :(得分:0)
如果您只想检查一个项目,请使用RadioGroup代替或使用recyclerview(在一个内部)。
这样用户就会知道他/她只能从列表中选择一个项目。这将提高用户的理解,同时还会减少循环,以检查是否已检查任何项目。