An overview of the

At present, like Taobao and display list, there are multiple item display requirements, probably most if not done, the first glance is ListView to nested ListView, although this can be done, but this will lead to excessive drawing of the phone, why? When an Item is loaded, the Adapter in the Item will be updated, and the apapter will update its own Item, which will lead to the loading time of an X *n Item.

solution

So when I have a problem like this, we can actually treat the whole List as a List. How do we do that? It’s a complex ListView:



The header is divided into a type

The content is divided into a type

The top is divided into a type

Then you can use the ListView or RecyclerView getItemViewType and getViewTypeCount, especially if you’re using the PulltoRefresh ListView, then you need to get getViewTypeCount back You can see the PullToRefreshListView will add the Header to the listView.

The difficulties in

The difficulty actually lies in the data requirements for the server. The server usually returns a single List containing multiple lists:

{ "code": "0000", "desc": null, "token": "ad762d27-ced6-4092-b415-ddad8ee0b98e__1472123395714", "msg": [ { "amount": "Consignee ":" Andrea ", "address": "A area of Tianfu Software Park ", "orderItem": [# list data {"thumbnail": NULL, "quantity": 1, "price": 601, "name": "id": 11}], "freight": 0, "orderStatus": "unconfirmed", "productCount": 1, "shippingStatus": Shipped ", "phone": "15892999216", "areaName": "Cn "," ID ": 9, "SN ": "20160825202", "paymentStatus": "New Jersey "," California ", "createDate": 1472123141000}, {"amount": 601, "Consignee ":" Andrea ", "Address ":" A zone of Tianfu Software Park ", "orderItem": [# listing data {"thumbnail": null, "quantity": 1, "price": 601, "name": "id": 10}], "freight": 0, "orderStatus": "Unconfirmed ", "productCount": 1, "shippingStatus": "unshipped", "phone": "15892999216", "areaName": 8, "sn": "20160825102", "paymentStatus": "unpaid", "createDate": 1472122855000 } ], "page": { "total": 8, "pageNumber": 1, "pageSize": 2 } }Copy the code

Encounter such data, do we ask the server to help me change it? What if it doesn’t change? Of course it can be fixed. What to do? So we can split the data. How? : – Divide an item into three parts – The first part: keep the item relationship and display the top data – the second part: Keep the item relationship and display the purchase list data – the third part: Keep the item relationship and need to display the top purchase quantity, price, payment status display or buttons and text

Through the above description, basically explained the difficulties encountered.

Code implementation

I just posted the relevant core code:

/ * * * * * @ split orders author Tanck * * @ @ param list return * / private list handleList list (list) {if (null = = list | | 0 = = list.size()) return list; List temp = new ArrayList<>(); for (int i = 0; i < list.size(); I++) {// unpack an order ++total; bean = list.get(i); bean.setType(1); // Add a type field to the bean, so that it can be used to set, in order to determine the type of the bean in adapter temp.add(bean); For (int j = 0; j < list.get(i).getOrderItem().size(); J++) {// unpack each order in an order ++total; bean = (ShopOrderListResponse.MsgBean) bean.clone(); // Deep copy is used here because the data is being modified and the connection is given later. Bean.settype (2); bean.setOrderPosition(j); temp.add(bean); // add a content} // ++total; bean = (ShopOrderListResponse.MsgBean) bean.clone(); bean.setType(3); temp.add(bean); } return temp; }Copy the code

Once you have the split, then you need to show it. Check out my adapter:

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    @Override
    public int getViewTypeCount() {
        return 5;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        ViewHolder holder = null;

        if (null == convertView) {
            holder = new ViewHolder();
            switch (getItemViewType(position)) {
                case 1: // head
                    convertView = View.inflate(context, R.layout.item_shop_order_list_head, null);
                    holder.time = (TextView) convertView.findViewById(R.id.tv_item_order_time);
                    holder.status = (TextView) convertView.findViewById(R.id.tv_item_order_status);
                    holder.detailHead = (RelativeLayout) convertView.findViewById(R.id.rl_item_order_detail_head);
                    break;
                case 2:// content
                    convertView = View.inflate(context, R.layout.item_shop_order_list_content, null);
                    holder.icon = (ImageView) convertView.findViewById(R.id.iv_sp_my_order_item);
                    holder.orderMoney = (TextView) convertView.findViewById(R.id.tv_sp_my_order_money);
                    holder.number = (TextView) convertView.findViewById(R.id.tv_sp_my_order_number);
                    holder.orderName = (TextView) convertView.findViewById(R.id.tv_sp_my_order_name);
                    holder.content = (RelativeLayout) convertView.findViewById(R.id.rl_sp_my_order_content);
                    break;
                case 3: // bottom
                    convertView = View.inflate(context, R.layout.item_shop_order_list_bottom, null);
                    holder.money = (TextView) convertView.findViewById(R.id.tv_item_order_money);
                    holder.cancel = (TextView) convertView.findViewById(R.id.tv_item_order_cancel);
                    holder.pay = (TextView) convertView.findViewById(R.id.tv_item_order_pay);
                    holder.detailBottom = (LinearLayout) convertView.findViewById(R.id.ll_item_order_detail_bottom);
                    holder.opt = (RelativeLayout) convertView.findViewById(R.id.rl_item_order_opt);
                    holder.back = (TextView) convertView.findViewById(R.id.tv_item_order_back);
                    holder.backGoods = (TextView) convertView.findViewById(R.id.tv_item_order_back_goods);
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String tempPayStatus = list.get(position).getPaymentStatus();
        String tempOrderStatus = list.get(position).getOrderStatus();
        String tempShippingStatus = list.get(position).getShippingStatus();
        holder.setListener(position);
        switch (getItemViewType(position)) {
            case 1:// head

                holder.time.setText(context.getString(R.string.order_time) + getDate(list.get(position).getCreateDate()));
                /**
                 * 订单状态
                 未确认
                 unconfirmed,

                 已确认
                 confirmed,

                 已完成
                 completed,

                 已取消
                 cancelled,

                 已失效
                 failure
                 */
                if ("failure".equals(tempOrderStatus)) {
                    holder.status.setText(R.string.time_out_close);
                } else if ("unpaid".equals(tempPayStatus) && "cancelled".equals(tempOrderStatus)) {
                    holder.status.setText(R.string.cancel_order);
                } else if ("unpaid".equals(tempPayStatus)) {
                    holder.status.setText(R.string.unpaid);
                } else if ("paid".equals(tempPayStatus) && "unconfirmed".equals(tempOrderStatus)) {
                    holder.status.setText(R.string.paid);
                } else if ("paid".equals(tempPayStatus) && "confirmed".equals(tempOrderStatus)) {
                    holder.status.setText(R.string.unsend);
                } else if ("shipped".equals(tempShippingStatus)) {
                    holder.status.setText(R.string.unrec);
                } else if ("received".equals(tempShippingStatus)) {
                    holder.status.setText(R.string.received);
                } else {
                    holder.status.setText(R.string.not_no);
                }
                holder.detailHead.setOnClickListener(holder);
                break;
            case 2:// content
                int tempP = list.get(position).getOrderPosition();//获取orderItem真实位置
                String iconString = list.get(position).getOrderItem().get(tempP).getThumbnail();
                if (StringUtil.isEmpty(iconString))
                    holder.icon.setScaleType(ImageView.ScaleType.FIT_XY);
                XUtilsImageLoader.getHomeAdvImg(context, R.drawable.udesk_defualt_failure, holder.icon, iconString);
                holder.orderName.setText(list.get(position).getOrderItem().get(tempP).getName());
                holder.number.setText("x" + list.get(position).getOrderItem().get(tempP).getQuantity());
                holder.orderMoney.setText("¥" + list.get(position).getOrderItem().get(tempP).getPrice());
                holder.content.setOnClickListener(holder);
                break;
            case 3://bottom
                holder.money.setText("共" + list.get(position).getProductCount() + "件商品 实付:¥" + list.get(position).getAmount());
                holder.detailBottom.setOnClickListener(holder);
                if ("failure".equals(tempOrderStatus)) {//交易关闭
                    holder.opt.setVisibility(View.GONE);
                } else if ("unpaid".equals(tempPayStatus) && "cancelled".equals(tempOrderStatus)) {//没给钱,点击已取消
                    holder.opt.setVisibility(View.GONE);
                } else if ("unpaid".equals(tempPayStatus)) {//待付款
                    holder.opt.setVisibility(View.VISIBLE);
                    // TODO 展示取消及付款,隐藏退货,退款
                    holder.pay.setVisibility(View.VISIBLE);
                    holder.cancel.setVisibility(View.VISIBLE);
                    holder.back.setVisibility(View.GONE);
                    holder.backGoods.setVisibility(View.GONE);
                    holder.pay.setOnClickListener(holder);
                    holder.cancel.setOnClickListener(holder);
                } else if ("paid".equals(tempPayStatus) && "unconfirmed".equals(tempOrderStatus)) {//给了钱,没确认
                    holder.opt.setVisibility(View.VISIBLE);
                    // TODO 展示退款,隐藏取消及付款,退货
                    holder.cancel.setVisibility(View.GONE);
                    holder.pay.setVisibility(View.GONE);
                    holder.backGoods.setVisibility(View.GONE);
                    holder.back.setVisibility(View.VISIBLE);
                    holder.back.setOnClickListener(holder);
                } else if ("paid".equals(tempPayStatus) && "confirmed".equals(tempOrderStatus)) {//给了钱,且确认了
                    holder.opt.setVisibility(View.VISIBLE);
                    // TODO 展示退款,隐藏取消及付款,退货
                    holder.cancel.setVisibility(View.GONE);
                    holder.pay.setVisibility(View.GONE);
                    holder.back.setVisibility(View.VISIBLE);
                    holder.backGoods.setVisibility(View.GONE);
                    holder.back.setOnClickListener(holder);
                } else if ("shipped".equals(tempShippingStatus)) {//待收货
                    holder.opt.setVisibility(View.VISIBLE);
                    // TODO 展示退款,隐藏取消及付款,退货(后期可能会增加查看物流)
                    holder.cancel.setVisibility(View.GONE);
                    holder.pay.setVisibility(View.GONE);
                    holder.backGoods.setVisibility(View.GONE);
                    holder.back.setVisibility(View.VISIBLE);
                    holder.back.setOnClickListener(holder);
                } else if ("received".equals(tempShippingStatus)) {//收货了,待评价
                    holder.opt.setVisibility(View.VISIBLE);
                    // TODO 展示退货,隐藏取消及付款,退款
                    holder.back.setVisibility(View.GONE);
                    holder.cancel.setVisibility(View.GONE);
                    holder.pay.setVisibility(View.GONE);
                    holder.backGoods.setVisibility(View.VISIBLE);
                    holder.backGoods.setOnClickListener(holder);
                } else { // 等待确认
                    holder.opt.setVisibility(View.GONE);
                }

                break;
        }

        return convertView;
    }Copy the code

Prototype pattern learning (see here)

conclusion

We can use a ListView or RecyclerView to do a lot of complex types. Why not? Because of its own reuse mechanism, reuse mechanism here is not much to say. So this way we can make complex lists at will without running out of time.