【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()));
相关推荐
灿烂阳光g7 小时前
domain_auto_trans,source_domain,untrusted_app
android·linux
低调小一9 小时前
Android传统开发 vs Android Compose vs HarmonyOS ArkUI 对照表
android·华为·harmonyos
雨白9 小时前
Java 多线程指南:从基础用法到线程安全
android·java
00后程序员张10 小时前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
程序员江同学11 小时前
ovCompose + AI 开发跨三端的 Now in Kotlin App
android·kotlin·harmonyos
2501_9151063211 小时前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
消失的旧时光-194311 小时前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
灿烂阳光g13 小时前
SELinux 策略文件编写
android·linux
.豆鲨包13 小时前
【Android】Viewpager2实现无限轮播图
android·java