【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()));
相关推荐
开维游戏引擎1 小时前
AI自动生成游戏时,deepseek和mimo对比
android·游戏·语言模型·游戏引擎·ai编程
BreezeDove8 小时前
【Android】AS项目自动连接mumu模拟器配置
android
乐世东方客11 小时前
备份脚本记录(binlog文件+mysql+mongo)
android·数据库·mysql
私人珍藏库11 小时前
[Android] 视频下载鸟 v20.02 会员
android·人工智能·智能手机·app·工具·多功能
zh_xuan11 小时前
tv浏览网页工具
android·tv浏览网页
Carson带你学Android13 小时前
Compose 终于上线 FlexBox:换行与弹性伸缩 都轻松搞定!
android·composer
私人珍藏库13 小时前
[Android] 三维山水全景地图-3D地形全景观测地图
android·3d·app·工具·软件·多功能
dengyuezhe806013 小时前
《C++ 异常机制与智能指针:从原理到实现》
android·java·c++
Wonderful U14 小时前
Python+Django实战|企业办公用品申领管理系统:物资入库、库存预警、申领审批、归还登记、损耗统计、供应商对账
android·python·django
plainGeekDev14 小时前
网络状态监听 → ConnectivityManager + Flow
android·java·kotlin