【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()));
相关推荐
aningxiaoxixi12 分钟前
Android 之 audiotrack
android
枷锁—sha14 分钟前
【DVWA系列】——CSRF——Medium详细教程
android·服务器·前端·web安全·网络安全·csrf
Cao_Shixin攻城狮4 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
呼啦啦呼啦啦啦啦啦啦7 小时前
利用pdfjs实现的pdf预览简单demo(包含翻页功能)
android·javascript·pdf
idjl8 小时前
Mysql测试题
android·adb
游戏开发爱好者811 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
人生游戏牛马NPC1号11 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter
星辰也为你祝福h13 小时前
Android原生Dialog
android
梁同学与Android14 小时前
Android ---【CPU优化】需要优化的原因及优化的地方
android
Misha韩14 小时前
React Native 基础tabBar和自定义tabBar - bottom-tabs
android·react native