Retrofit

 bottomTabBar.init(getSupportFragmentManager())
                .setImgSize(0,0)
                .setFontSize(20)
                .setTabPadding(0,0,0)
                .addTabItem("资讯",R.mipmap.ic_launcher,FragOne.class)
                .addTabItem("生活",R.mipmap.ic_launcher_round,FragTwo.class);
 public Frag1 frag1;

    public Frag1Adapter(List<JsonBean.DataBean> list, Context mContext, Frag1 frag1) {
        this.list = list;
        this.mContext = mContext;
        this.frag1 = frag1;
 //适配器
        MyAdapter2 myAdapter2 = new MyAdapter2(list.get(i).getList(),mContext,fragOne);
        holder.item_rec.setAdapter(myAdapter2);

**1.Retrofit **

private Retrofit retrofit;
    private static RetroFatUtil retroFatUtil;


    //拦截器
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Log.e("tab","request=="+request.url().toString());
            Response proceed = chain.proceed(request);
            return proceed;
        }
    }).build();

    //构造方法私有化
    public Retrofit getRetrofit(){

        retrofit = new Retrofit.Builder()
                .baseUrl(Url.TOU)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(okHttpClient)
                .build();

        return retrofit;
    }

    //单例模式
    public static synchronized RetroFatUtil getInRetroFatUtil(){

        if (retroFatUtil==null){
            retroFatUtil = new RetroFatUtil();
        }
        return retroFatUtil;
    }

     public <T>T setRetrofat(Class<T> tClass){
        return getRetrofit().create(tClass);
     }

2.Api

    @GET
    public Observable<ResponseBody> ApiGet(@Url String url);

3.M

 RetroFatUtil inRetroFatUtil = RetroFatUtil.getInRetroFatUtil();
        Api api = inRetroFatUtil.setRetrofat(Api.class);

        api.ApiGet(Url.BASEURL)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ResponseBody>() {
                    @Override
                    public void call(ResponseBody responseBody) {
                        try {
                            String string = responseBody.string();
                            Gson gson = new Gson();
                            JsonBean jsonBean = gson.fromJson(string, JsonBean.class);
                            myCallBack.succer(jsonBean);

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

4.p

@Override
    public void onShu(String url) {
        myModel.setMyModel(new MyModel.MyCallBack() {
            @Override
            public void succer(Object object) {
                ((ContantInterface.VIterface)tt).shouZhan(object);
            }
        });
        myModel.MyGet();
    }

5.V

  //实例化布局管理器
        LinearLayoutManager linear = new LinearLayoutManager(this);
        linear.setOrientation(LinearLayoutManager.VERTICAL);
        xrec.setLayoutManager(linear);

        pInterface.onShu(null);

        //适配器
        myAdapter = new MyAdapter(list,MainActivity.this);
        xrec.setAdapter(myAdapter);

        //上拉下拉,刷新
        xrec.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                list.clear();
                pager=1;
                pInterface.onShu(null);
                xrec.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                pager++;
                pInterface.onShu(null);
                xrec.loadMoreComplete();
            }
        });

        //全选反选
        mainCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mainCheck.isChecked()){
                    setChecAll(true);
                    total();
                }else{
                    setChecAll(false);
                    //为false时是不选中的,总价也是0
                    mainQian.setText(0+"");
                    total();
                }
                myAdapter.notifyDataSetChanged();
            }
        });
    }

    //全选反选
    public void setChecAll(boolean b){
        for (int i = 0; i < list.size(); i++) {
           list.get(i).isSelect = b;
            for (int j = 0; j < list.get(i).getList().size(); j++) {
                 list.get(i).getList().get(j).itemSelcet = b;
            }
        }
        myAdapter.notifyDataSetChanged();
    }


    //价钱
    public void total(){
        int num_pice=0;
        //计算价钱时,最外层,最里层循环遍历
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.get(i).getList().size(); j++) {
                 //判断商品是否是选中状态
                 if (list.get(i).getList().get(j).itemSelcet){
                     //商品价格
                     int price = (int) list.get(i).getList().get(j).getPrice();
                     //商品数量
                     int num = list.get(i).getList().get(j).getNum();
                     int to = price*num;
                     num_pice += to;
                 }
            }
           mainQian.setText(num_pice+"");
        }
    }

    //商家选中或者不选中
    /*i,商家的下标
      tab,孩子的下标,-1默认值
      b,是否选中状态
    */
    public void setShangAll(int i,int tab,boolean b){
        list.get(i).isSelect = b;
        if (tab!=-1){
            list.get(i).getList().get(tab).itemSelcet = false;
        }else{
            for (int j = 0; j < list.get(i).getList().size() ; j++) {
                list.get(i).getList().get(j).itemSelcet = b;
            }
        }
        total();
        myAdapter.notifyDataSetChanged();
    }

    @Override
    public void shouZhan(Object object) {
         JsonBean jsonBean = (JsonBean)object;
         List<JsonBean.DataBean> data = jsonBean.getData();
         list.addAll(data);
         myAdapter.notifyDataSetChanged();
    }

6.Adapter1

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

    public List<JsonBean.DataBean> list;
    public Context mContext;
    public MainActivity mainActivity;

    public MyAdapter(List<JsonBean.DataBean> list, Context mContext) {
        this.list = list;
        this.mContext = mContext;
        mainActivity = (MainActivity) mContext;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, null);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, final int i) {

        boolean select = list.get(i).isSelect();
        holder.item_check.setChecked(select);

        String sellerName = list.get(i).getSellerName();
        holder.item_text.setText(sellerName);

        //布局管理器
        LinearLayoutManager linear = new LinearLayoutManager(mContext);
        linear.setOrientation(LinearLayoutManager.VERTICAL);
        holder.item_rec.setLayoutManager(linear);

        //适配器
        MyAdapter2 myAdapter2 = new MyAdapter2(list.get(i).getList(),mainActivity);
        holder.item_rec.setAdapter(myAdapter2);

        //商家变化时,价格的变化
        holder.item_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainActivity.setShangAll(i,-1,holder.item_check.isChecked());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class Holder extends RecyclerView.ViewHolder {

        public CheckBox item_check;
        public TextView item_text;
        public RecyclerView item_rec;

        public Holder(@NonNull View itemView) {
            super(itemView);

            item_check = itemView.findViewById(R.id.item_check);
            item_text = itemView.findViewById(R.id.item_text);
            item_rec = itemView.findViewById(R.id.item_rec);
        }
    }
}

6.Adapter7

 boolean itemSelcet = list.get(i).isItemSelcet();
        holder.item2_check.setChecked(itemSelcet);

        String title = list.get(i).getTitle();
        holder.item2_name.setText(title);

        int num = list.get(i).getNum();
        holder.item2_he.setText(num+"");

        String price = list.get(i).getPrice()+"";
        holder.item2_pice.setText(price);

        String images = list.get(i).getImages();
        holder.item2_image.setImageURI(Uri.parse(images));

        //加
        holder.item2_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = list.get(i).getNum();
                num+=1;
                list.get(i).setNum(num);
                mainActivity.total();
                notifyDataSetChanged();
            }
        });

        //减
        holder.item2_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num2 = list.get(i).getNum();
                if (num2>=1){
                    num2-=1;
                }else{
                    num2=0;
                }
                list.get(i).setNum(num2);
                mainActivity.total();
                notifyDataSetChanged();
            }
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值