Retrofit2 + RxJava + Okhttp + RecyclerView用MVP架构展示数据
写代码思路流程:
//1.权限和依赖
//2.布局文件
//3.生成实体bean类
//4.封装工具类 Retrofit + RxJava + OkHttp
//5.搭建MVP架构
//6.创建适配器
//7.在Activity或者Fragment中进行加载布局管理器和绑定适配器
效果图:
a.添加联网权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
b.依赖:(带libs的依赖需要自己导入jar包,也可以自己在网上搜直接添加)
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'
compile 'com.squareup.retrofit2:retrofit:2.0.2' //Retrofit2库文件
compile 'com.squareup.retrofit2:converter-gson:2.0.2' //支持Gson解析
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' //支持RxJava
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
compile 'com.github.bumptech.glide:glide:3.7.0'
布局文件:
activity_main.xml
-
-
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:app="http://schemas.android.com/apk/res-auto"
-
xmlns:tools="http://schemas.android.com/tools"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
tools:context="com.ccm.mvpretrofitrxjava.activity.MainActivity">
-
-
<com.jcodecraeer.xrecyclerview.XRecyclerView
-
android:id="@+id/xrv"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView>
-
-
</android.support.constraint.ConstraintLayout>
recy_item
-
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="50dp"
-
android:orientation="horizontal">
-
-
<ImageView
-
android:id="@+id/iv_item"
-
android:layout_width="50dp"
-
android:layout_height="match_parent" />
-
-
<TextView
-
android:id="@+id/tv_item"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:text="我是文本"
-
android:textSize="20sp"/>
-
</LinearLayout>
生成实体bean类:
-
public class Bean {
-
-
private String msg;
-
private RetEntity ret;
-
private String code;
-
-
public String getMsg() {
-
return msg;
-
}
-
-
public void setMsg(String msg) {
-
this.msg = msg;
-
}
-
-
public RetEntity getRet() {
-
return ret;
-
}
-
-
public void setRet(RetEntity ret) {
-
this.ret = ret;
-
}
-
-
public String getCode() {
-
return code;
-
}
-
-
public void setCode(String code) {
-
this.code = code;
-
}
-
-
public static class RetEntity {
-
-
private AdvEntity adv;
-
private int pnum;
-
private int totalRecords;
-
private int records;
-
private int totalPnum;
-
private List<?> bannerList;
-
private List<ListEntity> list;
-
-
public AdvEntity getAdv() {
-
return adv;
-
}
-
-
public void setAdv(AdvEntity adv) {
-
this.adv = adv;
-
}
-
-
public int getPnum() {
-
return pnum;
-
}
-
-
public void setPnum(int pnum) {
-
this.pnum = pnum;
-
}
-
-
public int getTotalRecords() {
-
return totalRecords;
-
}
-
-
public void setTotalRecords(int totalRecords) {
-
this.totalRecords = totalRecords;
-
}
-
-
public int getRecords() {
-
return records;
-
}
-
-
public void setRecords(int records) {
-
this.records = records;
-
}
-
-
public int getTotalPnum() {
-
return totalPnum;
-
}
-
-
public void setTotalPnum(int totalPnum) {
-
this.totalPnum = totalPnum;
-
}
-
-
public List<?> getBannerList() {
-
return bannerList;
-
}
-
-
public void setBannerList(List<?> bannerList) {
-
this.bannerList = bannerList;
-
}
-
-
public List<ListEntity> getList() {
-
return list;
-
}
-
-
public void setList(List<ListEntity> list) {
-
this.list = list;
-
}
-
-
public static class AdvEntity {
-
-
private String imgURL;
-
private String dataId;
-
private String htmlURL;
-
private String shareURL;
-
private String title;
-
-
public String getImgURL() {
-
return imgURL;
-
}
-
-
public void setImgURL(String imgURL) {
-
this.imgURL = imgURL;
-
}
-
-
public String getDataId() {
-
return dataId;
-
}
-
-
public void setDataId(String dataId) {
-
this.dataId = dataId;
-
}
-
-
public String getHtmlURL() {
-
return htmlURL;
-
}
-
-
public void setHtmlURL(String htmlURL) {
-
this.htmlURL = htmlURL;
-
}
-
-
public String getShareURL() {
-
return shareURL;
-
}
-
-
public void setShareURL(String shareURL) {
-
this.shareURL = shareURL;
-
}
-
-
public String getTitle() {
-
return title;
-
}
-
-
public void setTitle(String title) {
-
this.title = title;
-
}
-
}
-
-
public static class ListEntity {
-
-
-
private int airTime;
-
private String duration;
-
private String loadtype;
-
private int score;
-
private String angleIcon;
-
private String dataId;
-
private String description;
-
private String loadURL;
-
private String shareURL;
-
private String pic;
-
private String title;
-
private String roomId;
-
-
public int getAirTime() {
-
return airTime;
-
}
-
-
public void setAirTime(int airTime) {
-
this.airTime = airTime;
-
}
-
-
public String getDuration() {
-
return duration;
-
}
-
-
public void setDuration(String duration) {
-
this.duration = duration;
-
}
-
-
public String getLoadtype() {
-
return loadtype;
-
}
-
-
public void setLoadtype(String loadtype) {
-
this.loadtype = loadtype;
-
}
-
-
public int getScore() {
-
return score;
-
}
-
-
public void setScore(int score) {
-
this.score = score;
-
}
-
-
public String getAngleIcon() {
-
return angleIcon;
-
}
-
-
public void setAngleIcon(String angleIcon) {
-
this.angleIcon = angleIcon;
-
}
-
-
public String getDataId() {
-
return dataId;
-
}
-
-
public void setDataId(String dataId) {
-
this.dataId = dataId;
-
}
-
-
public String getDescription() {
-
return description;
-
}
-
-
public void setDescription(String description) {
-
this.description = description;
-
}
-
-
public String getLoadURL() {
-
return loadURL;
-
}
-
-
public void setLoadURL(String loadURL) {
-
this.loadURL = loadURL;
-
}
-
-
public String getShareURL() {
-
return shareURL;
-
}
-
-
public void setShareURL(String shareURL) {
-
this.shareURL = shareURL;
-
}
-
-
public String getPic() {
-
return pic;
-
}
-
-
public void setPic(String pic) {
-
this.pic = pic;
-
}
-
-
public String getTitle() {
-
return title;
-
}
-
-
public void setTitle(String title) {
-
this.title = title;
-
}
-
-
public String getRoomId() {
-
return roomId;
-
}
-
-
public void setRoomId(String roomId) {
-
this.roomId = roomId;
-
}
-
}
-
}
-
}
封装工具类:
RequestApi接口
-
public interface RequestApi {
-
public static final String BASE_URL = "http://api.svipmovie.com/front/";
-
-
//get和RxJava
-
-
Observable<Bean> getData();
-
-
-
-
Call<ResponseBody> downloadFile(@Path("fileName") String fileName, @Header("Range") String range);
-
-
-
-
Call<ResponseBody> getFileLenght(@Path("fileName") String fileName);
-
}
RetrofitUtils类:
-
public class RetrofitUtils {
-
//支持RxJava
-
public static RequestApi getNetDatas(){
-
OkHttpClient client = new OkHttpClient.Builder()
-
.connectTimeout(5, TimeUnit.SECONDS)
-
.readTimeout(5, TimeUnit.SECONDS)
-
// .addNetworkInterceptor(new MyInterceptro())
-
.build();
-
-
Retrofit retrofit = new Retrofit.Builder()
-
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
-
.addConverterFactory(GsonConverterFactory.create())
-
.client(client)
-
.baseUrl(RequestApi.BASE_URL)
-
.build();
-
-
RequestApi api = retrofit.create(RequestApi.class);
-
-
return api;
-
}
-
-
-
public static RequestApi download(){
-
OkHttpClient client = new OkHttpClient.Builder()
-
.connectTimeout(5, TimeUnit.SECONDS)
-
.readTimeout(5, TimeUnit.SECONDS)
-
//.addNetworkInterceptor(new MyInterceptro())
-
.build();
-
-
Retrofit retrofit = new Retrofit.Builder()
-
.client(client)
-
.baseUrl("http://10.0.2.2:8080/aaa/")
-
.build();
-
-
RequestApi api = retrofit.create(RequestApi.class);
-
-
return api;
-
}
-
}
view层:
-
public interface IRecyView {
-
void showRecy(Bean bean);
-
}
model层:
IRecyModel接口
-
public interface IRecyModel {
-
void recy(Observer<Bean> observer);
-
}
RecyModel是IRecyView的实现类
-
public class RecyModel implements IRecyModel {
-
-
public void recy(Observer<Bean> observer) {
-
//OkHttpUtils.getInstance().doGet("http://api.svipmovie.com/front/columns/getVideoList.do?catalogId=402834815584e463015584e539330016", callback);
-
RetrofitUtils.getNetDatas().getData()
-
.subscribeOn(Schedulers.io()) //定义被观察者在子线程执行
-
.observeOn(AndroidSchedulers.mainThread()) //定义观察者在主线程执行
-
.subscribe(observer);
-
}
-
}
presenter层:
-
public class RecyPresenter {
-
private IRecyView view;
-
private IRecyModel model;
-
-
-
public RecyPresenter(IRecyView view) {
-
this.view = view;
-
model = new RecyModel();
-
}
-
-
public void showRecy(){
-
model.recy(new Observer<Bean>() {
-
-
public void onCompleted() {
-
Log.i("我用retrofit+RxJava好使啦", "onCompleted: ");
-
}
-
-
-
public void onError(Throwable e) {
-
Log.i("我用retrofit+RxJava好使啦", "onError: ");
-
}
-
-
-
public void onNext(Bean bean) {
-
Log.i("我用retrofit+RxJava好使啦", "onNext: ");
-
view.showRecy(bean);
-
}
-
});
-
}
-
-
//解绑
-
public void onDestory(){
-
view = null;
-
}
-
}
适配器:
-
public class MyRecyAdapter extends RecyclerView.Adapter<MyRecyAdapter.MyViewHolder>{
-
private Context context;
-
private Bean bean;
-
-
public MyRecyAdapter(Context context, Bean bean) {
-
this.context = context;
-
this.bean = bean;
-
}
-
-
-
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
-
View view = LayoutInflater.from(context).inflate(R.layout.recy_item, parent, false);
-
MyViewHolder holder = new MyViewHolder(view);
-
return holder;
-
}
-
-
-
public void onBindViewHolder(MyViewHolder holder, int position) {
-
Glide.with(context).load(bean.getRet().getList().get(position).getPic()).into(holder.iv_item);
-
holder.tv_item.setText(bean.getRet().getList().get(position).getDescription());
-
}
-
-
-
public int getItemCount() {
-
return bean.getRet().getList().size();
-
}
-
-
static class MyViewHolder extends RecyclerView.ViewHolder{
-
-
ImageView iv_item;
-
TextView tv_item;
-
-
public MyViewHolder(View itemView) {
-
super(itemView);
-
iv_item = itemView.findViewById(R.id.iv_item);
-
tv_item = itemView.findViewById(R.id.tv_item);
-
}
-
}
-
}
MainActivity:
-
//1.权限和依赖
-
//2.布局文件
-
//3.生成实体bean类
-
//4.封装工具类 Retrofit + RxJava + OkHttp
-
//5.搭建MVP架构
-
//6.创建适配器
-
//7.在Activity或者Fragment中进行加载布局管理器和绑定适配器
-
public class MainActivity extends AppCompatActivity implements IRecyView{
-
-
private XRecyclerView xrv;
-
private RecyPresenter presenter;
-
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
initView();
-
presenter = new RecyPresenter(this);
-
presenter.showRecy();
-
}
-
-
-
private void initView() {
-
xrv = (XRecyclerView) findViewById(R.id.xrv);
-
LinearLayoutManager manager = new LinearLayoutManager(this);
-
manager.setOrientation(LinearLayoutManager.VERTICAL);
-
xrv.setLayoutManager(manager);
-
//下面是加载更多的方法
-
}
-
-
public void showRecy(Bean bean) {
-
MyRecyAdapter myRecyAdapter = new MyRecyAdapter(this, bean);
-
xrv.setAdapter(myRecyAdapter);
-
}
-
}
--------------------- 本文来自 巫山老妖_ 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/weixin_39779085/article/details/78607591?utm_source=copy

浙公网安备 33010602011771号