//需要添加依赖和添加网络权限
compile'com.youth.banner:banner:1.4.9'
<uses-permissionandroid:name="android.permission.INTERNET" />
<uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE" />
//控件
<com.youth.banner.Banner
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/banner"
>com.youth.banner.Banner>
//操作
private Banner banner;
private List list = new ArrayList<>();
//网络上请求的数据
private String path = "http://api.kkmh.com/v1/topic_new/discovery_list?gender=0&sa_event=eyJwcm9qZWN0Ijoia3VhaWthbl9hcHAiLCJ0aW1lIjoxNDg3ODM5MDM5MzE1LCJwcm9wZXJ0aWVzIjp7IkhvbWVwYWdlVGFiTmFtZSI6IueDremXqCIsIlZDb21tdW5pdHlUYWJOYW1lIjoi54Ot6ZeoIiwiJG9zX3ZlcnNpb24iOiI0LjIuMiIsIkdlbmRlclR5cGUiOiLlpbPniYgiLCJGcm9tRmluZENhdGVnb3J5VGFiTmFtZSI6IuWFqOmDqCIsIklzQXV0b0xvYWQiOmZhbHNlLCIkbGliX3ZlcnNpb24iOiIxLjYuMzQiLCIkbmV0d29ya190eXBlIjoiV0lGSSIsIiR3aWZpIjp0cnVlLCIkbWFudWZhY3R1cmVyIjoic2Ftc3VuZyIsIkZyb21GaW5kVGFiTmFtZSI6IuaOqOiNkCIsIiRzY3JlZW5faGVpZ2h0Ijo1NzYsIkNhdGVnb3J5Ijoi5peg5rOV6I635Y-WIiwiSG9tZXBhZ2VVcGRhdGVEYXRlIjowLCJQcm9wZXJ0eUV2ZW50IjoiUmVhZEZpbmRQYWdlIiwiRmluZFRhYk5hbWUiOiLmjqjojZAiLCJhYnRlc3RfZ3JvdXAiOjQ2LCIkc2NyZWVuX3dpZHRoIjoxMDI0LCJGaW5kQ2F0ZWdvcnlUYWJOYW1lIjoi5YWo6YOoIiwiJG9zIjoiQW5kcm9pZCIsIlRyaWdnZXJQYWdlIjoiSG9tZVBhZ2UiLCIkY2FycmllciI6IkNNQ0MiLCIkbW9kZWwiOiJHVC1QNTIxMCIsIiRhcHBfdmVyc2lvbiI6IjMuOC4xIn0sInR5cGUiOiJ0cmFjayIsImRpc3RpbmN0X2lkIjoiQTo5MDUxMDQyNzYzNzU1MTA5Iiwib3JpZ2luYWxfaWQiOiJBOjkwNTEwNDI3NjM3NTUxMDkiLCJldmVudCI6IlJlYWRGaW5kUGFnZSJ9";
//这个是固定数据的操作
// private String[] images = {
// "http://l2.51fanli.net//tuan//images//1//5806eac956808.jpg",
// "http://l2.51fanli.net//tuan//images//b//580991bb30560.jpg",
// "http://l0.51fanli.net//tuan//images//b//58115f2593dc3.jpg",
// "http://l2.51fanli.net//tuan//images//0//57923840b054d.jpg",
// "http://l2.51fanli.net//tuan//images//e//58101e11ab164.jpg"};
private String[] str = new String[10];
private List stringList = new ArrayList<>();
private int position=0;
//通过handler来更新ui
private android.os.Handler han = new android.os.Handler(){
@Override
public void handleMessage(Message msg) {
//找到banner控件id
banner = (Banner) findViewById(R.id.banner);
//调用方法
banner.setImageLoader(new bannerGlide());
//添加图片的集合
banner.setImages(stringList);
//设置延迟时间
banner.setDelayTime(3000);
//是否自动轮播
banner.isAutoPlay(true);
//开启
banner.start();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//这个是遍历固定数据的集合
// for (String s:images) {
// stringList.add(s);
// }
initData();
}
//解析网络请求的数据
private void initData() {
new Thread(){
@Override
public void run() {
String s = Utils.parmrs(path);
Gson gson = new Gson();
Data data = gson.fromJson(s, Data.class);
List infos = data.getData().getInfos();
list.addAll(infos);
for(Data.DataBean.InfosBean aaa:list){
List banners = aaa.getBanners();
for (Data.DataBean.InfosBean.BannersBean banner: banners) {
stringList.add(banner.getPic());
Log.e("-----", "run: "+banner.getPic());
}
}
han.sendEmptyMessage(0);
// List banners = infos.get(0).getBanners();
//
}
}.start();
}
//需要继承imageLoader来操作图片的加载
class bannerGlide extends ImageLoader{
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load(path)
.placeholder(R.mipmap.ic_launcher)
.into(imageView);
}
<
uses-permission
android:name
=
"android.permission.INTERNET"
/>
<
uses-permission
android:name
=
"android.permission.READ_EXTERNAL_STORAGE"
/>