- 导入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<DataBean> data;
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<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
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.<String>get("http://vueshop.glbuys.com/api/home/index/slide?token=1ec949a15fb709370f").execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String json = response.body();
Gson gson = new Gson();
BannerBean bannerBean = gson.fromJson(json, BannerBean.class);
List<BannerBean.DataBean> list = bannerBean.getData();
}
});
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.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BannerActivity">
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="200dp"></com.youth.banner.Banner>
</LinearLayout>
8.在清单文件加入网络权限
<uses-permission android:name="android.permission.INTERNET" />
9.完成!!!!!!!!!!!!!!!!!