购物车(仿订餐)

效果图

购物车(仿订餐)_第1张图片

Activity页面

public class MainActivity extends AppCompatActivity implements MyInterFace {

    private TextView mSumPrice;
    private TextView mCount;
    private RecyclerView mLeftRecycler, mRightRecycler;
    private Presenter presenter;
    private com.example.lee.shopping_lx4.mLeftAdapter mLeftAdapter;
    private com.example.lee.shopping_lx4.mRightAdapter mRightAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSumPrice = findViewById(R.id.goods_sum_price);
        mCount = findViewById(R.id.goods_number);
        mLeftRecycler = findViewById(R.id.left_recycler);
        mRightRecycler = findViewById(R.id.right_recycler);
        //p层
        presenter = new Presenter(this);


        mLeftRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRightRecycler.setLayoutManager(new LinearLayoutManager(this));

        mLeftAdapter = new mLeftAdapter(this);
        //点击时,调用点击接口
        mLeftAdapter.setOnItemClickListenter(new mLeftAdapter.OnItemClickListenter() {
            @Override
            public void onItemClick(Shop shop) {
                mRightAdapter.clearList();//清空数据
                mRightAdapter.addItem(shop.getList());
                mRightAdapter.notifyDataSetChanged();
            }
        });

        mRightAdapter = new mRightAdapter(this);
        //点击加减,计算总价和总数
        mRightAdapter.setOnNum(new mRightAdapter.OnNum() {
            @Override
            public void onNum() {
                //将商品列表传入
                calculatePrice(mLeftAdapter.getList());
            }
        });
        //设置适配器
        mLeftRecycler.setAdapter(mLeftAdapter);
        mRightRecycler.setAdapter(mRightAdapter);
        //调用p层
        presenter.show();

    }

    //计算价格和计算总数
    private void calculatePrice(List list) {
        double totalPrice = 0;
        int totalNum = 0;
        for (int i = 0; i < list.size(); i++) {
            Shop shop = list.get(i);
            for (int j = 0; j < shop.getList().size(); j++) {
                Bean bean = shop.getList().get(j);
                //计算价格
                totalPrice = totalPrice + bean.getNum() * bean.getPrice();
                totalNum = totalNum + bean.getNum();
            }
        }
        mSumPrice.setText("总价:"+totalPrice);
        mCount.setText(""+totalNum);
    }
    //请求网络成功
    @Override
    public void success(Recult data) {
        List data1 = (List) data.getData();
        //计算数量和总价
        calculatePrice(data1);

        mLeftAdapter.addItem(data1);

        //得到默认选中的shop,设置上颜色和背景
        Shop shop = data1.get(1);
        shop.setTextColor(0xff000000);
        shop.setBackground(R.color.white);
        mRightAdapter.addItem(shop.getList());
        //刷新适配器
        mRightAdapter.notifyDataSetChanged();
        mLeftAdapter.notifyDataSetChanged();
    }
    //请求网络失败
    @Override
    public void fail(Recult recult) {
        Toast.makeText(this, "" + recult.getMsg(), Toast.LENGTH_SHORT).show();
    }
}

左边adapter页面

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

    //点击接口
    private OnItemClickListenter onItemClickListenter;

    public void setOnItemClickListenter(OnItemClickListenter onItemClickListenter) {
        this.onItemClickListenter = onItemClickListenter;
    }

    public interface OnItemClickListenter {
        void onItemClick(Shop shop);
    }


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

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

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
        final Shop shop = list.get(i);
        viewHolder.textView.setText(shop.getSellerName());
        viewHolder.textView.setBackgroundResource(shop.getBackground());
        viewHolder.textView.setTextColor(shop.getTextColor());
        //点击切换视图
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置点击颜色
                for (int j = 0; j  getList() {
        return list;
    }

    public void addItem(List data) {
        if (data != null) {
            list.addAll(data);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.left_text);
        }
    }
}

右边adapter

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

    public mRightAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
    }
    
    private OnNum onNum;

    public void setOnNum(OnNum onNum) {
        this.onNum = onNum;
    }

    //计算价格数量接口
    public interface OnNum{
        void onNum();
    }


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

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
        //设置值
        final Bean bean = list.get(i);
        viewHolder.text.setText(bean.getTitle());
        viewHolder.price.setText(String.valueOf(bean.getPrice()));
        viewHolder.sum.setText(String.valueOf(bean.getNum()));
        String images = bean.getImages();
        String[] split = images.split("!");
        Glide.with(context).load(split[0]).into(viewHolder.image);
        //加减数量
        viewHolder.jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = viewHolder.sum.getText().toString();
                int i1 = Integer.parseInt(string);
                i1++;
                bean.setNum(i1);
                notifyDataSetChanged();
                onNum.onNum();//计算价格
            }
        });
        viewHolder.jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = viewHolder.sum.getText().toString();
                int i1 = Integer.parseInt(string);
                i1--;
                if (i1 < 1) {
                    Toast.makeText(context, "数量不能为0", Toast.LENGTH_SHORT).show();
                    return;
                }
                bean.setNum(i1);
                notifyDataSetChanged();
                onNum.onNum();//计算价格
            }
        });
    }

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

    public void addItem(List list1) {
        if (list != null) {
            list.addAll(list1);
        }
    }

    public void clearList() {
        list.clear();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView text;
        TextView price;
        ImageView image;
        Button jia;
        Button jian;
        TextView sum;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
            price = itemView.findViewById(R.id.text_price);
            image = itemView.findViewById(R.id.image);
            jia = itemView.findViewById(R.id.jia);
            jian = itemView.findViewById(R.id.jian);
            sum = itemView.findViewById(R.id.sum);
        }
    }
}

p层 m层 还有bean类与二级列表实现购物车中的一样

你可能感兴趣的:(购物车(仿订餐))