【Android】开发Banner(轮播图)

开发Banner(轮播图)

打开GitHub找banner库

搜索banner

点击第一个
https://github.com/youth5201314/banner

按照使用API开始开发

添加依赖

arduino 复制代码
implementation 'io.github.youth5201314:banner:2.2.3'

添加权限(针对网络图片)

js 复制代码
<!-- if you want to load images from the internet -->
<uses-permission android:name="android.permission.INTERNET" /> 
xml 复制代码
<com.youth.banner.Banner  
    android:id="@+id/banner_id"  
    android:layout_width="match_parent"  
    android:layout_height="300dp"  
/>

创建Banner对象

创建BannerDataInfo.java文件

java 复制代码
package com.hxtx.august.MyObject;  
  
public class BannerDataInfo {  
    private int img;  
    private String title;  

    public BannerDataInfo(int img, String title) {  
    this.img = img;  
    this.title = title;  
    }  

    public int getImg() {  
    return img;  
    }  

    public void setImg(int img) {  
    this.img = img;  
    }  

    public String getTitle() {  
    return title;  
    }  

    public void setTitle(String title) {  
    this.title = title;  
    }  
}

显示Banner

java 复制代码
/*轮播图*/  
Banner bannerView = findViewById(R.id.banner_id);  
List<BannerDataInfo> BannerDataInfo = new ArrayList<>();  
BannerDataInfo.add(new BannerDataInfo(R.mipmap.banner1, "标题1"));  
BannerDataInfo.add(new BannerDataInfo(R.mipmap.banner2, "标题2"));  
BannerDataInfo.add(new BannerDataInfo(R.mipmap.banner3, "标题3"));  
bannerView.setAdapter(new BannerImageAdapter<BannerDataInfo>(BannerDataInfo) {  
    @Override  
    public void onBindView(BannerImageHolder holder, BannerDataInfo data, int position, int size) {  
        // 设置数据(本地图片)  
        holder.imageView.setImageResource(data.getImg());  
}  
}).addBannerLifecycleObserver(this)  
.setIndicator(new CircleIndicator(requireContext()));
相关推荐
雨白7 小时前
Jetpack系列(二):Lifecycle与LiveData结合,打造响应式UI
android·android jetpack
kk爱闹9 小时前
【挑战14天学完python和pytorch】- day01
android·pytorch·python
每次的天空11 小时前
Android-自定义View的实战学习总结
android·学习·kotlin·音视频
恋猫de小郭11 小时前
Flutter Widget Preview 功能已合并到 master,提前在体验毛坯的预览支持
android·flutter·ios
断剑重铸之日12 小时前
Android自定义相机开发(类似OCR扫描相机)
android
随心最为安12 小时前
Android Library Maven 发布完整流程指南
android
岁月玲珑12 小时前
【使用Android Studio调试手机app时候手机老掉线问题】
android·ide·android studio
还鮟16 小时前
CTF Web的数组巧用
android
小蜜蜂嗡嗡18 小时前
Android Studio flutter项目运行、打包时间太长
android·flutter·android studio
aqi0018 小时前
FFmpeg开发笔记(七十一)使用国产的QPlayer2实现双播放器观看视频
android·ffmpeg·音视频·流媒体