当前位置:主页 > 移动开发 > Android代码 >

Android 中Banner的使用详解

时间:2021-02-12 08:51:27 | 栏目:Android代码 | 点击:

首先倒入一个依赖: compile 'com.youth.banner:banner:1.4.9'

添加的权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 布局文件:

<com.youth.banner.Banner
  android:id="@+id/banner_id"
  android:layout_width="match_parent"
  android:layout_height="170dp"></com.youth.banner.Banner>

在MainActivity中写入的代码

String url1 = "http://api.kkmh.com/v1/topic_new/discovery_list?gender=0&sa_event=eyJwcm9qZWN0Ijoia3VhaWthbl9hcHAiLCJ0aW1lIjoxNDg3ODM5MDM5MzE1LCJwcm9wZXJ0aWVzIjp7IkhvbWVwYWdlVGFiTmFtZSI6IueDremXqCIsIlZDb21tdW5pdHlUYWJOYW1lIjoi54Ot6ZeoIiwiJG9zX3ZlcnNpb24iOiI0LjIuMiIsIkdlbmRlclR5cGUiOiLlpbPniYgiLCJGcm9tRmluZENhdGVnb3J5VGFiTmFtZSI6IuWFqOmDqCIsIklzQXV0b0xvYWQiOmZhbHNlLCIkbGliX3ZlcnNpb24iOiIxLjYuMzQiLCIkbmV0d29ya190eXBlIjoiV0lGSSIsIiR3aWZpIjp0cnVlLCIkbWFudWZhY3R1cmVyIjoic2Ftc3VuZyIsIkZyb21GaW5kVGFiTmFtZSI6IuaOqOiNkCIsIiRzY3JlZW5faGVpZ2h0Ijo1NzYsIkNhdGVnb3J5Ijoi5peg5rOV6I635Y-WIiwiSG9tZXBhZ2VVcGRhdGVEYXRlIjowLCJQcm9wZXJ0eUV2ZW50IjoiUmVhZEZpbmRQYWdlIiwiRmluZFRhYk5hbWUiOiLmjqjojZAiLCJhYnRlc3RfZ3JvdXAiOjQ2LCIkc2NyZWVuX3dpZHRoIjoxMDI0LCJGaW5kQ2F0ZWdvcnlUYWJOYW1lIjoi5YWo6YOoIiwiJG9zIjoiQW5kcm9pZCIsIlRyaWdnZXJQYWdlIjoiSG9tZVBhZ2UiLCIkY2FycmllciI6IkNNQ0MiLCIkbW9kZWwiOiJHVC1QNTIxMCIsIiRhcHBfdmVyc2lvbiI6IjMuOC4xIn0sInR5cGUiOiJ0cmFjayIsImRpc3RpbmN0X2lkIjoiQTo5MDUxMDQyNzYzNzU1MTA5Iiwib3JpZ2luYWxfaWQiOiJBOjkwNTEwNDI3NjM3NTUxMDkiLCJldmVudCI6IlJlYWRGaW5kUGFnZSJ9";
RequestParams params1 = new RequestParams();
params1.setUri(url1);
x.http().get(params1, new Callback.CommonCallback<String>() {
  @Override
  public void onSuccess(final String result) {
    if (result != null) {
      Gson gson = new Gson();
      LunboImage lunboImage = gson.fromJson(result, LunboImage.class);
      images = new ArrayList<>();
      List<LunboImage.DataBean.InfosBean> infos = lunboImage.getData().getInfos();
      for (LunboImage.DataBean.InfosBean bean : infos) {
        List<LunboImage.DataBean.InfosBean.BannersBean> banners = bean.getBanners();
        for (LunboImage.DataBean.InfosBean.BannersBean ban : banners) {
          String pic = ban.getPic();
          images.add(pic);
        }
      }
      //设置banner样式
     // banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
      //设置图片的加载器
      banner.setImageLoader(new GliderImageloader());
      banner.setDelayTime(2000);
      //设置图片集合
      banner.setImages(images);
      //banner设置方法全部调用完毕时最后调用
      banner.isAutoPlay(true);
      banner.start();
    }
  }
  @Override
  public void onError(Throwable ex, boolean isOnCallback) {
  }
  @Override
  public void onCancelled(CancelledException cex) {
  }
  @Override
  public void onFinished() {
  }
});

您可能感兴趣的文章:

相关文章