============== Main里 ================
```
public class MainActivity extends AppCompatActivity implements MyView {
? ? private RecyclerView mMainRec;
? ? private Presenterimpl presenterimpl;
? ? private static final String TAG = "MainActivity";
? ? private MainRecAdaper mainRecAdaper;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? initView();
? ? ? ? initRec();
//初始化P層
? ? ? ? initPresenter();
? ? }
? ? private void initPresenter() {
? ? ? ? presenterimpl = new Presenterimpl();
//調(diào)用P層里的方法 Attach 把 View 傳過去 ,不能再用構(gòu)造傳參了,會有內(nèi)存泄漏風險
? ? ? ? presenterimpl.Attach(this);
? ? ? ? presenterimpl.getgirllist();
? ? }
? ? private void initRec() {
? ? ? ? LinearLayoutManager lm = new LinearLayoutManager(getApplicationContext());
? ? ? ? lm.setOrientation(LinearLayoutManager.HORIZONTAL);
? ? ? ? mMainRec.setLayoutManager(lm);
? ? ? ? DividerItemDecoration dd = new DividerItemDecoration(getApplicationContext(), LinearLayout.HORIZONTAL);
? ? ? ? mMainRec.addItemDecoration(dd);
? ? ? ? mainRecAdaper = new MainRecAdaper(getApplicationContext());
? ? ? ? mMainRec.setAdapter(mainRecAdaper);
? ? }
? ? private void initView() {
? ? ? ? mMainRec = (RecyclerView) findViewById(R.id.main_rec);
? ? }
? ? @Override
? ? protected void onDestroy() {
? ? ? ? super.onDestroy();
//調(diào)用P層里的方法 Detach,防止用戶在沒加載的時候關(guān)閉界面導致內(nèi)存泄漏
? ? ? ? presenterimpl.Detach();
? ? }
? ? @Override
? ? public void succeedview(List<Girlbean.ResultsBean> girllist) {
? ? ? ? mainRecAdaper.initDate(girllist);
? ? ? ? ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 1000);
? ? ? ? //設置時長
? ? ? ? valueAnimator.setDuration(5000);
? ? ? ? //設置無限循環(huán)RepeatCount 循環(huán)次數(shù) INFINITE 無限的
? ? ? ? valueAnimator.setRepeatCount(valueAnimator.INFINITE);
? ? ? ? //設置改變監(jiān)聽
? ? ? ? valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onAnimationUpdate(ValueAnimator animation) {
? ? ? ? ? ? ? ? //設置動畫的移動方式 Y軸不動,X軸每秒走5個單位
? ? ? ? ? ? ? ? mMainRec.scrollBy(5, 0);
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //別忘記開始
? ? ? ? valueAnimator.start();
? ? }
? ? @Override
? ? public void errorview(String error) {
? ? ? ? Log.d(TAG, "errorview: " + error);
? ? }
}
```
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ==============? ? P層? ? ? ================
public class Presenterimpl implements MyPresenter {
? ? MyView view;
? ? @Override
? ? public void getgirllist() {
? ? ? ? Modelimpl modelimpl = new Modelimpl();
? ? ? ? modelimpl.Modelgetgirllist(new MyCallBack() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void succeed(List<Girlbean.ResultsBean> girllist) {
? ? ? ? ? ? ? ? if (view != null) {
? ? ? ? ? ? ? ? ? ? view.succeedview(girllist);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void error(String error) {
? ? ? ? ? ? ? ? if (view != null) {
? ? ? ? ? ? ? ? ? ? view.errorview(error);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? //通過方法傳遞View 不在用構(gòu)造傳參,防止內(nèi)存泄漏
? ? public void Attach(MyView view) {
? ? ? ? this.view = view;
? ? }
? ? //在main里ONDestroy里調(diào)用,防止用戶在沒加載的時候關(guān)閉界面導致內(nèi)存泄漏
? ? public void Detach() {
? ? ? ? this.view = null;
? ? }
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ==============? ? M層? ? ? ================
public class Modelimpl implements MyModel {
? ? private static final String TAG = "Modelimpl";
? ? @Override
? ? public void Modelgetgirllist(final MyCallBack callBack) {
//設置緩存 MyApplication下面有對應類 記得清單文件添加name
? ? ? ? File filesDir = MyApplication.context.getFilesDir();// 應用內(nèi)部目錄,不需要請求SD卡權(quán)限
? ? ? ? OkHttpClient client = new OkHttpClient.Builder()
? ? ? ? ? ? ? ? .cache(new Cache(filesDir, 1024 * 1024))
? ? ? ? ? ? ? ? .build();
? ? ? ? Observable<Girlbean> getgirllist = new Retrofit.Builder()
? ? ? ? ? ? ? ? .baseUrl(AppService.girlurl)
? ? ? ? ? ? ? ? .addConverterFactory(GsonConverterFactory.create())
? ? ? ? ? ? ? ? .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
? ? ? ? ? ? ? ? //因為Retrofit底層是OKHttp解析的,所以在這里可以設置client 方法,這樣就可以添加你需要的功能了比如緩存
? ? ? ? ? ? ? ? .client(client)
? ? ? ? ? ? ? ? .build()
? ? ? ? ? ? ? ? .create(AppService.class)
? ? ? ? ? ? ? ? .getgirllist();
? ? ? ? getgirllist.subscribeOn(Schedulers.newThread())
? ? ? ? ? ? ? ? .observeOn(AndroidSchedulers.mainThread())
? ? ? ? ? ? ? ? .subscribe(new Observer<Girlbean>() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onSubscribe(Disposable d) {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onNext(Girlbean girlbean) {
? ? ? ? ? ? ? ? ? ? ? ? List<Girlbean.ResultsBean> girllist = girlbean.getResults();
? ? ? ? ? ? ? ? ? ? ? ? callBack.succeed(girllist);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onError(Throwable e) {
? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "onError: "+e.getMessage());
? ? ? ? ? ? ? ? ? ? ? ? callBack.error(e.getMessage());
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onComplete() {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? }
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ==============? ? Adaper? ? ? ================
public class MainRecAdaper extends RecyclerView.Adapter<MainRecAdaper.ViewHolder> {
? ? Context context;
? ? List<Girlbean.ResultsBean> girllist = new ArrayList<>();
? ? public MainRecAdaper(Context context) {
? ? ? ? this.context = context;
? ? }
? ? @NonNull
? ? @Override
? ? public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
? ? ? ? View view = View.inflate(context, R.layout.mainrecitemlayout, null);
? ? ? ? ViewHolder holder = new ViewHolder(view);
? ? ? ? return holder;
? ? }
? ? @Override
? ? public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
? ? ? ? /*if (this.girllist != null && this.girllist.size() != 0) {
? ? ? ? ? ? i = i % girllist.size();
? ? ? ? }*/
//注意一定要判定 不能為空,不然報集合size = 0錯
? ? ? ? if (this.girllist.size() <= 0 && this.girllist.isEmpty()) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? i = i % girllist.size();
? ? ? ? viewHolder.tv.setText(girllist.get(i).getDesc());
? ? ? ? RoundedCorners roundedCorners = new RoundedCorners(20);
? ? ? ? RequestOptions requestOptions = RequestOptions.bitmapTransform(roundedCorners);
? ? ? ? Glide.with(context).load(girllist.get(i).getUrl()).apply(requestOptions).into(viewHolder.iv);
? ? }
? ? @Override
? ? public int getItemCount() {
? ? ? ? //返回integer 最大值,使其無限滑動
? ? ? ? //return girllist.size();
? ? ? ? return Integer.MAX_VALUE;
? ? }
? ? public class ViewHolder extends RecyclerView.ViewHolder {
? ? ? ? ImageView iv;
? ? ? ? TextView tv;
? ? ? ? public ViewHolder(@NonNull View itemView) {
? ? ? ? ? ? super(itemView);
? ? ? ? ? ? iv = itemView.findViewById(R.id.item_iv);
? ? ? ? ? ? tv = itemView.findViewById(R.id.item_tv);
? ? ? ? }
? ? }
? ? public void initDate(List<Girlbean.ResultsBean> girllist) {
? ? ? ? if (this.girllist != null) {
? ? ? ? ? ? this.girllist.clear();
? ? ? ? ? ? this.girllist.addAll(girllist);
? ? ? ? ? ? notifyDataSetChanged();
? ? ? ? }
? ? }
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ==============? ? MyApplication? ? ? ================
public class MyApplication extends Application {
? ? public static Context context;
? ? @Override
? ? public void onCreate() {
? ? ? ? super.onCreate();
? ? ? ? context = getApplicationContext();
? ? }
}