导入banner依赖以及OkGo依赖和解析gson依赖以及Glide依赖
implementation 'com.lzy.net:okgo:3.0.4'//okgo依赖
implementation 'com.github.bumptech.glide:glide:4.9.0'//glide依赖
implementation 'com.youth.banner:banner:1.4.10' //banner轮播图依赖
implementation 'com.google.code.gson:gson:2.8.0'//gson依赖
---------------------------------------------------------------------------------------------------------------------------------
2.新建实体类BannerBean
package com.example.three_week_lianxi;
import java.util.List;
public class BannerBean {
/**
* status : 1
* code : 200
* data : [{"title":"图片1","image":"//vueshop.glbuys.com/uploadfiles/1484285302.jpg","webs":"#"},{"title":"图片2","image":"//vueshop.glbuys.com/uploadfiles/1484285334.jpg","webs":"#"},{"title":"图片3","image":"//vueshop.glbuys.com/uploadfiles/1524206455.jpg","webs":"#"}]
*/
private int status;
private int code;
/**
* title : 图片1
* image : //vueshop.glbuys.com/uploadfiles/1484285302.jpg
* webs : #
*/
private List
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public List
return data;
}
public void setData(List
this.data = data;
}
public static class DataBean {
private String title;
private String image;
private String webs;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getWebs() {
return webs;
}
public void setWebs(String webs) {
this.webs = webs;
}
}
}
3.使用OkGo下载json串并且解析
OkGo.
@Override
public void onSuccess(Response
String json = response.body();
Gson gson = new Gson();
BannerBean bannerBean = gson.fromJson(json, BannerBean.class);
List
}
});
4.接下来就是banner轮播图的使用以及它的一些方法
//首先设置数据源
banner.setImages(list);
//是否为自动轮播
banner.isAutoPlay(true);
//轮播图数量
banner.setBannerStyle(BannerConfig.CENTER);
//动画效果
banner.setBannerAnimation(Transformer.ZoomOut);
//间隔时间
banner.setDelayTime(1000);
//banner样式
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
//标题
banner.setBannerTitles(title);
//监听banner
banner.setOnBannerListener(new OnBannerListener() {
@Override
public void OnBannerClick(int position) {
Toast.makeText(BannerActivity.this, "点击了"+position, Toast.LENGTH_SHORT).show();
}
});
//在里面给Banner设置图片
banner.setImageLoader(new ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
BannerBean.DataBean bean = (BannerBean.DataBean) path;
String image = bean.getImage();
Glide.with(context).load("http:"+image).into(imageView);
}
}).start();
6.这是效果截图
7.布局文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BannerActivity">
android:layout_width="match_parent"
android:layout_height="200dp">
8.在清单文件加入网络权限
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/AM12idi/article/details/115521473