写权限
<uses-permission android:name="android.permission.INTERNET" />
依赖
//butterknife快速查找资源的依赖
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//okgo
implementation 'com.lzy.net:okgo:3.0.4'
//glide
implementation 'com.github.bumptech.glide:glide:4.8.0'
//gson解析
implementation 'org.kie.modules:com-google-code-gson:6.5.0.Final'
首先写出MVP的结构
契约类
public interface IContract {
//V层
public interface IView{
public void showData(ArrayList<YdBean.DataBean> ydBean);
}
//P层
public interface IPersenter<IView>{
public void attacth(IContract.IView iView);
public void destach(IContract.IView iView);
public void request();
}
//M层
public interface IMoudel{
public void response(OnCallBack callback);
public interface OnCallBack{
public void oncallBack(ArrayList<YdBean.DataBean> ydBean);
}
}
}
p层
public class IPersenterIpl implements IContract.IPersenter<IContract.IView>{
private SoftReference<IMoudelIpl> reference;
private IMoudelIpl iMoudelIpl;
IContract.IView iView;
@Override
public void attacth(IContract.IView iView) {
this.iView=iView;
iMoudelIpl = new IMoudelIpl();
reference = new SoftReference<>(iMoudelIpl);
}
@Override
public void destach(IContract.IView iView) {
reference.clear();
}
@Override
public void request() {
iMoudelIpl.response(new IContract.IMoudel.OnCallBack() {
@Override
public void oncallBack(ArrayList<YdBean.DataBean> ydBean) {
iView.showData(ydBean);
}
});
}
}
M层
public class IMoudelIpl implements IContract.IMoudel {
@Override
public void response(final OnCallBack callback) {
OkGo.<String>get(Contant.INTERD_URL).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String toString = response.body().toString();
Gson gson = new Gson();
YdBean ydBean = gson.fromJson(toString, YdBean.class);
ArrayList<YdBean.DataBean> data = (ArrayList<YdBean.DataBean>) ydBean.getData();
callback.oncallBack(data);
}
});
}
}
activity页面
public class HomeActivity extends AppCompatActivity implements IContract.IView {
@BindView(R.id.viewpager)
ViewPager viewpager;
@BindView(R.id.but)
Button but;
private IPersenterIpl iPersenterIpl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//默认为隐藏
but.setVisibility(View.GONE);
//P层
iPersenterIpl = new IPersenterIpl();
iPersenterIpl.attacth(this);
iPersenterIpl.request();
}
@Override
public void showData(final ArrayList<YdBean.DataBean> ydBean) {
MyPagerAdapter myPagerAdapter = new MyPagerAdapter(ydBean, HomeActivity.this);
viewpager.setAdapter(myPagerAdapter);
//监听
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
if (i == ydBean.size() - 1) {
but.setVisibility(View.VISIBLE);
} else {
but.setVisibility(View.GONE);
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
// }
// });
}
//避免内存泄漏
@Override
protected void onDestroy() {
super.onDestroy();
iPersenterIpl.destach(this);
}
//最后页面的跳转
@OnClick(R.id.but)
public void onViewClicked() {
Intent intent = new Intent(HomeActivity.this,ShyActivity.class);
startActivity(intent);
}
}
适配器
public class MyPagerAdapter extends PagerAdapter {
//list集合
ArrayList<YdBean.DataBean> list;
Context context;
//构造方法
public MyPagerAdapter(ArrayList<YdBean.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view==o;
}
//设置图片
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
YdBean.DataBean dataBean = list.get(position);
ImageView imageView = new ImageView(context);
//glide 图片加载框架
Glide.with(context).load(list.get(position).getIcon()).into(imageView);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}