============== 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();
//调用P层里的方法 Attach 把 View 传过去 ,不能再用构造传参了,会有内存泄漏风险
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();
//调用P层里的方法 Detach,防止用户在没加载的时候关闭界面导致内存泄漏
presenterimpl.Detach();
}
@Override
public void succeedview(List
mainRecAdaper.initDate(girllist);
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 1000);
//设置时长
valueAnimator.setDuration(5000);
//设置无限循环RepeatCount 循环次数 INFINITE 无限的
valueAnimator.setRepeatCount(valueAnimator.INFINITE);
//设置改变监听
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
if (view != null) {
view.succeedview(girllist);
}
}
@Override
public void error(String error) {
if (view != null) {
view.errorview(error);
}
}
});
}
//通过方法传递View 不在用构造传参,防止内存泄漏
public void Attach(MyView view) {
this.view = view;
}
//在main里ONDestroy里调用,防止用户在没加载的时候关闭界面导致内存泄漏
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();// 应用内部目录,不需要请求SD卡权限
OkHttpClient client = new OkHttpClient.Builder()
.cache(new Cache(filesDir, 1024 * 1024))
.build();
Observable
.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
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Girlbean girlbean) {
List
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
Context context;
List
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
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();
}
}