MVP泛型+xRecyclerView+Retrofit+OkHttp+RxJava多條目,橫向
阿新 • • 發佈:2019-02-11
這是所用到的依賴
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"建立一個抽象類它就相當於我們主方法繼承的Activity)//請求頭 .addConverterFactory(GsonConverterFactory.create())//資料的解析 .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //資料解析 .build(); objget = retrofit.create(ObjGet.class);//拿到objget裡的請求方式 } }
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<NewsBean> get(@QueryMap Map<String ,String> 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<String, String> map = new HashMap<>(); //傳值 map.put("key", "71e58b5b2f930eaf1f937407acde08fe"); map.put("num", "10"); App.objget.get(map) .subscribeOn(Schedulers.io())//子執行緒 .observeOn(AndroidSchedulers.mainThread())//主執行緒 .subscribe(new Consumer<NewsBean>() { @Override public void accept(NewsBean bean) throws Exception { supperModle.Success(bean); } }); } }建立Presnenter層
public class SupperPresenter { SupperModleS supperModleS; //model層的實體類 //WeakReference弱引用 WeakReference<SupperView> 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<NewsBean.NewslistBean> 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; List<NewsBean.NewslistBean>list = 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>