【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()));
相关推荐
我不会插花弄玉4 小时前
7.函数【由浅入深-MySQL】
android·数据库·mysql
vilya5 小时前
手机端 Agent 怎么动起来:Kotlin 与 Python 之间的桥
android·agent
高晶5 小时前
Android 新手实战:从零搭建 WebRTC 在线直播系统
android·程序员
alexhilton5 小时前
为什么你的 LazyColumn 仍然卡顿
android·kotlin·android jetpack
>Andre<7 小时前
系统架构全景
android
hai_android11 小时前
Android 嵌套滑动冲突:内部拦截 vs 外部拦截实战
android·android studio·android jetpack
木子欢儿12 小时前
Zabbix 7.4 Alpine 版 Docker Compose 部署
android·运维·docker·容器·zabbix
素师良码13 小时前
第9篇:Android驱动踩坑实录:DSI RX FIFO前置脏0、数据错位复盘
android
夜雪一千14 小时前
如何在MySQL中实现字段级加密
android·数据库·mysql
夜雪一千14 小时前
MySQL 如何存储经纬度坐标:选型、建表、空间查询实战
android·mysql·adb