Model层1 package com.example.mvpretrofit.Model; import java.util.Map; /** * Created by 佘鑫海 on 2017/12/6. */ public interface IModel { void getData(Map<String,String> map); } Model层2 package com.example.mvpretrofit.Model; import com.example.mvpretrofit.Bean.MessageBean; import com.example.mvpretrofit.Bean.NewBean; import com.example.mvpretrofit.Presenter.NewsPresenter; import com.example.mvpretrofit.Utils.RetrofitUtils; import java.util.List; import java.util.Map; import io.reactivex.Flowable; /** * Created by 佘鑫海 on 2017/12/6. */ public class NewModel implements IModel { private NewsPresenter presenter; public NewModel(NewsPresenter presenter){ this.presenter =presenter; } @Override public void getData(Map<String, String> map) { Flowable<MessageBean<List<NewBean>>> flowable = RetrofitUtils.getInstance().getApiService() .getData(map); presenter.getNews(flowable); } }
View层 package com.example.mvpretrofit.View; /** * Created by 佘鑫海 on 2017/12/6. */ public interface IView { void onSuccess(Object o); void onFailed(Exception e); }
Presenter层1 package com.example.mvpretrofit.Presenter; import java.util.Map; /** * Created by 佘鑫海 on 2017/12/6. */ public interface BasePresenter { void getData(Map<String,String>map); } Presenter层2 package com.example.mvpretrofit.Presenter; import com.example.mvpretrofit.Bean.MessageBean; import com.example.mvpretrofit.Bean.NewBean; import com.example.mvpretrofit.Model.IModel; import com.example.mvpretrofit.Model.NewModel; import com.example.mvpretrofit.View.IView; import java.util.List; import java.util.Map; import io.reactivex.Flowable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import io.reactivex.subscribers.DisposableSubscriber; /** * Created by 佘鑫海 on 2017/12/6. */ public class NewsPresenter implements BasePresenter { private IView iView; private DisposableSubscriber subscriber; public void attachView(IView iView){ this.iView = iView; } public void detachView(){ if (iView != null){ iView = null; } if (subscriber != null){ if (!subscriber.isDisposed()){ subscriber.dispose(); } } } @Override public void getData(Map<String, String> map) { IModel model = new NewModel(this); model.getData(map); } public void getNews(Flowable<MessageBean<List<NewBean>>> flowable){ subscriber = flowable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableSubscriber<MessageBean<List<NewBean>>>() { @Override public void onNext(MessageBean<List<NewBean>> listMessageBean) { if (listMessageBean != null){ List<NewBean> list = listMessageBean.getData(); if (list != null){ iView.onSuccess(list); } } } @Override public void onError(Throwable t) { iView.onFailed(new Exception(t)); } @Override public void onComplete() { } }); } }
MessageBean package com.example.mvpretrofit.Presenter; import com.example.mvpretrofit.Bean.MessageBean; import com.example.mvpretrofit.Bean.NewBean; import com.example.mvpretrofit.Model.IModel; import com.example.mvpretrofit.Model.NewModel; import com.example.mvpretrofit.View.IView; import java.util.List; import java.util.Map; import io.reactivex.Flowable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import io.reactivex.subscribers.DisposableSubscriber; /** * Created by 佘鑫海 on 2017/12/6. */ public class NewsPresenter implements BasePresenter { private IView iView; private DisposableSubscriber subscriber; public void attachView(IView iView){ this.iView = iView; } public void detachView(){ if (iView != null){ iView = null; } if (subscriber != null){ if (!subscriber.isDisposed()){ subscriber.dispose(); } } } @Override public void getData(Map<String, String> map) { IModel model = new NewModel(this); model.getData(map); } public void getNews(Flowable<MessageBean<List<NewBean>>> flowable){ subscriber = flowable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableSubscriber<MessageBean<List<NewBean>>>() { @Override public void onNext(MessageBean<List<NewBean>> listMessageBean) { if (listMessageBean != null){ List<NewBean> list = listMessageBean.getData(); if (list != null){ iView.onSuccess(list); } } } @Override public void onError(Throwable t) { iView.onFailed(new Exception(t)); } @Override public void onComplete() { } }); } }
NewBean package com.example.mvpretrofit.Bean; import com.google.gson.annotations.SerializedName; /** * Created by 佘鑫海 on 2017/12/6. */ public class NewBean { /** * ID : 30736417 * TITLE : 九三学社中央十三届二十次常委会举行 * SUBTITLE : 九三学社第十三届中央常务委员会第二十次会议10月29日在京举行。会议学习贯彻 * IMAGEURL : http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/30/15_13_54_275_30248_A62I7285_fb.jpg * FROMNAME : 九三学社中央宣传部 * SHOWTIME : 2017-10-30 * RN : 1 */ @SerializedName("ID") private String id; @SerializedName("TITLE") private String title; @SerializedName("SUBTITLE") private String subtitle; @SerializedName("IMAGEURL") private String imageurl; @SerializedName("FROMNAME") private String fromname; @SerializedName("SHOWTIME") private String showtime; private int rn; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSubtitle() { return subtitle; } public void setSubtitle(String subtitle) { this.subtitle = subtitle; } public String getImageurl() { return imageurl; } public void setImageurl(String imageurl) { this.imageurl = imageurl; } public String getFromname() { return fromname; } public void setFromname(String fromname) { this.fromname = fromname; } public String getShowtime() { return showtime; } public void setShowtime(String showtime) { this.showtime = showtime; } public int getrn() { return rn; } public void setrn(int rn) { this.rn = rn; } }
ApiService package com.example.mvpretrofit.Api; import com.example.mvpretrofit.Bean.MessageBean; import com.example.mvpretrofit.Bean.NewBean; import java.util.List; import java.util.Map; import io.reactivex.Flowable; import retrofit2.http.GET; import retrofit2.http.QueryMap; /** * Created by 佘鑫海 on 2017/12/6. */ public interface ApiService { @GET("93app/data.do") Flowable<MessageBean<List<NewBean>>> getData(@QueryMap Map<String,String> map); }
Adapter package com.example.mvpretrofit.Adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.example.mvpretrofit.Bean.NewBean; import com.example.mvpretrofit.R; import java.util.List; /** * Created by 佘鑫海 on 2017/12/7. */ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private Context context; private List<NewBean> list; public MyAdapter(Context context, List<NewBean> list) { this.context = context; this.list = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.activity_item, null); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { Glide.with(context).load(list.get(position).getImageurl()).into(holder.imgLogo); holder.txtName.setText(list.get(position).getTitle()); } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder{ private ImageView imgLogo; private TextView txtName; public ViewHolder(View itemView) { super(itemView); imgLogo = itemView.findViewById(R.id.MyImage); txtName = itemView.findViewById(R.id.mytext); } } }
package com.example.mvpretrofit.Utils; import com.example.mvpretrofit.Api.ApiService; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; /** * Created by 佘鑫海 on 2017/12/6. */ public class RetrofitUtils { private static volatile RetrofitUtils instance; private ApiService apiService; private RetrofitUtils(){ Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .baseUrl("http://www.93.gov.cn/") .build(); apiService = retrofit.create(ApiService.class); } public static RetrofitUtils getInstance(){ if (instance == null){ synchronized (RetrofitUtils.class){ if (null == instance){ instance = new RetrofitUtils(); } } } return instance; } public ApiService getApiService(){ return apiService; } }
package com.example.mvpretrofit; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.Toast; import com.example.mvpretrofit.Adapter.MyAdapter; import com.example.mvpretrofit.Bean.NewBean; import com.example.mvpretrofit.Presenter.NewsPresenter; import com.example.mvpretrofit.View.IView; import com.liaoinstan.springview.widget.SpringView; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity implements IView{ @BindView(R.id.myrecycle) RecyclerView recyclerView; private List<NewBean> list; private MyAdapter adapter; private NewsPresenter presenter; private LinearLayoutManager manager; private SpringView springview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); list = new ArrayList<>(); manager = new LinearLayoutManager(this); recyclerView.setLayoutManager(manager); adapter = new MyAdapter(this, list); recyclerView.setAdapter(adapter); springview = findViewById(R.id.mySpringView); springview.setHeader(new MyHeader()); springview.setListener(new SpringView.OnFreshListener() { @Override public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { springview.onFinishFreshAndLoad(); } }, 1000); } @Override public void onLoadmore() { new Handler().postDelayed(new Runnable() { @Override public void run() { springview.onFinishFreshAndLoad(); } }, 1000); } }); getData(); } private void getData() { Map<String,String> map = new HashMap<>(); map.put("channelId","0"); map.put("startNum","0"); presenter = new NewsPresenter(); presenter.attachView(this); presenter.getData(map); } @Override public void onSuccess(Object o) { if (o instanceof List) { List<NewBean> data = (List<NewBean>) o; if (data != null) { list.clear(); list.addAll(data); //进行刷新 adapter.notifyDataSetChanged(); } } } @Override public void onFailed(Exception e) { Toast.makeText(MainActivity.this,"你失败了"+e.getMessage(),Toast.LENGTH_SHORT).show(); } //防止内存泄露 @Override protected void onDestroy() { super.onDestroy(); if (presenter != null){ presenter.detachView(); } } }