Recently, I like MVP, so I also learned other MVPS on the website and wrote a simple MVP. I am not sure if I am the standard MVP, but I feel quite separated. The MVP here is a combination of Retrofit + RxJava, all simple, because they are just beginning to learn, I hope you have more advice. In my opinion, MVP is to completely separate v layer from M layer, and control V layer and M layer through P layer (that is, call v layer or M layer method in P layer). I will post the code directly below, it is easy to understand. V layer:

public class MainActivity extends AppCompatActivity implements MainActivityInterface {

    @Bind(R.id.click_me_BN)
    Button clickMeBN;
    @Bind(R.id.result_tv)
    TextView resultTv;

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

    }

    @OnClick(R.id.click_me_BN)
    public void onClick() {
        MainPresent mainPresent = new MainPresent(this);
        mainPresent.loadData();
    }



    @Override
    public void showData(MovieEntity movieEntity) {
        resultTv.setText(movieEntity.getTitle());
    }

    @Override
    public void showError() {}}Copy the code
public interface MainActivityInterface {
    void showData(MovieEntity movieEntity);

}
Copy the code

P:

public class MainPresent implements MainPresentInterface,MainModel.OngetResult {

    private final MainModel mMainModel;
    public MainActivityInterface mActivityInterface;


    public MainPresent(MainActivityInterface activityInterface) {
        getInterfaceView(activityInterface);
        mMainModel = new MainModel(this);
    }


    @Override
    public void addViewData(MovieEntity movieEntity) {
        mActivityInterface.showData(movieEntity);
    }

    @Override
    public void getInterfaceView(MainActivityInterface mainActivityInterface) {
        this.mActivityInterface = mainActivityInterface;
    }

    @Override
    public void loadData() { mMainModel.loadData(); } @override public void getData(MovieEntity MovieEntity) {// getData successfully, cancel progress bar addViewData(MovieEntity); } @override public void error(Throwable e) {// Failed to obtain data, cancel the progress bar and perform other operations}}Copy the code
public interface MainPresentInterface {

    void addViewData(MovieEntity movieEntity);

    void getInterfaceView(MainActivityInterface mainActivityInterface);

    void loadData();

}
Copy the code

M layer:

public class MainModel implements MainModelInterface { private MainPresent mMainPresent; private OngetResult mOngetResult; public interface OngetResult { void getData(MovieEntity movieEntity); void error(Throwable e); } public MainModel(MainPresent mainPresent) { this.mMainPresent = mainPresent; } requests for network / * * * * * / @ https://api.douban.com/v2/movie/top250?start=0&count=10 Override public voidloadData() {
        String baseUrl = "https://api.douban.com/v2/movie/"; ApiManager. GetWriData (baseUrl, 0, 10). The subscribeOn (Schedulers. IO ()). ObserveOn (AndroidSchedulers. MainThread ()) .subscribe(new Observer<MovieEntity>() { @Override public voidonCompleted} @override public void onError(Throwable e) {// mongetresult.error (e); } @override public void onNext(MovieEntity MovieEntity) {// Take data mongetresult.getData (MovieEntity); }}); }}Copy the code
public interface MainModelInterface {
    void loadData();
}
Copy the code

APImanager:

public class ApiManager {


    private static String baseUrl;
    private static Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
   private static MovieService movieService = retrofit.create(MovieService.class);


    public static Observable<MovieEntity> getWriData(String url, int start, int count) {
        baseUrl = url;
        returnmovieService.getTopMovie(start, count); }}Copy the code

MovieService :

public interface MovieService {

    @GET("top250")
    Observable<MovieEntity> getTopMovie(@Query("start") int start, @Query("count") int count);
}
Copy the code