购物车基础展示数据(MVP+Brvah+OKgo)

首先是MVP逻辑架构,此处省略
导入下列我们需要的依赖

//MetrialDesign:Design库
    implementation 'com.android.support:design:28.+'
    //Butterknife:根据反射注入框架
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    //Brvah:RecyclerView快速开发框架
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
    //SmartRefreshLayout
    implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-19'
    implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-19'
    //没有使用特殊Header,可以不加这行
    //okgo
    //必须使用
    implementation 'com.lzy.net:okgo:3.0.4'
    implementation 'com.google.code.gson:gson:2.8.5'
    //图片加载框架
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

导入Brvah需要在Gradle下你自己得到包下添加以下代码

maven { url "https://jitpack.io" }

购物车主要的布局分为三部分:
1.activity_main



   
    

        
    

    


        


        

2.recycler_busniess_item


    

        

        
    

    

3.recycler_goods_item



    


        

            

            

            

                

                
            

        
    

在M层使用OKGO进行网络请求(get方法):尽量把请求 的结果打印一下,方便找错

public class ShoppingModelImpl implements ShoppingContract.IModel {
    @Override
    public void containData(final CallBack callBack) {
        OkGo.get(Constant.SHOPPINGCART_URL).execute(new StringCallback() {
            @Override
            public void onSuccess(Response response) {
                String reponseData = response.body().toString();
                //回显
                callBack.onCall(reponseData);
                Log.i("cx", "onSuccess: "+reponseData);
            }
        });
    }
}

在view层的数据展示方法下面进行数据处理
1.数据解析(Gson)

  ShoppingCartBean cartBean = new Gson().fromJson(reponseData, ShoppingCartBean.class);

2.添加外层商家条目的数据源

List businessList = cartBean.getData();

创建外层商家适配器

public class BusinessAdapter extends BaseQuickAdapter {
    public BusinessAdapter(int layoutResId, @Nullable List data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ShoppingCartBean.DataBean item) {
        //设置商品名称
       helper.setText(R.id.tv_business_name,item.getSellerName());
       //设置商品子商品条目
        RecyclerView rv_goods = helper.getView(R.id.rv_goods);
        //自商品条目数据源
        List goodsList = item.getList();
        LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        rv_goods.setLayoutManager(manager);
        //设置子条目适配器
        GoodsAdapter goodsAdapter = new GoodsAdapter(R.layout.recycler_goods_item, goodsList);

        rv_goods.setAdapter(goodsAdapter);

    }
}

4.添加布局管理器

LinearLayoutManager manager = new LinearLayoutManager(mContxt, LinearLayoutManager.VERTICAL, false);
rvBusiness.setLayoutManager(manager);

4.设置适配器

 BusinessAdapter adapter = new BusinessAdapter(R.layout.recycler_business_item, businessList);
        rvBusiness.setAdapter(adapter);

5.创建商品适配器

public class GoodsAdapter extends BaseQuickAdapter {
    public GoodsAdapter(int layoutResId, @Nullable List data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ShoppingCartBean.DataBean.ListBean item) {
        helper.setText(R.id.tv_goodsPrice,"¥:"+item.getPrice());
         helper.setText(R.id.tv_goodsTitle,item.getTitle());
        ImageView iv_goodIcon = helper.getView(R.id.iv_goodsIcon);
        String imagesString = item.getImages();
        String[] imagesStr = imagesString.split("\\|");
        Glide.with(mContext).load(imagesStr[0]).into(iv_goodIcon);
        
    }
}

你可能感兴趣的:(购物车基础展示数据(MVP+Brvah+OKgo))