购物车

自定义计数器

public class CustomCounter extends LinearLayout {
    private Context mContext;
    private EditText editText_num;
    private ImageView imageView_jia;
    private ImageView imageView_jian;
    private CartChildAdapter adapter;
    private List list;
    private int num;
    private int position;

    public CustomCounter(Context context) {
        super(context);
        mContext=context;
        list=new ArrayList<>();
        initView();

    }

    public CustomCounter(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        list=new ArrayList<>();
        initView();
    }
    public void setData(CartChildAdapter adapter, final List list, int i){
        this.adapter=adapter;
        this.list=list;
        position=i;
         num = list.get(i).getNum();
         editText_num.setText(num+"");
         editText_num.addTextChangedListener(new TextWatcher() {
             @Override
             public void beforeTextChanged(CharSequence s, int start, int count, int after) {

             }

             @Override
             public void onTextChanged(CharSequence s, int start, int before, int count) {
                  try {
                      String s1 = String.valueOf(s);
                      num = Integer.parseInt(s1);
                      list.get(position).setNum(num);

                  }catch (Exception e){
                      e.printStackTrace();
                      list.get(position).setNum(1);
                  }
                  if(callBackListener!=null){
                      callBackListener.getCheckState();
                  }

             }

             @Override
             public void afterTextChanged(Editable s) {

             }
         });
    }
    private void initView() {
        View view=View.inflate(mContext, R.layout.count_item,null);
        editText_num = view.findViewById(R.id.edit_num);
        imageView_jia = view.findViewById(R.id.image_jia);
        imageView_jian = view.findViewById(R.id.image_jian);
        imageView_jia.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
             num++;
             list.get(position).setNum(num);
             editText_num.setText(num+"");
              callBackListener.getCheckState();
              adapter.notifyItemChanged(position);
            }
        });
        imageView_jian.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(num>1){
                    num--;
                    list.get(position).setNum(num);
                    editText_num.setText(num+"");
                    callBackListener.getCheckState();
                    adapter.notifyItemChanged(position);
                }else {
                    Toast.makeText(mContext,"数量不能小于0",Toast.LENGTH_SHORT).show();
                }
            }
        });
        addView(view);
    }
    public onCallBackListener callBackListener;
    public void setOnCallBackListener(onCallBackListener backListener){
        this.callBackListener=backListener;
    }

    public interface onCallBackListener{
        void getCheckState();
    }
}

商品的adapter

public class CartChildAdapter extends RecyclerView.Adapter {
    private List list;
    private Context context;

    public CartChildAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(context).inflate(R.layout.cart_child_item,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {

               viewHolder.price.setText("¥"+list.get(i).getPrice()+".00");
               viewHolder.title.setText(list.get(i).getTitle());
               String images = list.get(i).getImages();
               String[] split = images.split("\\|");
                 Glide.with(context).load(split[0]).into(viewHolder.imageView);
                viewHolder.checkBox.setChecked(list.get(i).isChecked());
               viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                   @Override
                   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                       list.get(i).setChecked(viewHolder.checkBox.isChecked());
                       if(callBackListener!=null){
                           callBackListener.getCheckState();
                       }
                   }
               });


                //设置数量
                 viewHolder.customCounter.setData(this,list,i);
                 viewHolder.customCounter.setOnCallBackListener(new CustomCounter.onCallBackListener() {
                     @Override
                     public void getCheckState() {
                           if(callBackListener!=null){
                               callBackListener.getCheckState();
                           }
                     }
                 });


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_check)
        CheckBox checkBox;
        @BindView(R.id.item_price)
        TextView price;
        @BindView(R.id.item_title)
        TextView title;
        @BindView(R.id.item_simple)
        ImageView imageView;
        @BindView(R.id.custom_counter)
        CustomCounter customCounter;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    public onCallBackListener callBackListener;
    public void setOnCallBackListener(onCallBackListener backListener){
        this.callBackListener=backListener;
    }

    public interface onCallBackListener{
        void getCheckState();
    }
    public void updateCheck(boolean isSelect){
       for(CartBean.DataBean.ListBean listBean:list){
           listBean.setChecked(isSelect);
       }
        notifyDataSetChanged();
    }
}

商家的adapter

public class CartAdapter extends RecyclerView.Adapter {
    private List list;
    private Context context;

    public CartAdapter(Context context) {
        this.context = context;
        list=new ArrayList<>();
    }

    public void setList(List list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(context).inflate(R.layout.cart_item,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
         viewHolder.textView_seller.setText(list.get(i).getSellerName());
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(OrientationHelper.VERTICAL);
        viewHolder.recyclerView.setLayoutManager(manager);
        List alist = this.list.get(i).getList();
        final CartChildAdapter   adapter = new CartChildAdapter(alist,context);
        viewHolder.recyclerView.setAdapter(adapter);
       viewHolder.checkBox_goods.setChecked(list.get(i).isChecked());
         adapter.setOnCallBackListener(new CartChildAdapter.onCallBackListener() {
             @Override
             public void getCheckState() {
                 if(callBackListener!=null){
                     callBackListener.getCheckState();
                 }
                 List list1 = list.get(i).getList();
                 boolean isCheckAll=true;
                 for (CartBean.DataBean.ListBean listBean:list1){
                     if(!listBean.isChecked()){
                      isCheckAll=false;
                     }
                 }
                 list.get(i).setChecked(isCheckAll);
                 viewHolder.checkBox_goods.setChecked(isCheckAll);
             }

         });


         viewHolder.checkBox_goods.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 list.get(i).setChecked(viewHolder.checkBox_goods.isChecked());
                 adapter.updateCheck(viewHolder.checkBox_goods.isChecked());
             }
         });


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.text_seller)
        TextView textView_seller;
        @BindView(R.id.child_recycler)
        RecyclerView recyclerView;
        @BindView(R.id.check_goods)
        CheckBox checkBox_goods;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    public onCallBackListener callBackListener;

    public void setOnCallBackListener(onCallBackListener backListener){
        this.callBackListener=backListener;
    }

    public interface onCallBackListener{
        void getCheckState();
    }
}

activity

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        iPresenter=new IPresenterImpl(this);

        ButterKnife.bind(this);

        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(OrientationHelper.VERTICAL);
        recyclerView.setLayoutManager(manager);

        adapter=new CartAdapter(this);
        recyclerView.setAdapter(adapter);

        adapter.setOnCallBackListener(new CartAdapter.onCallBackListener() {
            @Override
            public void getCheckState() {
                double totalPrice=0;
                int num=0;
                int totalNum=0;
                for(int a=0;a list = data1.get(a).getList();
                    for (int i=0;i map=new HashMap<>();
        map.put("uid",26522+"");
        iPresenter.startRequestPost(Apis.URL_CART,map, CartBean.class);
    }
    @OnClick({R.id.check_all})
    public void getViewClick(View view){
        switch (view.getId()){
            case R.id.check_all:
                checkAll(checkBox_all.isChecked());
                adapter.notifyDataSetChanged();
                break;
        }
    }
    public void checkAll(boolean isCheckAll){
        double totalPrice=0;
        int num=0;
        for(int a=0;a list = data1.get(a).getList();
            data1.get(a).setChecked(isCheckAll);
            for (int i=0;i

你可能感兴趣的:(网络,网络请求)