首先添加依赖哦
implementation files('libs\banner-2.1.0.aar')
接下来回到布局上面设置banner的属性 代码如下:
如果是网络请求数据的话可以这样写:
创建一个RetrofitClient的类,这里用retrofit的请求方法哦
public class RetrofiClient {
public static final String IP = "http://124.93.196.45:10091";
private static Retrofit retrofit = new Retrofit.Builder()
// 网络请求url地址
.baseUrl(IP)
// 网络请求适配器工厂的集合
.addConverterFactory(GsonConverterFactory.create())
.build();
private RetrofiClient() {
}
public static Retrofit getRetrofit() {
return retrofit;
}
public static APIService apiService() {
return retrofit.create(APIService.class);
}
}
在api的接口中写需要请求的地址:
//获取轮播图数据
@GET ("/prod-api/api/metro/rotation/list")
Call getBeanDate();
上面的BannerBean是将要请求的数据的实体类
public class BannerBean {
/**
* total : 2
* rows : [{"searchValue":null,"createBy":"admin","createTime":"2021-05-12 08:50:17","updateBy":"1","updateTime":"2021-05-12 09:07:33","remark":null,"params":{},"id":30,"appType":"metro","status":"1","sort":2,"advTitle":"地铁广告2","advImg":"/prod-api/profile/upload/image/2021/05/12/4a0fd4e7-2e01-420c-b89b-e39108d2f191.jpg","servModule":"新闻资讯","targetId":81,"type":"2"},{"searchValue":null,"createBy":"admin","createTime":"2021-05-12 09:12:51","updateBy":"1","updateTime":"2021-05-12 09:14:04","remark":null,"params":{},"id":33,"appType":"metro","status":"1","sort":3,"advTitle":"地铁广告3","advImg":"/prod-api/profile/upload/image/2021/05/12/aaa2ac1e-94da-4d0f-8077-8b1affa8c932.png","servModule":"新闻资讯","targetId":73,"type":"2"}]
* code : 200
* msg : 查询成功
*/
private int total;
private int code;
private String msg;
private List rows;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
public static class RowsBean {
/**
* searchValue : null
* createBy : admin
* createTime : 2021-05-12 08:50:17
* updateBy : 1
* updateTime : 2021-05-12 09:07:33
* remark : null
* params : {}
* id : 30
* appType : metro
* status : 1
* sort : 2
* advTitle : 地铁广告2
* advImg : /prod-api/profile/upload/image/2021/05/12/4a0fd4e7-2e01-420c-b89b-e39108d2f191.jpg
* servModule : 新闻资讯
* targetId : 81
* type : 2
*/
private Object searchValue;
private String createBy;
private String createTime;
private String updateBy;
private String updateTime;
private Object remark;
private ParamsBean params;
private int id;
private String appType;
private String status;
private int sort;
private String advTitle;
private String advImg;
private String servModule;
private int targetId;
private String type;
public Object getSearchValue() {
return searchValue;
}
public void setSearchValue(Object searchValue) {
this.searchValue = searchValue;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getUpdateBy() {
return updateBy;
}
public void setUpdateBy(String updateBy) {
this.updateBy = updateBy;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
public Object getRemark() {
return remark;
}
public void setRemark(Object remark) {
this.remark = remark;
}
public ParamsBean getParams() {
return params;
}
public void setParams(ParamsBean params) {
this.params = params;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAppType() {
return appType;
}
public void setAppType(String appType) {
this.appType = appType;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getSort() {
return sort;
}
public void setSort(int sort) {
this.sort = sort;
}
public String getAdvTitle() {
return advTitle;
}
public void setAdvTitle(String advTitle) {
this.advTitle = advTitle;
}
public String getAdvImg() {
return advImg;
}
public void setAdvImg(String advImg) {
this.advImg = advImg;
}
public String getServModule() {
return servModule;
}
public void setServModule(String servModule) {
this.servModule = servModule;
}
public int getTargetId() {
return targetId;
}
public void setTargetId(int targetId) {
this.targetId = targetId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public static class ParamsBean {
}
}
最后,在HomeFragment.java中对布局中的banner进行初始化
initView();
// 初始化
private void initView() {
banner = root.findViewById(R.id.banner);
}
接下来去获取轮播图
getBeanDate();
/ 获取轮播图
private void getBeanDate() {
RetrofiClient.apiService().getBeanDate().enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
BannerBean body = response.body();
if (body!=null && body.getCode()==200) {
mBannerList = body.getRows();
Log.i(TAG,"获取轮播图");
// 使用轮播图
userBanner();
}
}
@Override
public void onFailure(Call call, Throwable throwable) {
}
});
}
获取轮播图之后使用轮播图
/ 使用轮播图
private void userBanner(){
banner.setAdapter(new BannerImageAdapter(mBannerList) {
@Override
public void onBindView(BannerImageHolder bannerImageHolder, BannerBean.RowsBean rowsBean, int i, int i1) {
final String IPP =IP+mBannerList.get(i).getAdvImg();
Glide.with(getActivity().getApplicationContext())
.load(IP+mBannerList.get(i).getAdvImg())
.placeholder(R.mipmap.ic_launcher)
.apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
.into(bannerImageHolder.imageView);
Log.d(TAG, "onResponse: " + "使用轮播图222: IP = " + IPP);
}
})
// 添加生命周期观察者
.addBannerLifecycleObserver(this)
// 圆点指示器
.setIndicator(new CircleIndicator(getActivity().getApplicationContext()));
}
请求成功后就是这样的轮播图噢
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)