MVP泛型请求数据

来源:互联网 发布:网络客服招聘条件 编辑:程序博客网 时间:2024/06/02 03:33

实现我们先写

public class BasePresenter<V> {    // V 相当于V的接口    public V view;    /**     *  Presenter 持有View 的接口     * @param v     */    public void attach(V v){        this.view = v ;    }    /**     * Presenter 释放持有View的接口, 防止内存泄漏     */    public void detach(){        this.view = null;    }}

BaseMVPActivity

package com.xjq.fanxing_dome.base;import android.app.Activity;import android.os.Bundle;/** *  <V> 表示的View *  <T> 表示的Presenter */public abstract class BaseMvpActivity<V,T extends BasePresenter<V>> extends Activity {    public T t;    public abstract T initPresenter();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        t = initPresenter();    }    @Override    protected void onResume() {        super.onResume();        t.attach((V) this);    }    @Override    protected void onDestroy() {        super.onDestroy();        t.detach();    }}

Retrofit+Rxjava接口

public interface Inters {    @GET("/nba")    Observable<Bean> get(@QueryMap Map<String,String> map);}

MyApp类,主要写的就是Retrofit和Fresco

public class MyApp extends Application {    public static Inters inters;    @Override    public void onCreate() {        super.onCreate();        //Fresco        Fresco.initialize(this);        //Retrofit        Retrofit retrofit = new Retrofit.Builder()                .baseUrl("http://api.tianapi.com")                .addConverterFactory(GsonConverterFactory.create())                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())                .build();        inters = retrofit.create(Inters.class);    }}
下面Bean包我就不写了,直接开始写MVP

V层

public interface LoginView {    void success(Bean bean);    void failure();}
M层
public class LoginModelImpl{    //自己定义一个请求的方法    public void getData(final LoginModelCallBack callBack){        Map<String,String> map = new HashMap<>();        map.put("key","71e58b5b2f930eaf1f937407acde08fe");        map.put("num","10");        MyApp.inters.get(map)                .subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread())                .subscribe(new Consumer<Bean>() {                    @Override                    public void accept(Bean bean) throws Exception {                        callBack.success(bean);                    }                });    }    //定义一个接口    public interface LoginModelCallBack {     void success(Bean bean);    }}
P层

public class LoginPresenter extends BasePresenter<LoginView> {    LoginModelImpl loginModel;    public LoginPresenter() {        this.loginModel = new LoginModelImpl();    }    public void login(){        //  判断//        view.success();        loginModel.getData(new LoginModelImpl.LoginModelCallBack() {            @Override            public void success(Bean bean) {                view.success(bean);            }        });    }}
下面是适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {    private Context context;    private Bean bean;    public MyAdapter(Context context,Bean bean) {        this.context = context;        this.bean = bean;    }    @Override    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {        //拿到我们自己定义的布局        MyHolder holder = new MyHolder(LayoutInflater.from(context).inflate(R.layout.rv_litem, parent, false));        return holder;    }    @Override    public void onBindViewHolder(MyHolder holder, final 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();    }    class MyHolder extends RecyclerView.ViewHolder {        SimpleDraweeView iv;        TextView tv;        public MyHolder(View itemView) {            super(itemView);            iv = itemView.findViewById(R.id.iv);            tv = itemView.findViewById(R.id.tv);        }    }}
适配器布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal"    >    <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>
主方法

public class MainActivity extends BaseMvpActivity<LoginView,LoginPresenter> implements LoginView {    public RecyclerView rv;    @Override    public LoginPresenter initPresenter() {        return new LoginPresenter();    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //拿到p层的方法        t.login();        rv = findViewById(R.id.btn);    }    @Override    public void success(Bean bean) {       //线性格式       LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);        rv.setLayoutManager(manager);        //适配器              MyAdapter adapter = new MyAdapter(MainActivity.this,bean);        rv.setAdapter(adapter);        Toast.makeText(MainActivity.this,bean.toString(),Toast.LENGTH_SHORT).show();    }    @Override    public void failure() {    }}
主布局
<RelativeLayout    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.xjq.fanxing_dome.MainActivity">    <android.support.v7.widget.RecyclerView        android:id="@+id/btn"        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout>





原创粉丝点击