解决列表嵌套列表的卡顿

现象:在实际开发的过程中特么是商城开发的过程中UI妹子经常要设计出列表中含有列表的页面,类似淘宝的订单页面,这对程序员来说这简直是一件坑爹的事情,因为列表嵌套列表会出现很多问题,比如页面卡顿,违背了视图的重复利用的机制,等等。


解决思路:在实相信很多开发者都在想能不能不使用listview嵌套listview呢或者不使用RecyclerView嵌套RecyclerView呢,其实这是可以了,我们利用listview或者RecyclerView的多视图getItemViewType()方法就能解决这个问题。


实现:

       case1:一般后台返回的数据格式为集合嵌套集合的方式,我们需要想办法去掉products这个节点,换句话说我们需要对数据进行从新出来(当然如果你有足够好的后台,他们会帮你处理)

"list": [

       {

         "products": [

         ]

       }

     ]

case 2:我们先根据上面的数据结构做好业务bean,这bean并对数据进行去products节点的处理

1.这个是对应products接口内部的bean

public class ProductInfo implements Serializable {
    private String userOrderId="";
    private String product_count="";
    private String product_price="";
    private String product_name="";
    private int type;

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getUserOrderId() {
        return userOrderId;
    }

    public void setUserOrderId(String userOrderId) {
        this.userOrderId = userOrderId;
    }

    public String getProduct_count() {
        return product_count;
    }

    public void setProduct_count(String product_count) {
        this.product_count = product_count;
    }

    public String getProduct_price() {
        return product_price;
    }

    public void setProduct_price(String product_price) {
        this.product_price = product_price;
    }

    public String getProduct_name() {
        return product_name;
    }

    public void setProduct_name(String product_name) {
        this.product_name = product_name;
    }
}

2.这是对应list节点内部的bean,通过下面这个bean我们可以很容易看出这是平时我们列表嵌套列表经常使用的数据格式

package com.tiangxi.business.ui.efuneat;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2017/3/23.
 *
 *  "packid": "1",
 "create_time": "2017-03-22 09:42:31",
 "orderId": "80",
 "user_name": "lieren",
 "mobile": "13143778502",
 "taking_code": "123456",
 */
public class EatOrderInfo implements Serializable {
    private String packid="";
    private String create_time="";
    private String orderId="";
    private String user_name="";
    private String mobile="";
    private String taking_code="";
    private List<ProductInfo> products;
    private String totalCount="";
    private String orderStatus="";
    private String totalPrice="";

    public String getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(String totalCount) {
        this.totalCount = totalCount;
    }

    public String getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }

    public String getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(String totalPrice) {
        this.totalPrice = totalPrice;
    }

    public String getPackid() {
        return packid;
    }

    public void setPackid(String packid) {
        this.packid = packid;
    }

    public String getCreate_time() {
        return create_time;
    }

    public void setCreate_time(String create_time) {
        this.create_time = create_time;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getTaking_code() {
        return taking_code;
    }

    public void setTaking_code(String taking_code) {
        this.taking_code = taking_code;
    }

    public List<ProductInfo> getProducts() {
        return products;
    }

    public void setProducts(List<ProductInfo> products) {
        this.products = products;
    }
}

                          图1

       3:我们如何去掉图1中箭头所指向的数据集合呢?这个时候我们多建立一个数据bean ,这时候我们看出图1和图2的区别就是箭头所指的地方

         

public class History0rderInfo implements Serializable {
    private String packid="";
    private String create_time="";
    private String orderId="";
    private String user_name="";
    private String mobile="";
    private String taking_code="";
    private ProductInfo products;
    private int type;
    private String totalCount="";
    private String orderStatus="";
    private String totalPrice="";

    public String getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(String totalCount) {
        this.totalCount = totalCount;
    }

    public String getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }

    public String getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(String totalPrice) {
        this.totalPrice = totalPrice;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getPackid() {
        return packid;
    }

    public void setPackid(String packid) {
        this.packid = packid;
    }

    public String getCreate_time() {
        return create_time;
    }

    public void setCreate_time(String create_time) {
        this.create_time = create_time;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getTaking_code() {
        return taking_code;
    }

    public void setTaking_code(String taking_code) {
        this.taking_code = taking_code;
    }

    public ProductInfo getProducts() {
        return products;
    }

    public void setProducts(ProductInfo products) {
        this.products = products;
    }
}

                 

                                                              图2

                  c

case 3:如图3所以这是传统列表嵌套列表中的一个item,现在我们要将这个item视图切分为三块如图3中的黄色区域,每一个块是一个视图,将红色区域中,原来的内嵌列表中的一个item,作为第二视图,底部为第三视图


                                                         图3

case 4: 我们最终的目标就是将图1的数据模型转换为图2的数据模型,我们需要通过遍历或者克隆的方式将数据转换过来,最重要的一个就是一定要给每个HistoryOrderInfo设置一个类型setType();这样就对应case3中的三个视图

private void eatOrderInfoToHistoryOrderInfo(List<EatOrderInfo> eatOrderInfoList){
    for (int i=0;i<eatOrderInfoList.size();i++){
        EatOrderInfo eatOrderInfo=eatOrderInfoList.get(i);
        History0rderInfo history0rderInfo=new History0rderInfo();
        history0rderInfo.setType(1);
        history0rderInfo.setCreate_time(eatOrderInfo.getCreate_time());
        history0rderInfo.setPackid(eatOrderInfo.getPackid());
        history0rderInfo.setUser_name(eatOrderInfo.getUser_name());
        history0rderInfo.setTaking_code(eatOrderInfo.getTaking_code());
        history0rderInfo.setMobile(eatOrderInfo.getMobile());
        history0rderInfo.setOrderId(eatOrderInfo.getOrderId());
        history0rderInfo.setOrderStatus(eatOrderInfo.getOrderStatus());
        history0rderInfo.setTotalCount(eatOrderInfo.getTotalCount());
        history0rderInfo.setTotalPrice(eatOrderInfo.getTotalPrice());
        history0rderInfoList.add(history0rderInfo);
        List<ProductInfo>productInfoList=eatOrderInfo.getProducts();
        for (int j=0;j<productInfoList.size();j++){
            History0rderInfo historyProductInfo=new History0rderInfo();
            historyProductInfo.setType(2);
            historyProductInfo.setProducts(productInfoList.get(j));
            history0rderInfoList.add(historyProductInfo);
        }
        History0rderInfo historyBodyInfo=new History0rderInfo();
        historyBodyInfo.setType(3);
        historyBodyInfo.setCreate_time(eatOrderInfo.getCreate_time());
        historyBodyInfo.setPackid(eatOrderInfo.getPackid());
        historyBodyInfo.setUser_name(eatOrderInfo.getUser_name());
        historyBodyInfo.setTaking_code(eatOrderInfo.getTaking_code());
        historyBodyInfo.setMobile(eatOrderInfo.getMobile());
        historyBodyInfo.setOrderId(eatOrderInfo.getOrderId());
        historyBodyInfo.setOrderStatus(eatOrderInfo.getOrderStatus());
        historyBodyInfo.setTotalCount(eatOrderInfo.getTotalCount());
        historyBodyInfo.setTotalPrice(eatOrderInfo.getTotalPrice());
        history0rderInfoList.add(historyBodyInfo);
    }
    adapter.notifyDataSetChanged();
}

case5:最后我们利用listview或者 RecyclerView的多视图功能来解决,通过对type类型的判断来添加相应的视图

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    if (1==viewType){
        return new HeadViewHolder(LayoutInflater.from(context).inflate(R.layout.item_history_head,null));
    }else if (2==viewType){
        View convertView=LayoutInflater.from(context).inflate(R.layout.item_history_body,null);
        convertView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return new BodyViewHolder(convertView);
    }else if (3==viewType){
        View convertView=LayoutInflater.from(context).inflate(R.layout.item_history_bottom,null);
        convertView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return new BottomViewHolder(convertView);
    }
    return null;
}

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

































































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值