第一步:配置权限和导包:
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.VIBRATE" />
compile 'com.jcodecraeer:xrecyclerview:1.3.2' compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.retrofit2:converter-gson:2.0.1' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1' compile 'io.reactivex:rxandroid:1.1.0' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' compile 'com.facebook.fresco:fresco:0.9.0+' compile 'com.android.support:mediarouter-v7:25.0.0' compile 'com.android.support:appcompat-v7:25.0.0' compile 'com.android.support:recyclerview-v7:25.0.0'
第二步:创建三个包,创建两个接口,分别是model,view俩个接口:
public interface Iview { void getdata(List<Mysupercalss.DataBean> data); }
public interface Imodel { void login(String mm); }
第三步:model实现接口:
package model; import java.util.List; import bean.API; import bean.Iservers; import bean.Mysupercalss; import retrofit2.Retrofit; import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; import rx.Observable; import rx.Observer; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; /** * Created by Administrator on 2017/11/5 0005. */ public class Mymodel implements Imodel { private OnFinishLisenter lisenter; //定义接口 public interface OnFinishLisenter{ void onFinish(List<Mysupercalss.DataBean> userBean); } public void setOnFinishLisenter(OnFinishLisenter lisenter){ this.lisenter = lisenter; } @Override public void login(String mm) { Retrofit retrofit = new Retrofit.Builder().baseUrl(API.MYAPI).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); Iservers apiser = retrofit.create(Iservers.class); Observable<Mysupercalss> gethom = apiser.getdata(); gethom.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<Mysupercalss>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Mysupercalss mysup) { lisenter.onFinish(mysup.getData()); } }); } }
第四步:Persenter层:
package presenter; import java.util.List; import bean.Mysupercalss; import model.Mymodel; import view.Iview; /** * Created by Administrator on 2017/11/5 0005. */ public class Mypre implements Mymodel.OnFinishLisenter { private Iview iview; private Mymodel mymodel; public Mypre(Iview iview) { this.iview = iview; this.mymodel = new Mymodel(); } public void setadapter(String path){ mymodel.setOnFinishLisenter(this); mymodel.login(path); } @Override public void onFinish(List<Mysupercalss.DataBean> userBean) { iview.getdata(userBean); } }
需要注意的是这里使用了接口回调,再调用时需要注意顺序的问题
第五步:MainActivity:
package com.example.mvp_retrofit; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import com.jcodecraeer.xrecyclerview.XRecyclerView; import java.util.List; import bean.API; import bean.MyAdapter; import bean.Mysupercalss; import presenter.Mypre; import view.Iview; public class MainActivity extends AppCompatActivity implements Iview{ XRecyclerView xrecyclerview; List<Mysupercalss.DataBean> list; MyAdapter adapter; Mypre mypre; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xrecyclerview = (XRecyclerView)findViewById(R.id.xrecyclerview); LinearLayoutManager layoutManager = new LinearLayoutManager(this); /* layoutManager.setOrientation(LinearLayoutManager.VERTICAL); */ xrecyclerview.setLayoutManager(layoutManager); mypre=new Mypre(this); mypre.setadapter(API.MYAPI); } @Override public void getdata(final List<Mysupercalss.DataBean> data) { adapter = new MyAdapter(MainActivity.this, data); xrecyclerview.setAdapter(adapter); xrecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { list=data; adapter.notifyDataSetChanged(); xrecyclerview.refreshComplete(); } @Override public void onLoadMore() { list.addAll(data); adapter.notifyDataSetChanged(); xrecyclerview.loadMoreComplete(); } }); } }
还有url和适配器我就不贴了,可以根据自己的json串来做响应的改变.