MVP+Retrofit+Cart

Main

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.cart03.adapter.CartAdapter;
import com.example.lenovo.cart03.adapter.ChildAdapter;
import com.example.lenovo.cart03.bean.CartBean;
import com.example.lenovo.cart03.cart.presenter.CartPresenter;
import com.example.lenovo.cart03.cart.view.IView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;



public class MainActivity extends AppCompatActivity implements IView{


    @BindView(R.id.rv_top)
    RelativeLayout rvTop;
    @BindView(R.id.cb_total_select)
    CheckBox cbTotalSelect;
    @BindView(R.id.txt_total_price)
    TextView txtTotalPrice;
    @BindView(R.id.btn_calu)
    Button btnCalu;
    @BindView(R.id.rl_bottom)
    RelativeLayout rlBottom;
    @BindView(R.id.rv_shopper)
    RecyclerView rvShopper;





    private List<CartBean.DataBean> list;
    private CartPresenter presenter;
    private CartAdapter cartAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initData();

    }

    private void initData() {

        presenter = new CartPresenter();
        presenter.attach(this);
        presenter.getCart(71);

        list = new ArrayList<>();

        cartAdapter = new CartAdapter(this,list);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        rvShopper.setLayoutManager(layoutManager);

        rvShopper.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

        cartAdapter.setCartClickListener(new CartAdapter.onCartClickListener() {
            @Override
            public void onCartListener(int position, boolean isChecked) {

                if (!isChecked){
                    cbTotalSelect.setChecked(false);
                }else {
                    boolean isAllCartSelected = true;
                    for (CartBean.DataBean dataBean : list) {
                        if (!dataBean.isChecked()){
                            isAllCartSelected = false;
                            break;
                        }
                    }
                    cbTotalSelect.setChecked(isAllCartSelected);
                    AllSum();
                }

            }


        });

        cartAdapter.setAddCreateClickListener(new ChildAdapter.onAddCreateClickListener() {
            @Override
            public void onChange(int position, int num) {
                AllSum();
            }
        });

        rvShopper.setAdapter(cartAdapter);
        cbTotalSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = cbTotalSelect.isChecked();
                for (CartBean.DataBean dataBean : list) {

                    dataBean.setChecked(checked);
                    for (CartBean.DataBean.ListBean listBean : dataBean.getList()) {
                        listBean.setChecked(checked);
                    }

                }
                AllSum();
                cartAdapter.notifyDataSetChanged();

            }
        });


    }


    public void AllSum(){
        float sum = 0;
        for (CartBean.DataBean dataBean : list) {

            for (CartBean.DataBean.ListBean listBean : dataBean.getList()) {

                if (listBean.isChecked()){
                    sum+=listBean.getPrice()*listBean.getNum();
                }
            }

        }
       txtTotalPrice.setText("总价"+sum);

    }

    @Override
    public void getCart(List<CartBean.DataBean> dataBeans) {

        Toast.makeText(this,"sdgsdf"+dataBeans,Toast.LENGTH_LONG).show();

        if (dataBeans!= null){
            list.clear();
            list.addAll(dataBeans);
          cartAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void faild(Throwable t) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detach();
    }
}

 

 

 

//Utils

CartApi

public interface CartApi {


    //购物车
    @GET("product/getCarts")
    Observable<CartBean> cart1(@Query("uid") int uid);

}

APP

public class CartApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

ImgUtils

public class ImgUtils {

    public static String getImg(String url){
        String replace = url.replace("https","http");
        String[] split = replace.split("\\|");
        return split[0];
    }

}

 

RetrofitManager

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *
 */

public class RetrofitManager {

    private static final String BASE_URL = "http://www.zhaoapi.cn/";

    private Retrofit retrofit;

    private static final class SINGLE{
        private static RetrofitManager INSTANCE = new RetrofitManager();
    }

    public static RetrofitManager getInstance(){
        return SINGLE.INSTANCE;
    }

    private RetrofitManager(){
        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 Retrofit getRetrofit(){
        return retrofit;
    }

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

}

 

AddDecreaseView类

package com.example.lenovo.cart03.inter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lenovo.cart03.R;

import butterknife.BindView;

/**
 * 

 */

public class AddDecreaseView extends LinearLayout implements View.OnClickListener {
    @BindView(R.id.txt_decrease)
    TextView txtDecrease;
    @BindView(R.id.txt_add)
    TextView txtAdd;
    @BindView(R.id.txt_num)
    TextView txtNum;
    private int num;

    public void setNum(int num) {
        this.num = num;
        txtNum.setText(num+"");
    }

    public int getNum(int num){
        return num;
    }




    //接口回调
    public interface OnAddDecreaseClickListener{
        void add(int num);

        void decrease(int num);

    }

    private OnAddDecreaseClickListener clickListener;

    public void setClickListener(OnAddDecreaseClickListener clickListener){
        this.clickListener = clickListener;
    }


    public AddDecreaseView(Context context) {
        this(context, null);
    }

    public AddDecreaseView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AddDecreaseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {

        View.inflate(context, R.layout.item_add, this);
        txtAdd = findViewById(R.id.txt_add);
        txtDecrease = findViewById(R.id.txt_decrease);
        txtNum = findViewById(R.id.txt_num);
        txtAdd.setOnClickListener(this);
        txtDecrease.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.txt_add:
                num++;
                txtNum.setText(num+"");
                if (clickListener != null){
                    clickListener.add(num);
                }
                break;


            case R.id.txt_decrease:
                if (num>1){
                    num--;
                }
                txtNum.setText(num+"");
                if (clickListener!=null){
                    clickListener.decrease(num);
                }
                break;
        }
    }

}

 

MVP

View

public interface IView {

    void getCart(List<CartBean.DataBean> list);

    void faild(Throwable t);

}

Model

public class CartModel {

    public Observable<CartBean> getcart(int uid){
        CartApi cartApi = RetrofitManager.getInstance().create(CartApi.class);
        Observable<CartBean> cartBeanObservable = cartApi.cart1(uid);

        return cartBeanObservable;
    }

}

 

Presenter

package com.example.lenovo.cart03.cart.presenter;

import com.example.lenovo.cart03.bean.CartBean;
import com.example.lenovo.cart03.cart.model.CartModel;
import com.example.lenovo.cart03.cart.view.IView;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 
 */

public class CartPresenter {

    private CartModel model;
    private IView iv;

    public void attach(IView iv){
        this.iv = iv;
        model = new CartModel();

    }


    public void detach(){
        if (iv!=null){
            iv=null;
        }
    }

   public void getCart(int uid){

        model.getcart(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CartBean>() {
                    @Override
                    public void accept(CartBean cartBean) throws Exception {
                        if (cartBean != null & "0".equals(cartBean.getCode())) {
                            if (iv != null) {
                                iv.getCart(cartBean.getData());
                            }
                            return;
                        }
                        if (iv != null) {
                            iv.faild(new Throwable("" + cartBean.getMsg()));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (iv!=null){
                            iv.faild(throwable);
                        }
                    }
                });

   }


}

 

CartAdapter

 

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.lenovo.cart03.R;
import com.example.lenovo.cart03.bean.CartBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 
 */

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


    //接口回调一级列表商家
    public interface onCartClickListener{
        void onCartListener(int position,boolean isChecked);
    }

    private onCartClickListener cartClickListener;

    public void setCartClickListener(onCartClickListener cartClickListener){
        this.cartClickListener= cartClickListener;
    }


    // 二级列表的加减器监听
    private ChildAdapter.onAddCreateClickListener addCreateClickListener;

    public void setAddCreateClickListener(ChildAdapter.onAddCreateClickListener addCreateClickListener){
        this.addCreateClickListener = addCreateClickListener;
    }

    private Context context;
    private List<CartBean.DataBean> list;

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

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = View.inflate(context, R.layout.item_cart, null);

        return new Holder(v);
    }

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

        final CartBean.DataBean dataBean = list.get(position);

        holder.txtShopperName.setText(list.get(position).getSellerName());


        //产品列表RecyclerView适配器+布局管理器
        final RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
        final ChildAdapter childAdapter = new ChildAdapter(context,dataBean.getList());
        holder.rvProduct.setLayoutManager(layoutManager);


        if (addCreateClickListener!=null){
            childAdapter.setAddCreateClickListener(addCreateClickListener);
        }


        childAdapter.setChildClickListener(new ChildAdapter.onChildClickListener() {
            @Override
            public void onChildListener(int position, boolean isChecked) {
                if (!isChecked){
                    dataBean.setChecked(false);
                    cartClickListener.onCartListener(position,false);
                }else {
                    boolean isAllChildSelected = true;
                    for (CartBean.DataBean.ListBean listBean : dataBean.getList()) {
                        if (!listBean.isChecked()) {
                            isAllChildSelected = false;
                            break;
                        }
                    }
                    dataBean.setChecked(isAllChildSelected);
                    cartClickListener.onCartListener(position, true);

                }
                notifyDataSetChanged();

            }
        });

        holder.rvProduct.setAdapter(childAdapter);

        holder.cbShopperBox.setOnCheckedChangeListener(null);
        holder.cbShopperBox.setChecked(dataBean.isChecked());

        holder.cbShopperBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                dataBean.setChecked(b);
                for (CartBean.DataBean.ListBean listBean : dataBean.getList()) {
                    listBean.setChecked(b);
                }

                childAdapter.notifyDataSetChanged();

                if (cartClickListener != null){
                    cartClickListener.onCartListener(position,b);
                }


            }
        });

    }

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

    class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.cb_shopper_box)
        CheckBox cbShopperBox;
        @BindView(R.id.txt_shopper_name)
        TextView txtShopperName;
        @BindView(R.id.rv_product)
        RecyclerView rvProduct;
        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}

 

ChildAdapter

package com.example.lenovo.cart03.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.lenovo.cart03.R;
import com.example.lenovo.cart03.bean.CartBean;
import com.example.lenovo.cart03.inter.AddDecreaseView;
import com.example.lenovo.cart03.utils.ImgUtils;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 */

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

    //二级列表
    public interface onChildClickListener {
        void onChildListener(int position, boolean isChecked);
    }

    private onChildClickListener childClickListener;

    public void setChildClickListener(onChildClickListener childClickListener) {
        this.childClickListener = childClickListener;
    }


    // 加减器发生变化的监听
    public interface onAddCreateClickListener {
        void onChange(int position, int num);
    }

    private onAddCreateClickListener addCreateClickListener;

    public void setAddCreateClickListener(onAddCreateClickListener addCreateClickListener) {
        this.addCreateClickListener = addCreateClickListener;
    }


    private Context context;
    private List<CartBean.DataBean.ListBean> list;

    public ChildAdapter(Context context, List<CartBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = View.inflate(context, R.layout.item_cart_child, null);
        return new Holder(v);
    }

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

        final CartBean.DataBean.ListBean listBean = list.get(position);

        holder.txtProductName.setText(listBean.getTitle());
        holder.txtSinglePrice.setText(listBean.getPrice() + "");
        holder.imgProduct.setImageURI(Uri.parse(ImgUtils.getImg(listBean.getImages())));
        holder.advProduct.setNum(listBean.getNum());

        //复选框
        holder.cbProduct.setOnCheckedChangeListener(null);
        holder.cbProduct.setChecked(listBean.isChecked());
        holder.cbProduct.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                listBean.setChecked(b);
                if (childClickListener != null) {
                    childClickListener.onChildListener(position, b);
                }

            }
        });

        //加减器事件
        holder.advProduct.setClickListener(new AddDecreaseView.OnAddDecreaseClickListener() {
            @Override
            public void add(int num) {
                listBean.setNum(num);
                if (addCreateClickListener != null) {
                    addCreateClickListener.onChange(position, num);
                }
            }

            @Override
            public void decrease(int num) {
                listBean.setNum(num);
                if (addCreateClickListener != null) {
                    addCreateClickListener.onChange(position, num);
                }
            }
        });


    }

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

    class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.cb_product)
        CheckBox cbProduct;
        @BindView(R.id.img_product)
        SimpleDraweeView imgProduct;
        @BindView(R.id.adv_product)
        AddDecreaseView advProduct;
        @BindView(R.id.txt_product_name)
        TextView txtProductName;
        @BindView(R.id.txt_single_price)
        TextView txtSinglePrice;
        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}

 

 

MainLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">





<RelativeLayout
    android:id="@+id/rv_top"
    android:layout_width="match_parent"
    android:layout_height="48dp">

    <TextView
        android:text="购物车"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:text="编辑"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>


    <RelativeLayout
        android:id="@+id/rl_bottom"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <CheckBox
            android:id="@+id/cb_total_select"
            android:text="全选"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />



        <TextView
            android:id="@+id/txt_total_price"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/cb_total_select"
            android:textColor="#c23636"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <Button
            android:id="@+id/btn_calu"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="结算"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


    </RelativeLayout>


<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_shopper"
    android:layout_below="@id/rv_top"
    android:layout_above="@id/rl_bottom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>


</RelativeLayout>

 

AddLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="70dp"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/txt_decrease"
        android:background="#d26b67"
        android:text="-"
        android:padding="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <TextView
        android:id="@+id/txt_add"
        android:background="#d26b67"
        android:text=" + "
        android:layout_alignParentRight="true"
        android:padding="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/txt_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/txt_add"
        android:layout_toRightOf="@id/txt_decrease"
        android:gravity="center"
        android:maxLength="3"/>

</RelativeLayout>

 

CartLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_gravity="center_vertical"
        android:padding="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/cb_shopper_box"
            android:text="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/txt_shopper_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_product"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

 

ChildLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:paddingLeft="20dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp">


    <CheckBox
        android:id="@+id/cb_product"
        android:text="@null"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/img_product"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/cb_product"
        android:layout_width="45dp"
        android:layout_height="45dp" />



    <com.example.lenovo.cart03.inter.AddDecreaseView
        android:id="@+id/adv_product"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></com.example.lenovo.cart03.inter.AddDecreaseView>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/adv_product"
        android:layout_toRightOf="@id/img_product"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_product_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />

        <TextView
            android:id="@+id/txt_single_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>


</RelativeLayout>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Android项目中的网络请求,RxJava、RetrofitMVP是常用的框架组合。下面是一个简单的网络框架封装示例: 首先,在项目中引入RxJava和Retrofit的依赖。 ``` implementation 'io.reactivex.rxjava2:rxjava:2.2.19' implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0' ``` 然后,创建一个Retrofit的单例类,用于进行网络请求的初始化和配置。 ```java public class RetrofitClient { private static Retrofit retrofit; private static final String BASE_URL = "https://api.example.com/"; public static Retrofit getClient() { if (retrofit == null) { retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } return retrofit; } } ``` 接下来,创建一个ApiService接口,定义网络请求的方法。 ```java public interface ApiService { @GET("users") Observable<List<User>> getUsers(); } ``` 然后,创建一个DataManager类,用于管理网络请求。 ```java public class DataManager { private ApiService apiService; public DataManager() { apiService = RetrofitClient.getClient().create(ApiService.class); } public Observable<List<User>> getUsers() { return apiService.getUsers(); } } ``` 最后,在MVP的Presenter中调用DataManager类进行网络请求。 ```java public class UserPresenter { private UserView userView; private DataManager dataManager; public UserPresenter(UserView userView) { this.userView = userView; dataManager = new DataManager(); } public void getUsers() { dataManager.getUsers() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<List<User>>() { @Override public void onSubscribe(Disposable d) { // 在请求开始时的操作 } @Override public void onNext(List<User> users) { // 请求成功返回数据时的操作 userView.showUsers(users); } @Override public void onError(Throwable e) { // 请求失败时的操作 userView.showError(e.getMessage()); } @Override public void onComplete() { // 请求完成时的操作 } }); } } ``` 这样,就完成了一个简单的Android RxJava + Retrofit + MVP网络框架封装。你可以根据自己的需要,进行进一步的封装和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值