代码很简单,逻辑很重要
大体布局效果实现:RecyclerView的条目中嵌套RecyclerView
将购物车分为三层:
外面的一层:全选/全不选 CheckBox
中间的一层:商家的 checkbox
里面的一层:商品的 checkbox
重点来了:我们需要在我们的Bean类(我们需要反序列化的类)当中去写两个boolean类型的状态,一个商家,一个商品 这两个必须要写,不然的话实现不了我们全选反选的功能
第一步:自定义view制作加减器:
public class AdditionView extends LinearLayout {
private int postion;
private int numX;
private TextView plus;
private TextView num;
private TextView reduce;
private ShopAdapter shopAdapter;
private List list = new ArrayList<>();
public AdditionView(Context context) {
super(context);
initView(context);
}
public AdditionView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public AdditionView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(final Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.addition_item, null);
plus = view.findViewById(R.id.shop_plus);
num = view.findViewById(R.id.shop_num);
reduce = view.findViewById(R.id.shop_reduce);
num.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
numX = Integer.parseInt(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
numX++;
num.setText(numX+"");
list.get(postion).setNum(String.valueOf(numX));
callBack.CallBack();
}
});
reduce.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(numX>1){
numX--;
}else {
Toast.makeText(context,"数量不能小于1",Toast.LENGTH_SHORT).show();
}
num.setText(numX+"");
list.get(postion).setNum(String.valueOf(numX));
callBack.CallBack();
}
});
num.getText();
addView(view);
}
public void setDaata(ShopAdapter shopAdapter,List list1,int i){
this.shopAdapter = shopAdapter;
this.list = list1;
postion = i;
numX = Integer.parseInt(list1.get(i).getNum());
num.setText(numX+"");
}
private Linstener callBack;
public void setCallBack (Linstener callBack){
this.callBack = callBack;
}
public interface Linstener{
void CallBack();
}
}
第二步:商品的适配器ShopAdapter:
public class ShopAdapter extends RecyclerView.Adapter {
private Context context;
private List list;
public ShopAdapter(Context context, List list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.commodity_item, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
Glide.with(context).load(list.get(i).getImages()).into(viewHolder.img);
viewHolder.name.setText(list.get(i).getTitle());
viewHolder.price.setText("$"+list.get(i).getPrice());
viewHolder.ck.setChecked(list.get(i).isCheck());
viewHolder.ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
list.get(i).setCheck(isChecked);
if (callBack!=null){
callBack.CallBack();
}
}
});
viewHolder.view.setDaata(this,list,i);
viewHolder.view.setCallBack(new AdditionView.Linstener() {
@Override
public void CallBack() {
if (callBack!=null){
callBack.CallBack();
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final ImageView img;
private final TextView name;
private final TextView price;
private final CheckBox ck;
private final AdditionView view;
public ViewHolder(@NonNull View itemView) {
super(itemView);
ck = itemView.findViewById(R.id.shop_ck);
img = itemView.findViewById(R.id.shop_img);
name = itemView.findViewById(R.id.shop_name);
price = itemView.findViewById(R.id.shop_price);
view = itemView.findViewById(R.id.shop_view);
}
}
public void selectOrRemoveAll(boolean isSelectAll){
for (ShowBean.DataBean.ListBean listBean:list){
listBean.setCheck(isSelectAll);
}
notifyDataSetChanged();
}
private Linstener callBack;
public void setCallBack (Linstener callBack1){
this.callBack = callBack1;
}
public interface Linstener{
void CallBack();
}
}
第三步:商家的适配器 ManAdapter
public class ManAdapter extends RecyclerView.Adapter {
private Context context;
private List data;
public ManAdapter(Context context, List data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.businessman_item, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
viewHolder.tv.setText(data.get(i).getSellerName());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
viewHolder.rcv.setLayoutManager(linearLayoutManager);
final ShopAdapter shopAdapter = new ShopAdapter(context, data.get(i).getList());
viewHolder.rcv.setAdapter(shopAdapter);
viewHolder.ck.setChecked(data.get(i).isCheck());
shopAdapter.setCallBack(new ShopAdapter.Linstener() {
@Override
public void CallBack() {
if (callBack!=null){
callBack.CallBack(data);
}
List listBeans = data.get(i).getList();
boolean isAllCheck = true;
for (ShowBean.DataBean.ListBean bean:listBeans){
if (!bean.isCheck()){
isAllCheck = false;
break;
}
}
viewHolder.ck.setChecked(isAllCheck);
data.get(i).setCheck(isAllCheck);
}
});
viewHolder.ck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
data.get(i).setCheck(viewHolder.ck.isChecked());
shopAdapter.selectOrRemoveAll(viewHolder.ck.isChecked());
}
});
}
@Override
public int getItemCount() {
return data.size();
}
public void setList(List list){
this.data = list;
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final TextView tv;
private final CheckBox ck;
private final RecyclerView rcv;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.shop_man);
ck = itemView.findViewById(R.id.shop_man_ck);
rcv = itemView.findViewById(R.id.shop_rcv);
}
}
private Linstener callBack;
public void setCallBack (Linstener callBack){
this.callBack = callBack;
}
public interface Linstener{
void CallBack(List list);
}
}
第四步:购物车的Activity:
public class MainActivity extends AppCompatActivity implements Coustent.ViewIml {
private Presenter presenter;
private RecyclerView rcv;
private CheckBox ck;
private TextView priceAll;
private TextView numAll;
private List data1;
private ManAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
presenter = new Presenter();
LoadData();
}
private void initView() {
rcv = findViewById(R.id.shop_all_rcv);
ck = findViewById(R.id.shop_all_ck);
priceAll = findViewById(R.id.shop_all_price);
numAll = findViewById(R.id.shop_all_num);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rcv.setLayoutManager(linearLayoutManager);
ck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkSeller(ck.isChecked());
myAdapter.notifyDataSetChanged();
}
});
}
private void checkSeller(boolean bool){
double totalPrice = 0;
int num = 0;
for (int i= 0;i list = data1.get(i).getList();
for (int j=0;j list) {
double totalPrice = 0;
int num = 0;
int totalNulm = 0;
for (int i= 0;i list1 = list.get(i).getList();
for (int j=0;j