主Activity
public class SupperActivity extends BaseMvpActivity<SupperView, SupperPresenter> implements SupperView { @BindView(R.id.rv) RecyclerView rv; @Override public SupperPresenter initPresenter() { return new SupperPresenter(this); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_supper); ButterKnife.bind(this); p.login();//调用 p层定义的方法 } @Override public void Success(Bean bean) { //线性格式 布局管理器 LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); rv.setLayoutManager(manager); //适配器 SupperApdata adapter = new SupperApdata(SupperActivity.this,bean); rv.setAdapter(adapter); //绑定 } //失败 @Override public void Failure(Exception e) { } //内存泄漏 @Override public void onDestroy() { super.onDestroy(); //解绑 p.data(); } }主布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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.example.my_12_6.SupperActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>BaseMvpActicity 相当于我们主布局继承的Activity
public abstract class BaseMvpActivity<V,P extends BasePresenter<V>> extends Activity{ public P p; //p层的泛型 public abstract P initPresenter(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); p = initPresenter(); //实例化 } //这里是相当于p层持有了View层的接口 @Override protected void onResume() { super.onResume(); p.Hold((V) this); } //释放持有View的接口, 防止内存泄漏 @Override protected void onDestroy() { super.onDestroy(); p.Datach(); } }网络请求
public interface objGet { @GET("/nba") Observable<Bean> get(@QueryMap Map<String ,String> Map); }MyApp
//数据的初始化 public class MyApp extends Application { public static objGet objget; @Override public void onCreate() { super.onCreate(); Fresco.initialize(this);//图片框架的初始化 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://api.tianapi.com")//请求头 .addConverterFactory(GsonConverterFactory.create())//数据的解析 .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //数据解析 .build(); objget = retrofit.create(objGet.class);//拿到objget里的请求方式 } }View层
public interface SupperView { void Success(Bean bean); void Failure(Exception e); }Model层接口
public interface SupperModleINf { void Success(Bean bean); void Failure(Exception e); }Modle层类
public class SupperModle extends SupperActivity { //自己定义一个请求的方法 public void getData(final SupperModleINf supperModleINf){ //创建HashMap Map<String, String> map = new HashMap<>(); //传值 map.put("key","71e58b5b2f930eaf1f937407acde08fe"); map.put("num","10"); MyApp.objget.get(map) .subscribeOn(Schedulers.io())//子线程 .observeOn(AndroidSchedulers.mainThread())//主线程 .subscribe(new Consumer<Bean>() { @Override public void accept(Bean bean) throws Exception { supperModleINf.Success(bean); } }); } }Presenter层
public class SupperPresenter extends BasePresenter<SupperView>{ SupperModle supperModle; //model层的实体类 //WeakReference弱引用 WeakReference<SupperView> supperViewWeakReference; //构造参数 public SupperPresenter(SupperActivity supperModle) { this.supperModle = new SupperModle(); } //自己定义的方法 public void login(){ // 判断 // view.success(); //new LoginModelImpl.LoginModelCallBack() 用modle层的实体类莱调用实体类的接口 得到成功的方法 supperModle.getData(new SupperModleINf() { //成功的方法 @Override public void Success(Bean bean) { view.Success(bean); } //失败的方法 @Override public void Failure(Exception e) { view.Failure(e); } }); } // 绑定 内存泄漏 public void datach(SupperView view){ supperViewWeakReference = new WeakReference(view); } //解绑 public void data(){ supperViewWeakReference.clear(); } }适配器
public class SupperApdata extends RecyclerView.Adapter<SupperApdata.ViewHolder> { private Context context; private Bean bean; public SupperApdata(Context context, Bean bean) { this.context = context; this.bean = bean; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.rv_litem, null); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { //加载文字 holder.tv.setText(bean.getNewslist().get(position).getTitle()); holder.iv.setImageURI(bean.getNewslist().get(position).getPicUrl()); } @Override public int getItemCount() { return bean.getNewslist().size(); } static public class ViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.iv) SimpleDraweeView iv; @BindView(R.id.tv) TextView tv; public ViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } }封装View层
//这里的V就相当于View层 public class BasePresenter<V>{ // v相当于View层的接口 public V view; //这里相当于把View初始化 //这里相当于持有View层 public void Hold(V views){ this.view = views; } //防止内存泄漏 public void Datach(){ view =null; } }适配器布局
<?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"> <com.facebook.drawee.view.SimpleDraweeView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/iv"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:id="@+id/tv"/> </LinearLayout>
用到的依赖
compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.1.7' compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' compile 'com.android.support:recyclerview-v7:26.1.0' compile 'com.facebook.fresco:fresco:1.5.0' compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'