MVP+xRecyclerView+Retrofit+OkHttp+RxJava
阿新 • • 發佈:2019-02-19
依賴
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.0.+' implementation 'com.android.support.constraint:constraint-layout:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1'androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' compile 'com.android.support:recyclerview-v7:26.0.+' compile 'com.facebook.fresco:fresco:0.12.0' // Retrofit庫 compile 'com.squareup.retrofit2:retrofit:2.0.1' // Okhttp庫 compile 'com.squareup.okhttp3:okhttp:3.1.2' // rxjava+rxandroid+retrofit2+okhttpcompile 'io.reactivex:rxjava:1.0.14' compile 'io.reactivex:rxandroid:1.0.1' compile 'com.squareup.retrofit2:converter-gson:2.0.1' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' }
Api包
介面BeanApi類
package com.example.com.rikao0419.Api; importcom.example.com.rikao0419.Bean; import java.util.HashMap; import java.util.Map; import retrofit2.http.GET; import retrofit2.http.QueryMap; import rx.Observable; /** * Created by 丶未央 on 2018/4/19. */ public interface BeanApi { @GET("nba/") Observable<Bean> bean(@QueryMap HashMap<String, String> map); //http://api.tianapi.com/nba/?key="+APIKEY+"&num=10 } MyAPL類
model層package com.example.com.rikao0419.Api; /** * Created by 丶未央 on 2018/4/19. */ public class MyAPI { public static final String APIKEY="71e58b5b2f930eaf1f937407acde08fe"; public static final String utils="http://api.tianapi.com/"; }
IMOdle介面
package com.example.com.rikao0419.molde; /** * Created by 丶未央 on 2018/4/19. */ public interface IMOdle { //處理資料方法 void GetDate(int a) ; }
BeanModel 類
package com.example.com.rikao0419.molde; import com.example.com.rikao0419.Api.BeanApi; import com.example.com.rikao0419.Api.MyAPI; import com.example.com.rikao0419.Bean; import java.util.ArrayList; import java.util.HashMap; import retrofit2.Retrofit; import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; import rx.Observable; import rx.Observer; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; /** * Created by 丶未央 on 2018/4/19. */ public class BeanModel implements IMOdle { ArrayList<Bean.NewslistBean> lists; OnFinish onFinish; public BeanModel(OnFinish onFinish){ this.onFinish=onFinish; } //建立介面 public interface OnFinish{ void onFinishLinisher(ArrayList<Bean.NewslistBean> lists); } @Override public void GetDate(int a) { lists = new ArrayList<>(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(MyAPI.utils) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); // 通過動態代理獲得網路介面資料 BeanApi apiService = retrofit.create(BeanApi.class); HashMap<String, String> map = new HashMap<>(); map.put("key",MyAPI.APIKEY); map.put("num",a+""); Observable<Bean> data = apiService.bean(map); //請求資料的執行緒 data.subscribeOn(Schedulers.io()) // 設定請求結束後的執行緒 .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<Bean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Bean bean) { // Log.d("main2", "onNext: " + bean.newslist + "----"); lists = (ArrayList<Bean.NewslistBean>) bean.getNewslist(); onFinish.onFinishLinisher(lists); } }); } }
presenter層
UserPresenter類
package com.example.com.rikao0419.presenter; import com.example.com.rikao0419.Bean; import com.example.com.rikao0419.molde.BeanModel; import com.example.com.rikao0419.view.IView; import java.util.ArrayList; /** * Created by 丶未央 on 2018/4/19. */ public class UserPresenter implements BeanModel.OnFinish { private final IView iview; private final BeanModel userModel; public UserPresenter(IView iview) { this.iview = iview; userModel = new BeanModel(this); } // 處理model與view的邏輯 public void getData(){ userModel.GetDate(20); } @Override public void onFinishLinisher(ArrayList<Bean.NewslistBean> lists) { iview.ShowData(lists); } }
View層IView
package com.example.com.rikao0419.view; import com.example.com.rikao0419.Bean; import java.util.ArrayList; /** * Created by 丶未央 on 2018/4/19. */ public interface IView { void ShowData(ArrayList<Bean.NewslistBean> lists); }
MainActivity類
package com.example.com.rikao0419; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.example.com.rikao0419.presenter.UserPresenter; import com.example.com.rikao0419.view.IView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements IView{ private RecyclerView rlv; UserPresenter presenter; MyXrecyclerviewAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { rlv= (RecyclerView) findViewById(R.id.rlv); // 初始化資料 initData(); } private void initData() { presenter = new UserPresenter(this); presenter.getData(); } @Override public void ShowData(ArrayList<Bean.NewslistBean> lists) { rlv.setLayoutManager(new LinearLayoutManager(this)); rlv.setAdapter(adapter = new MyXrecyclerviewAdapter(lists,this)); } }
Adapter介面卡MyXrecyclerviewAdapter
package com.example.com.rikao0419; import android.annotation.SuppressLint; import android.content.Context; import android.net.Uri; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.facebook.drawee.view.SimpleDraweeView; import java.util.ArrayList; /** * Created by 丶未央 on 2018/4/19. */ class MyXrecyclerviewAdapter extends RecyclerView.Adapter{ // 資料來源 ArrayList<Bean.NewslistBean> lists; // 上下文 Context context; public MyXrecyclerviewAdapter(ArrayList<Bean.NewslistBean> lists, Context context) { this.lists = lists; this.context = context; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(parent.getContext(), R.layout.recy_item, null); MyViewHolder holder = new MyViewHolder(view); return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { MyViewHolder holderone= (MyViewHolder) holder; holderone.tv.setText(lists.get(position).getTitle()); Uri uri = Uri.parse(lists.get(position).getPicUrl()); holderone.img.setImageURI(uri); } @Override public int getItemCount() { return lists.size(); } class MyViewHolder extends RecyclerView.ViewHolder { TextView tv; SimpleDraweeView img; @SuppressLint("WrongViewCast") public MyViewHolder(View view) { super(view); tv = view.findViewById(R.id.recy_item_tv); img = view.findViewById(R.id.recy_item_img); } } }