美文网首页
电商订单模块实现思路

电商订单模块实现思路

作者: 盗梦如画 | 来源:发表于2017-05-16 17:38 被阅读362次

电商订单

一般订单我们都要遇到 一个店铺 购买多个商品 需要展现的结果 类似于淘宝

我们用一个recyclerview 实现, 把一个店铺看成 三个不同的item, 店铺信息头部 是一个 item 商品是内容, 价格什么的是底部item

1:从服务器获取数据:

bean.png
**获取数据如上 : 我们需要获取整个界面的大订单 依次存储到一个新集合中 **
// 
public class OrderDataHelper {
    public static Collection<Object> getDataAfterHandle(List<OrdersBean.DataBean.ListBean> listBean) {
        List<Object> dataList = new ArrayList<Object>();
        //遍历整个大订单
        for (OrdersBean.DataBean.ListBean bean : listBean) {//循环依次的把 头 商品 底部信息存储到集合
      
            int ordersId = bean.getOrdersId();
            int pharmacyId = bean.getPharmacyId();
            String pharmacyName = bean.getPharmacyName();
            //头  创建一个对象存储头的数据
            OrderPayInfo orderPayInfo = new OrderPayInfo();
            orderPayInfo.setOrdersId(ordersId);
            orderPayInfo.setPharmacyId(pharmacyId);
            orderPayInfo.setPharmacyName(bean.getPharmacyName());
            orderPayInfo.setState(bean.getState());
            dataList.add(orderPayInfo);   //用集合 存储第一个对象里面 的头布局信息

            // 商品  把 每个订单 里面的每个对象遍历出来  存储到集合
            List<OrdersBean.DataBean.ListBean.ChartListBean> chartList = bean.getChartList();
            Map<String, List<OrderGoodsItem>> orderGoodsMap = new HashMap<String, List<OrderGoodsItem>>();
            for (OrdersBean.DataBean.ListBean.ChartListBean chartListBean : chartList) {
                OrderGoodsItem orderGoodsItem = new OrderGoodsItem();
                orderGoodsItem.setPharmacyId(ordersId);
                orderGoodsItem.setOrdersId(ordersId);
                orderGoodsItem.setBetterPrice(chartListBean.getBetterPrice());
                orderGoodsItem.setDescribe(chartListBean.getDescribe());
                orderGoodsItem.setFormat(chartListBean.getFormat());
                orderGoodsItem.setImage(chartListBean.getImage());
                orderGoodsItem.setName(chartListBean.getName());
                orderGoodsItem.setNumber(chartListBean.getNumber());
                orderGoodsItem.setOriginalPrice(chartListBean.getOriginalPrice());
                dataList.add(orderGoodsItem);  //存储集合
            }

            // 底部
            GoodsOrderInfo goodsOrderInfo = new GoodsOrderInfo();
            goodsOrderInfo.setOrdersId(ordersId);
            goodsOrderInfo.setPharmacyId(pharmacyId);
            goodsOrderInfo.setBid(bean.getBid());
            goodsOrderInfo.setCreateDate(bean.getCreateDate());
            goodsOrderInfo.setFreight(bean.getFreight());
            goodsOrderInfo.setState(bean.getState());
            goodsOrderInfo.setNumbers(chartList.size());
            dataList.add(goodsOrderInfo); //存储集合
        }
        return dataList;
    }
}

 List<Object> mList = new ArrayList<>();
  mList.addAll(OrderDataHelper.getDataAfterHandle(ListBean));// 把 返回的集合存储进来
  

然后设置 Adapter 把获取的集合设置到Adapter

public class OrdersAdapter02 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private BaseViewHolder mHolder1;
    private String mToken;
    private int mLayoutPosition;
    private List<Object> data;
    private int ITEM_HEADER = 1, ITEM_CONTENT = 2, ITEM_FOOTER = 3;

    public OrdersAdapter02(Context context, String token, List<Object> list) {
        mContext = context;
        data = list;
        mToken = token;
    }
    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }


    @Override
    public int getItemViewType(int position) {  //根据不同对象 返回不同的viewholder
        if (data.get(position) instanceof OrderPayInfo) {
            return ITEM_HEADER;
        } else if (data.get(position) instanceof OrderGoodsItem) {
            return ITEM_CONTENT;
        } else if (data.get(position) instanceof GoodsOrderInfo) {
            return ITEM_FOOTER;
        }
        return ITEM_CONTENT;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == ITEM_HEADER) {//根据不同对象 加载不同布局 返回不同的viewholder
            view = LayoutInflater.from(context).inflate(R.layout.item_title_hader, parent, false);
            return new MyViewHolderHeader(view);
        } else if (viewType == ITEM_CONTENT) {
            view = LayoutInflater.from(context).inflate(R.layout.simple_list_item_1, parent, false);
            return new MyViewHolderContent(view);
        } else if (viewType == ITEM_FOOTER) {
            view = LayoutInflater.from(context).inflate(R.layout.item_title_foot, parent, false);
            return new MyViewHolderFooter(view);
        }
        return  new MyViewHolderContent(LayoutInflater.from(context).inflate(R.layout.simple_list_item_1, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolderHeader) { //根据不同的holder 绑定数据
            OrderPayInfo datas = (OrderPayInfo) data.get(position);
            MyViewHolderHeader holder1 = (MyViewHolderHeader) holder;
            setItemHoderGoodsOrderInfo(datas, holder1);
        } else if (holder instanceof MyViewHolderContent) {
            OrderGoodsItem orderGoodsItem = (OrderGoodsItem) data.get(position);
            MyViewHolderContent holder1 = (MyViewHolderContent) holder;
            setHolderOrderGoodsItem(orderGoodsItem, holder1);
        } else if (holder instanceof MyViewHolderFooter) {
            GoodsOrderInfo orderGoodsItem = (GoodsOrderInfo) data.get(position);
            MyViewHolderFooter holder1 = (MyViewHolderFooter) holder;
            setFooterGoodsOrderInfo(orderGoodsItem, holder1);
        }
        
    }


    /**
     * 设置标题
     *
     * @param datas
     * @param holder1
     */
    private void setItemHoderGoodsOrderInfo(OrderPayInfo datas, MyViewHolderHeader holder1) {
        int pharmacyId = datas.getPharmacyId();
        if (pharmacyId == 0)
            holder1.mBtLeixing.setVisibility(View.VISIBLE);  //自营
        holder1.mTvMall.setText(datas.getPharmacyName());

        switch (datas.getState()) {
            case 0:
                holder1.mTvZhuangtai.setText("待支付");
                break;
            case 1:
                holder1.mTvZhuangtai.setText("待发货");
                break;
            case 2:
                holder1.mTvZhuangtai.setText("待收货");
                break;
            case 3:
                holder1.mTvZhuangtai.setText("待评价");
                break;
            case 4:
                holder1.mTvZhuangtai.setText("退货中");
                break;
            case 5:
                holder1.mTvZhuangtai.setText("退货成功");
                break;
            case 6:
                holder1.mTvZhuangtai.setText("已完成");
                break;
            case 7:
                holder1.mTvZhuangtai.setText("已取消");
                break;
            case 8:
                holder1.mTvZhuangtai.setText("已删除");
                break;
        }

    }

    /**
     * 设置 商品
     *
     * @param orderGoodsItem
     * @param holder1
     */
    private void setHolderOrderGoodsItem(OrderGoodsItem orderGoodsItem, MyViewHolderContent holder1) {
        ImageUtils.showImageOriginal(mContext, Api.address + orderGoodsItem.getImage(), holder1.mImage);
        holder1.mTextName.setText(orderGoodsItem.getName());
        holder1.mTvDescribe.setText(orderGoodsItem.getDescribe());
        holder1.mTvOriginalPrice.setText(orderGoodsItem.getOriginalPrice() + "");
        holder1.mTvOriginalPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        holder1.mTvBetterPrice.setText(orderGoodsItem.getBetterPrice() + "");
        holder1.mTvNumber.setText("X" + orderGoodsItem.getNumber());
    }

    private void setFooterGoodsOrderInfo(GoodsOrderInfo item, MyViewHolderFooter holder1) {
        holder1.mBtCancel.setVisibility(View.GONE);
        holder1.mBtDeletes.setVisibility(View.GONE);
        holder1.mBtEvaluate.setVisibility(View.GONE);
        holder1.mBtPayment.setVisibility(View.GONE);
        holder1.mBtExpress.setVisibility(View.GONE);
        holder1.mBtReceipt.setVisibility(View.GONE);
        holder1.mBtRefund.setVisibility(View.GONE);
        holder1.mBtRefundin.setVisibility(View.GONE);
        holder1.mBtView.setVisibility(View.GONE);
        holder1.mTvNumbers.setText("共" + item.getNumbers() + "商品");//商品数量
        holder1.mTvOriginalPrice.setText(item.getBid() + "");//价格
        String createDate = item.getCreateDate();
        holder1.mTvTime.setText(createDate);
        holder1.mTvFreight.setText("运费:" + item.getFreight());
        int state = item.getState();
        switch (state) {
            case 0:
                //holder.bt_deletes.setVisibility(View.VISIBLE); //删除
                holder1.mBtPayment.setVisibility(View.VISIBLE);  //付款
                holder1.mBtCancel.setVisibility(View.VISIBLE);  //取消订单
                break;
            case 1:
                holder1.mBtRefund.setVisibility(View.VISIBLE);  //退款
                // holder.getView(R.id.bt_view).setVisibility(View.VISIBLE);  //查看订单
                break;
            case 2:
                holder1.mBtExpress.setVisibility(View.VISIBLE);  //查看物流
                holder1.mBtReceipt.setVisibility(View.VISIBLE);  //确认收货
                break;
            case 3:
                holder1.mBtDeletes.setVisibility(View.VISIBLE);  //删除
                holder1.mBtEvaluate.setVisibility(View.VISIBLE);  //评价
                break;
            case 4:
                holder1.mBtRefundin.setVisibility(View.VISIBLE); //退货中
                break;
            case 5:
                holder1.mBtDeletes.setVisibility(View.VISIBLE);  //删除
                //holder.bt_evaluate.setVisibility(View.VISIBLE);  //评价
                break;
            case 6:
                holder1.mBtDeletes.setVisibility(View.VISIBLE);  //删除
                //holder.bt_evaluate.setVisibility(View.VISIBLE);  //评价
                break;
            case 7:
                holder1.mBtDeletes.setVisibility(View.VISIBLE);  //删除
                //holder.bt_evaluate.setVisibility(View.VISIBLE);  //评价
                break;
            case 8:
                holder1.mBtDeletes.setVisibility(View.VISIBLE);  //删除
                break;
        }
    }

    public  class MyViewHolderHeader extends RecyclerView.ViewHolder {
        Button mBtLeixing;
        TextView mTvMall;
        TextView mTvZhuangtai;
        LinearLayout mTitleMall;

        public MyViewHolderHeader(View view) {
            super(view);
            mBtLeixing = (Button) view.findViewById(R.id.bt_leixing);
            mTvMall = (TextView) view.findViewById(R.id.tv_mall);
            mTvZhuangtai = (TextView) view.findViewById(R.id.tv_zhuangtai);
            mTitleMall = (LinearLayout) view.findViewById(R.id.title_mall);

        }
    }

    public  class MyViewHolderContent extends RecyclerView.ViewHolder {
        ImageView mImage;
        TextView mTextName;
        TextView mTvDescribe;
        TextView mTvBetterPrice;
        TextView mTvOriginalPrice;
        TextView mTvNumber;
        LinearLayout mItemClickLinear;

        public MyViewHolderContent(View view) {
            super(view);
            mImage= (ImageView) view.findViewById(R.id.image);
            mTextName=(TextView) view.findViewById(R.id.text_name);
            mTvDescribe=(TextView) view.findViewById(R.id.tv_describe);
            mTvBetterPrice=(TextView) view.findViewById(R.id.tv_betterPrice);
            mTvOriginalPrice=(TextView) view.findViewById(R.id.tv_originalPrice);
            mTvNumber=(TextView) view.findViewById(R.id.tv_number);
            mItemClickLinear= (LinearLayout) view.findViewById(R.id.item_click_linear);
        }
    }

   public class MyViewHolderFooter extends RecyclerView.ViewHolder {
        TextView mTvNumbers;
        TextView mTvOriginalPrice;
        TextView mTvFreight;
        TextView mTvTime;
        Button mBtDeletes;
        Button mBtCancel;
        Button mBtEvaluate;
        Button mBtPayment;
        Button mBtExpress;
        Button mBtRefund;
        Button mBtRefundin;
        Button mBtReceipt;
        Button mBtView;

        public MyViewHolderFooter(View view) {
            super(view);
            mTvNumbers=(TextView) view.findViewById(R.id.tv_numbers);
            mTvOriginalPrice=(TextView) view.findViewById(R.id.tv_originalPrice);
            mTvFreight=(TextView) view.findViewById(R.id.tv_freight);
            mTvTime=(TextView) view.findViewById(R.id.tv_time);
            mBtDeletes=(Button) view.findViewById(R.id.bt_deletes);
            mBtCancel=(Button) view.findViewById(R.id.bt_cancel);
            mBtEvaluate=(Button) view.findViewById(R.id.bt_evaluate);
            mBtPayment=(Button) view.findViewById(R.id.bt_payment);
            mBtExpress=(Button) view.findViewById(R.id.bt_express);
            mBtRefund=(Button) view.findViewById(R.id.bt_refund);
            mBtRefundin=(Button) view.findViewById(R.id.bt_refundin);
            mBtReceipt=(Button) view.findViewById(R.id.bt_receipt);
            mBtView=(Button) view.findViewById(R.id.bt_view);
        }
    }

}

demo代码中 点击事件还没有实现 需要用的时候 设置接口回调就好了

相关文章

网友评论

      本文标题:电商订单模块实现思路

      本文链接:https://www.haomeiwen.com/subject/ttnlxxtx.html