MVP泛型+xRecyclerView+Retrofit+OkHttp+RxJava多条目,横向

MVP泛型+xRecyclerView+Retrofit+OkHttp+RxJava多条目,横向_第1张图片


这是所用到的依赖

compile 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile "io.reactivex.rxjava2:rxjava:2.1.7"
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.facebook.fresco:fresco:1.5.0'
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

先创建一个类封装一下View层

public class BaseView<V> {

    public  V View ;
    public  void  Hold(V views){
        this.View= views;
    }


    public  void  datach(){
        View = null;
    }
}
创建一个初始化数据的类

public class App 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里的请求方式
    }
}
创建一个抽象类它就相当于我们主方法继承的Activity

public  abstract class BaseMvpActivity<V,P extends BaseView<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 get(@QueryMap Map Map);
}
创建View层

public interface SupperView {
    void  Success(NewsBean bean);
    void  Fail(Exception e);
}
创建Modle层的接口

public interface SupperModle {
    void  Success(NewsBean bean);
    void  Fail(Exception e);
}
创建Modle层的类

public class SupperModleS {
    //自己定义一个请求的方法
    public void getData(final SupperModle supperModle) {
        //创建HashMap
        Map map = new HashMap<>();
        //传值
        map.put("key", "71e58b5b2f930eaf1f937407acde08fe");
        map.put("num", "10");
        App.objget.get(map)
                .subscribeOn(Schedulers.io())//子线程
                .observeOn(AndroidSchedulers.mainThread())//主线程
                .subscribe(new Consumer() {
                    @Override
                    public void accept(NewsBean bean) throws Exception {
                        supperModle.Success(bean);
                    }
                });
    }
}
创建Presnenter层

public class SupperPresenter  {
    SupperModleS supperModleS;  //model层的实体类
    //WeakReference弱引用
    WeakReference supperViewWeakReference;
    //构造参数
    
    public SupperPresenter(SupperView supperView) {
        datach((MainActivity) supperView);
       this.supperModleS = new SupperModleS();

    }
    
  public  void  get(){
      supperModleS.getData(new SupperModle() {
          @Override
          public void Success(NewsBean bean) {
                  if (supperViewWeakReference!=null){
                      supperViewWeakReference.get().Success(bean);
                  }
          }

          @Override
          public void Fail(Exception e) {
              if (supperViewWeakReference!=null){
                  supperViewWeakReference.get().Fail(e);
              }
          }
      });

  }
    // 绑定 内存泄漏
    public  void  datach(MainActivity view){
        supperViewWeakReference = new WeakReference(view);
    }
    //解绑
    public  void  data(){
        supperViewWeakReference.clear();
    }

}
下面是最主要的适配器

public class SupperApdata extends  RecyclerView.Adapter {
    Context context;
 NewsBean bean;
   //List list = new ArrayList<>();


    public SupperApdata(Context context, NewsBean bean) {
        this.context = context;
        this.bean = bean;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case 0:
                view = View.inflate(context,R.layout.layout,null);
                holder = new ViewHolder1(view);
                break;
            case 1:
                view = View.inflate(context,R.layout.layout2,null);
                holder = new ViewHolder2(view);
                break;
        }
        return holder;

    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        switch (getItemViewType(position)){
            case 0:
                final ViewHolder1 holder1 = (ViewHolder1) holder;
                //加载文字
                holder1.tv.setText(bean.getNewslist().get(position).getTitle());
                Uri uri = Uri.parse(bean.getNewslist().get(position).getPicUrl());
                DraweeController controller = Fresco.newDraweeControllerBuilder()
                        .setUri(uri)
                        .setAutoPlayAnimations(true)
                        .build();
                holder1.iv.setController(controller);
                break;
            case 1:
                final ViewHolder2 holder2 = (ViewHolder2) holder;
                holder2.tv2.setText(bean.getNewslist().get(position).getTitle());
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(position%2 == 0){
            return 0;
        }else {
            return 1;
        }

    }

    @Override
    public int getItemCount() {
        return bean.getNewslist().size();
    }


    public class ViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.iv)
        SimpleDraweeView iv;
        @BindView(R.id.tv)
        TextView tv;
        public ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public class ViewHolder2 extends RecyclerView.ViewHolder {
        @BindView(R.id.tv2)
        TextView tv2;
        public ViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
下面是适配器的两个布局

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>
第二个

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">
  <TextView
      android:id="@+id/tv2"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />
LinearLayout>
下面进入主方法

public class MainActivity extends AppCompatActivity implements SupperView{

    @BindView(R.id.rcv)
    RecyclerView rcv;
    @BindView(R.id.rv)
    RecyclerView rv;
    Listlist = new ArrayList<>();
    SupperPresenter presenter;
    SupperApdata apdata;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        presenter = new SupperPresenter(this);
        presenter.get();
    }

    @Override
    public void Success(NewsBean bean) {
        //线性格式  布局管理器
        LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        //横向布局
        LinearLayoutManager manager1 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
        rv.setLayoutManager(manager);
        rcv.setLayoutManager(manager1);
        //适配器
        apdata = new SupperApdata(MainActivity.this,bean);
         rv.setAdapter(apdata);  //绑定
        rcv.setAdapter(apdata);//横向布局
    }

    @Override
    public void Fail(Exception e) {

    }
    //内存泄漏
    @Override
    public void onDestroy() {
        super.onDestroy();
        //解绑
        presenter.data();
    }
}
主方法布局

xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.resumex.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rcv"
        android:layout_width="match_parent"
        android:layout_height="100dp">

    android.support.v7.widget.RecyclerView>
    <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>



你可能感兴趣的:(移动开发)