流式布局,购物车的简单使用

一:导入相关依赖二:根据相关接口写入bean类三:根据接口写接口请求类 Api  如:uid = 85public interface Api { //购物车 @GET("product/getCarts") Observable<ShopBean> gouwuche(@Query("uid") String uid); }四:网络...
摘要由CSDN通过智能技术生成

一:导入相关依赖

二:根据相关接口写入bean类

三:根据接口写接口请求类 Api  如:uid = 85

public interface Api {

    //购物车
    @GET("product/getCarts")
    Observable<ShopBean> gouwuche(@Query("uid") String uid);
    
}

四:网络请求类

public class HttpUtils {
    private static final String BASE_URL = "http://www.zhaoapi.cn/";
    private Retrofit retrofit;

    private static final class SINGLE_INSTANCE{
        private static final HttpUtils _INSTANCE = new HttpUtils();
    }

    public static HttpUtils getInstance(){
        return SINGLE_INSTANCE._INSTANCE;
    }

    private HttpUtils(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(buildOkHttpClient())
                .build();
    }

    private OkHttpClient buildOkHttpClient() {
        return new OkHttpClient.Builder()
                .readTimeout(5000,TimeUnit.MILLISECONDS)
                .writeTimeout(5000,TimeUnit.MILLISECONDS)
                .build();
    }

    public <T> T create(Class<T> clazz){
        return retrofit.create(clazz);
    }
}

五:view层

public interface ShopView {
    void onSuccess(ShopBean shopBean);

    void onFailed(Throwable t);
}

六:model层

public class ShopModel {
    public Observable<ShopBean> shopData(String uid){
        Api api = HttpUtils.getInstance().create(Api.class);
        Observable<ShopBean> shop = api.gouwuche(uid);
        return shop;
    }
}

七:presenter层

public class ShopPresenter {
    public ShopView shopView;
    private final ShopModel shopModel;

    public ShopPresenter(ShopView shopView) {
        this.shopView = shopView;
        shopModel = new ShopModel();
    }

    public void gouwuche(String uid){
        shopModel.shopData(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ShopBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ShopBean shopBean) {
                        //Log.e("aaaaa",shopBean.getData().get(1).getSellerName());
                        shopView.onSuccess(shopBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                       // Log.e("sss",e+"");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void attach(ShopView iv) {
        this.shopView = iv;
    }

    //解决内存泄漏
    public void dettach(){
        shopView = null;
    }
}

八:adapter层  ShopAdapter

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> {

    private Context context;
    private List<ShopBean.DataBean> list;
    private ShopZiAdapter shopZiAdapter;

    public ShopAdapter(Context context, List<ShopBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    public interface OnShoppingClickListener{
        void OnShoppingClick(int postion,boolean ischeked);
    }
    private OnShoppingClickListener shoppingClickListener;

    public void setOnShoppingClickListener(OnShoppingClickListener listener){
        this.shoppingClickListener=listener;
    }
    private ShopZiAdapter.OnAddDecreaseProductListener productListener;
    public void setOnAddDecreaseProductListener(ShopZiAdapter.OnAddDecreaseProductListener listener){
        this.productListener=listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.shop_adapter, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final ShopBean.DataBean dataBean = list.get(position);
        holder.txtshoppername.setText(list.get(position).getSellerName());
        RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(context);
        holder.rvproduct.setLayoutManager(layoutManager);
        final ShopZiAdapter adapter = new ShopZiAdapter(context,dataBean.getList());
        if(productListener!=null){
            adapter.setOnAddDecreaseProductListener(productListener);
        }
        adapter.setOnProductClickListener(new ShopZiAdapter.OnProductClickListener() {
            @Override
            public void OnProductClickListener(int postion, boolean ischecked) {
                if(!ischecked){
                    dataBean.setChecked(false);
                    shoppingClickListener.OnShoppingClick(postion,false);
                }else{
                    boolean isAllProductSelected=true;
                    for (ShopBean.DataBean.ListBean listBean : dataBean.getList()) {
                        if(!listBean.isChecked()){
                            isAllProductSelected=false;
                            break;
                        }
                    }
                    dataBean.setChecked(isAllProductSelected);
                    shoppingClickListener.OnShoppingClick(postion,true);
                }
                notifyDataSetChanged();
                productListener.onChange(0,0);
            }
        });
        holder.rvproduct.setAdapter(adapter);
        holder.cbshopper.setOnCheckedChangeListener(null);
        holder.cbshopper.setChecked(dataBean.isChecked());
        holder.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值