Android列表优化,可以参考参考

作者: 温开创 | 来源:发表于2017-08-15 11:48 被阅读193次

    OrderList

    优化列表,大量数据也不会造成卡顿,代码简洁可读性高

    使用AutoSwipeRefreshLayout+RecyclerView+通用Adapter(RecyclerView)

    这里Adapter是用张鸿洋封装的Adapter 使用方便(这里没有使用到最简化)

    ##效果图

    gif

    List有三种数据类型:

    1、OrderGoodsInfo 表示每个小订单的头部信息(订单号、订单状态、店铺名称)

    2、OrderGoodsItem 表示小订单中的商品

    3、OrderPayInfo 表示大订单的支付信息(金额、订单状态)

    将列表一个item分成3个部分(布局一般是固定的)

    然后通过Adapter根据不同部分来加载不同布局级数据

    具体实现:

    数据处理:将请求回来的数据赋值到(OrderGoodsInfo,OrderGoodsItem,OrderPayInfo)

    public classOrderDataHelper {

    /**

    * List有三种数据类型:

    * 1、OrderGoodsInfo表示每个小订单的头部信息(订单号、订单状态、店铺名称)

    * 2、OrderGoodsItem表示小订单中的商品

    * 3、OrderPayInfo表示大订单的支付信息(金额、订单状态)

    *

    *@paramresultList

    *@return

    */

    public staticList getDataAfterHandle(List resultList) {

    List dataList =newArrayList();

    //遍历每一张大订单

    for(OrderSummary orderSummary : resultList) {

    //大订单支付的金额核定单状态

    OrderPayInfo orderPayInfo =newOrderPayInfo();

    orderPayInfo.setTotalAmount(orderSummary.getTotalPrice());

    orderPayInfo.setStatus(orderSummary.getStatus());

    orderPayInfo.setId(orderSummary.getId());

    //小订单商品的

    List orderDetailList = orderSummary.getOrderDetailList();

    Map> orderGoodsMap =newHashMap>();

    Map orderInfoMap =newHashMap();

    //遍历每个大订单里面的小订单

    for(OrderGoodsItem orderGoodsItem : orderDetailList) {

    //获取小订单里面的商铺信息的订单号

    String orderCode = orderGoodsItem.getOrder().getOrderCode();

    orderGoodsItem.setOrderid(orderSummary.getId());

    //拿到相对应订单号的所有商品

    List goodsList = orderGoodsMap.get(orderCode);

    //如果goodsList为空,则新建;而且把这个订单号的orderGoodsMap持有订单的对象goodsList

    if(goodsList ==null) {

    goodsList =newArrayList<>();

    orderGoodsMap.put(orderCode, goodsList);

    }

    //goodsList添加商品的对象,因为orderGoodsMap已经持有这个订单号的goodsList对象,所以不用重新put

    goodsList.add(orderGoodsItem);

    //把小订单的店铺信息赋给GoodsOrderInfo对象,并加入到orderInfoMap

    OrderGoodsInfo orderInfo = orderInfoMap.get(orderCode);

    if(orderInfo ==null) {

    orderInfo =newOrderGoodsInfo();

    orderInfo.setOrderCode(orderCode);

    orderInfo.setShopName(orderGoodsItem.getOrder().getShopName());

    orderInfo.setStatus(orderGoodsItem.getOrder().getStatus());

    orderInfoMap.put(orderCode, orderInfo);

    }

    }

    //把所有数据按照头部、内容和尾部三个部分排序好

    Set keySet = orderGoodsMap.keySet();

    for(String orderCode : keySet) {

    dataList.add(orderInfoMap.get(orderCode));

    dataList.addAll(orderGoodsMap.get(orderCode));

    }

    dataList.add(orderPayInfo);

    }

    returndataList;

    }

    }

    Adapter:

    public classOrderAdapterextendsRcCommonAdapter {

    privateContextcontext;

    privateListdata;

    private intITEM_HEADER=1,ITEM_CONTENT=2,ITEM_FOOTER=3;

    publicOrderAdapter(Context context, List data){

    super(context,1, data);

    this.context= context;

    this.data= data;

    }

    @Override

    publicRcViewHolder onCreateViewHolder(ViewGroup parent,intviewType) {

    View view =null;

    if(viewType ==ITEM_HEADER) {

    view = LayoutInflater.from(context).inflate(R.layout.item_allorder_header, parent,false);

    }else if(viewType ==ITEM_CONTENT){

    view = LayoutInflater.from(context).inflate(R.layout.item_allorder_content, parent,false);

    }else if(viewType ==ITEM_FOOTER){

    view = LayoutInflater.from(context).inflate(R.layout.item_allorder_footer, parent,false);

    }

    return newRcViewHolder(context,view);

    }

    @Override

    public intgetItemViewType(intposition) {

    if(data.get(position)instanceofOrderGoodsInfo) {

    returnITEM_HEADER;

    }else if(data.get(position)instanceofOrderGoodsItem){

    returnITEM_CONTENT;

    }else if(data.get(position)instanceofOrderPayInfo){

    returnITEM_FOOTER;

    }

    returnITEM_CONTENT;

    }

    @Override

    protected voidconvert(RcViewHolder holder, Object data,final intposition) {

    if(holder.getItemViewType()==ITEM_HEADER){

    OrderGoodsInfo datas = (OrderGoodsInfo)data;

    holder.setText(R.id.tv_orderno,"订单编号:"+ datas.getOrderCode())

    .setText(R.id.tv_shopname,datas.getShopName());

    if(datas.getStatus().equals("0")){

    holder.setText(R.id.tv_state,"待付款");

    }else if(datas.getStatus().equals("1")){

    holder.setText(R.id.tv_state,"待发货");

    }else if(datas.getStatus().equals("2")){

    holder.setText(R.id.tv_state,"待收货");

    }else if(datas.getStatus().equals("3")){

    holder.setText(R.id.tv_state,"交易完成");

    }

    }else if(holder.getItemViewType()==ITEM_CONTENT) {

    OrderGoodsItem datas = (OrderGoodsItem)data;

    holder.setGlidePic(context,R.id.iv_pic,datas.getProductPic())

    .setText(R.id.tv_title,datas.getProductName())

    .setText(R.id.tv_num,"共"+ datas.getCount() +"件")

    .setText(R.id.tv_price,"¥"+ datas.getTotalPrice());

    final intpos = datas.getOrderid();

    finalString name = datas.getProductName();

    holder.setOnClickListener(R.id.ll,newView.OnClickListener() {

    @Override

    public voidonClick(View v) {

    Toast.makeText(context,name,Toast.LENGTH_SHORT).show();

    }

    });

    }else if(holder.getItemViewType()==ITEM_FOOTER) {

    OrderPayInfo datas = (OrderPayInfo)data;

    holder.setText(R.id.tv_total,datas.getTotalAmount() +"");

    final intpos = datas.getId();

    if(datas.getStatus().equals("0")){

    holder.setText(R.id.tv_submit,"付款");

    }else if(datas.getStatus().equals("2")){

    holder.setText(R.id.tv_submit,"确认收货");

    }else{

    holder.setText(R.id.tv_submit,"再次购买");

    }

    finalString status = datas.getStatus();

    holder.setOnClickListener(R.id.tv_submit,newView.OnClickListener() {

    @Override

    public voidonClick(View v) {

    mOnBtClickListener.onBtClick(status,position);

    }

    });

    }

    }

    privateOnBtClickListenermOnBtClickListener;

    public interfaceOnBtClickListener{

    voidonBtClick(String status,intposition);

    }

    public voidsetOnBtClickListener(OnBtClickListener mOnBtClickListener){

    this.mOnBtClickListener= mOnBtClickListener;

    }

    }

    改Demo学习总结而成,就当做笔记参考吧

    github链接:github.com/wenkaichuang0729/OrderList

    相关文章

      网友评论

        本文标题:Android列表优化,可以参考参考

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