购物车参考

main主布局  导入RecyclerView依赖


    

        

            

            

        

        

            

            

                

                

            

            

        

    


item条目  把图片放在drawable-xhdpi


    
        

            

            

        

    

    


        

            

            

                

                
            
            

                

                

                

                    



                    

                        

                        

                        

                    
                
                


 
  
Bean类ShopCartBean
导入List包
/**
     * shopId : 2
     * shopName : null
     * cartlist : [{"id":2,"shopId":2,"shopName":null,"productId":2,"productName":null,"color":null,"size":null,"price":null,"count":null}]
     */

    private int shopId;
    private Object shopName;
    /**
     * id : 2
     * shopId : 2
     * shopName : null
     * productId : 2
     * productName : null
     * color : null
     * size : null
     * price : null
     * count : null
     */

    private List cartlist;


    public int getShopId() {
        return shopId;
    }

    public void setShopId(int shopId) {
        this.shopId = shopId;
    }

    public Object getShopName() {
        return shopName;
    }

    public void setShopName(Object shopName) {
        this.shopName = shopName;
    }

    public List getCartlist() {
        return cartlist;
    }

    public void setCartlist(List cartlist) {
        this.cartlist = cartlist;
    }


    public static class CartlistBean {
        private int id;
        private int shopId;
        private String shopName;
        private int productId;
        private String productName;
        private String color;
        private String size;
        private String price;
        private String defaultPic;
        private int count;
        private boolean isSelect = true;
        private int isFirst = 2;
        private boolean isShopSelect = true;

        public String getDefaultPic() {
            return defaultPic;
        }

        public void setDefaultPic(String defaultPic) {
            this.defaultPic = defaultPic;
        }

        public boolean getIsShopSelect() {
            return isShopSelect;
        }

        public void setShopSelect(boolean shopSelect) {
            isShopSelect = shopSelect;
        }

        public int getIsFirst() {
            return isFirst;
        }

        public void setIsFirst(int isFirst) {
            this.isFirst = isFirst;
        }

        public boolean getIsSelect() {
            return isSelect;
        }

        public void setSelect(boolean select) {
            isSelect = select;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getShopId() {
            return shopId;
        }

        public void setShopId(int shopId) {
            this.shopId = shopId;
        }

        public String getShopName() {
            return shopName;
        }

        public void setShopName(String shopName) {
            this.shopName = shopName;
        }

        public int getProductId() {
            return productId;
        }

        public void setProductId(int productId) {
            this.productId = productId;
        }

        public String getProductName() {
            return productName;
        }

        public void setProductName(String productName) {
            this.productName = productName;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public String getSize() {
            return size;
        }

        public void setSize(String size) {
            this.size = size;
        }

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }
    }


 
  
创建适配器ShopCartAdapter
导包
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.List;

public class ShopCartAdapter extends RecyclerView.Adapter{
    private Context context;
    private List data;
    private View headerView;
    private OnDeleteClickListener mOnDeleteClickListener;
    private OnEditClickListener mOnEditClickListener;
    private OnResfreshListener mOnResfreshListener;

    public ShopCartAdapter(Context context, List data){
        this.context = context;
        this.data = data;
    }

    @Override
    public ShopCartAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(context).inflate(R.layout.item_car, parent, false);
        return new ShopCartAdapter.MyViewHolder(view);
    }



    @Override
    public void onBindViewHolder(final ShopCartAdapter.MyViewHolder holder, final int position) {

        Glide.with(context).load(data.get(position).getDefaultPic()).into(holder.img);
        if (position > 0) {
            if (data.get(position).getShopId() == data.get(position - 1).getShopId()) {
                holder.llShopCartHeader.setVisibility(View.GONE);
            } else {
                holder.llShopCartHeader.setVisibility(View.VISIBLE);
            }
        }else {
            holder.llShopCartHeader.setVisibility(View.VISIBLE);
        }
        holder.content.setText(data.get(position).getProductName());
        holder.shopName.setText(data.get(position).getShopName());
        holder.price.setText("¥" + data.get(position).getPrice());
        holder.num.setText(data.get(position).getCount() + "");

        if(mOnResfreshListener != null){
            boolean isSelect = false;
            for(int i = 0;i < data.size(); i++){
                if(!data.get(i).getIsSelect()){
                    isSelect = false;
                    break;
                }else{
                    isSelect = true;
                }
            }
            mOnResfreshListener.onResfresh(isSelect);
        }

        holder.jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(data.get(position).getCount() > 1) {
                    int count = data.get(position).getCount() - 1;
                    if (mOnEditClickListener != null) {
                        mOnEditClickListener.onEditClick(position, data.get(position).getId(), count);
                    }
                    data.get(position).setCount(count);
                    notifyDataSetChanged();
                }
            }
        });

        holder.jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = data.get(position).getCount() + 1;
                if(mOnEditClickListener != null){
                    mOnEditClickListener.onEditClick(position,data.get(position).getId(),count);
                }
                data.get(position).setCount(count);
                notifyDataSetChanged();
            }
        });

        //商家选中状态图片
        if(data.get(position).getIsSelect()){
            holder.smallSelect.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected));
        }else {
            holder.smallSelect.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected));
        }

        //商品选中状态图片
        if(data.get(position).getIsShopSelect()){
            holder.select.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected));
        }else {
            holder.select.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected));
        }

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用删除的接口
                Delete(v,position);
            }
        });

        holder.smallSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data.get(position).setSelect(!data.get(position).getIsSelect());
                //通过循环找出不同商铺的第一个商品的位置
                for(int i = 0;i < data.size(); i++){
                    if(data.get(i).getIsFirst() == 1) {
                        //遍历去找出同一家商铺的所有商品的勾选情况
                        for(int j = 0;j < data.size();j++){
                            //如果是同一家商铺的商品,并且其中一个商品是未选中,那么商铺的全选勾选取消
                            if(data.get(j).getShopId() == data.get(i).getShopId() && !data.get(j).getIsSelect()){
                                data.get(i).setShopSelect(false);
                                break;
                            }else{
                                //如果是同一家商铺的商品,并且所有商品是选中,那么商铺的选中全选勾选
                                data.get(i).setShopSelect(true);
                            }
                        }
                    }
                }
                notifyDataSetChanged();
            }
        });

        holder.select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(data.get(position).getIsFirst() == 1) {
                    data.get(position).setShopSelect(!data.get(position).getIsShopSelect());
                    for(int i = 0;i < data.size();i++){
                        if(data.get(i).getShopId() == data.get(position).getShopId()){
                            data.get(i).setSelect(data.get(position).getIsShopSelect());
                        }
                    }
                    notifyDataSetChanged();
                }
            }
        });

    }

    private void Delete(final View view, final int position){
        //调用删除某个规格商品的接口
        if(mOnDeleteClickListener != null){
            mOnDeleteClickListener.onDeleteClick(view,position,data.get(position).getId());
        }
        data.remove(position);
        Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
        //重新排序,标记所有商品不同商铺第一个的商品位置
        MainActivity.isSelectFirst(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        int count = (data == null ? 0 : data.size());
        if(headerView != null){
            count++;
        }
        return count;
    }


    class MyViewHolder extends RecyclerView.ViewHolder
    {
        private ImageView select;//CheckBox
        private TextView shopName;//商家名字
        private TextView content;//商品内容
        private TextView price;//商品价格
        private TextView num;//商品数量
        private ImageView smallSelect;//小CheckBox
        private ImageView jian;//减
        private ImageView jia;//加
        private Button delete;//删除
        private ImageView img;//商品图片
        private LinearLayout llShopCartHeader;

        public MyViewHolder(View view)
        {
            super(view);
            llShopCartHeader = (LinearLayout) view.findViewById(R.id.ll_shopcart_header);
            select = (ImageView) view.findViewById(R.id.img_select);
            shopName = (TextView) view.findViewById(R.id.tv_shopName);
            content = (TextView) view.findViewById(R.id.tv_content);
            price = (TextView) view.findViewById(R.id.tv_price);
            num = (TextView) view.findViewById(R.id.tv_num);
            smallSelect = (ImageView) view.findViewById(R.id.iv_smallSelect);
            jian = (ImageView) view.findViewById(R.id.iv_jian);
            jia = (ImageView) view.findViewById(R.id.iv_jia);
            img = (ImageView) view.findViewById(R.id.iv_img);
            delete = (Button) view.findViewById(R.id.iv_delete);
        }
    }


    public View getHeaderView(){
        return headerView;
    }

    private ShopCartAdapter.OnItemClickListener mOnItemClickListener;
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(ShopCartAdapter.OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnDeleteClickListener{
        void onDeleteClick(View view, int position, int cartid);
    }

    public void setOnDeleteClickListener(OnDeleteClickListener mOnDeleteClickListener){
        this.mOnDeleteClickListener = mOnDeleteClickListener;
    }

    public interface OnEditClickListener{
        void onEditClick(int position, int cartid, int count);
    }

    public void setOnEditClickListener(OnEditClickListener mOnEditClickListener){
        this.mOnEditClickListener = mOnEditClickListener;
    }

    public interface OnResfreshListener{
        void onResfresh(boolean isSelect);
    }

    public void setResfreshListener(OnResfreshListener mOnResfreshListener){
        this.mOnResfreshListener = mOnResfreshListener;
    }

}


 
  
MainActivity类
导包
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import licancan.com.recyclerviewcar.adapter.ShopCartAdapter;
import licancan.com.recyclerviewcar.api.Api;
import licancan.com.recyclerviewcar.bean.ShopCartBean;
import licancan.com.recyclerviewcar.utils.OkHttpUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private TextView jiesuan,allselect,tv_Allnum;
    private RecyclerView recyclerView;
    private ShopCartAdapter adapter;
    private List mAllOrderList = new ArrayList<>();
    private ArrayList mGoPayList = new ArrayList<>();
    private List mHotProductsList = new ArrayList<>();
    private TextView CarAllPrice;
    private int mCount,mPosition;
    private float allPrice;
    private boolean mSelect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initMyData();

        //创建LinearLayoutManager管理器
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //创建适配器  关联适配器
        adapter = new ShopCartAdapter(this,mAllOrderList);
        recyclerView.setAdapter(adapter);
        //删除商品接口
        adapter.setOnDeleteClickListener(new ShopCartAdapter.OnDeleteClickListener() {
            @Override
            public void onDeleteClick(View view, int position,int cartid) {
                adapter.notifyDataSetChanged();
            }
        });
        //修改数量接口
        adapter.setOnEditClickListener(new ShopCartAdapter.OnEditClickListener() {
            @Override
            public void onEditClick(int position, int cartid, int count) {
                mCount = count;
                mPosition = position;
            }
        });
        //判断全选按钮
        adapter.setResfreshListener(new ShopCartAdapter.OnResfreshListener() {
            @Override
            public void onResfresh( boolean isSelect) {
                mSelect = isSelect;
                if(isSelect){
                    Drawable left = getResources().getDrawable(R.drawable.shopcart_selected);
                    allselect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
                }else {
                    Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected);
                    allselect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
                }
                float allPrice = 0;
                int allNum = 0;
                allPrice = 0;
                mGoPayList.clear();
                for(int i = 0;i < mAllOrderList.size(); i++)
                    if(mAllOrderList.get(i).getIsSelect()) {
                        allPrice += Float.parseFloat(mAllOrderList.get(i).getPrice()) * mAllOrderList.get(i).getCount();
                        allNum += 1;
                        mGoPayList.add(mAllOrderList.get(i));
                    }
                allPrice = allPrice;
                CarAllPrice.setText("总价:" + allPrice);
                tv_Allnum.setText("共" + allNum + "件商品");
                jiesuan.setText("结算"+"("+allNum+")");
            }
        });

        //全选
        allselect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSelect = !mSelect;
                if(mSelect){
                    Drawable left = getResources().getDrawable(R.drawable.shopcart_selected);
                    allselect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
                    for(int i = 0;i < mAllOrderList.size();i++){
                        mAllOrderList.get(i).setSelect(true);
                        mAllOrderList.get(i).setShopSelect(true);
                    }
                }else{
                    Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected);
                    allselect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
                    for(int i = 0;i < mAllOrderList.size();i++){
                        mAllOrderList.get(i).setSelect(false);
                        mAllOrderList.get(i).setShopSelect(false);
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
        adapter.notifyDataSetChanged();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        allselect = (TextView) findViewById(R.id.allselect);
        CarAllPrice = (TextView) findViewById(R.id.tv_shopcart_totalprice);
        tv_Allnum = (TextView) findViewById(R.id.tv_Allnum);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        jiesuan = (TextView) findViewById(R.id.jiesuan);
    }

    private void initData() {
        OkHttpUtils.doGet(Api.URL, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
            }
        });
    }
    private void initMyData(){
        for(int i = 0;i < 2;i ++){
            ShopCartBean.CartlistBean sb = new ShopCartBean.CartlistBean();
            sb.setShopId(1);
            sb.setPrice("5199.0");
            sb.setDefaultPic("https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg");
            sb.setProductName("Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机");
            sb.setShopName("商家1");
            sb.setCount(1);
            mAllOrderList.add(sb);
        }

        for(int i = 0;i < 1;i ++){
            ShopCartBean.CartlistBean sb = new ShopCartBean.CartlistBean();
            sb.setShopId(2);
            sb.setPrice("272.0");
            sb.setDefaultPic("https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg");
            sb.setProductName("北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g");
            sb.setShopName("商家6");
            sb.setCount(1);
            mAllOrderList.add(sb);
        }

        for(int i = 0;i < 2;i ++){
            ShopCartBean.CartlistBean sb = new ShopCartBean.CartlistBean();
            sb.setShopId(3);
            sb.setPrice("2434.0");
            sb.setDefaultPic("https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg");
            sb.setProductName("小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】");
            sb.setShopName("商家8");
            sb.setCount(1);
            mAllOrderList.add(sb);
        }

        for(int i = 0;i < 1;i ++){
            ShopCartBean.CartlistBean sb = new ShopCartBean.CartlistBean();
            sb.setShopId(4);
            sb.setPrice("4362.0");
            sb.setDefaultPic("https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg");
            sb.setProductName("全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G");
            sb.setShopName("商家10");
            sb.setCount(1);
            mAllOrderList.add(sb);
        }

        /*OkHttpClient ok = new OkHttpClient.Builder().build();
        FormBody.Builder fb = new FormBody.Builder();
        fb.add("uid","148");
        FormBody build = fb.build();
        Request re = new Request.Builder().post(build).url("http://120.27.23.105/product/getCarts")
                .build();
        ok.newCall(re).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                System.out.println("string = " + string);
                try {

                    JSONObject json = new JSONObject(string);
                    JSONArray data = json.getJSONArray("data");

                    for (int i = 0; i  list){
        if(list.size() > 0) {
            list.get(0).setIsFirst(1);
            for (int i = 1; i < list.size(); i++) {
                if (list.get(i).getShopId() == list.get(i - 1).getShopId()) {
                    list.get(i).setIsFirst(2);
                } else {
                    list.get(i).setIsFirst(1);
                }
            }
        }
    }
}


 
  

你可能感兴趣的:(购物车参考)