Android框架之路——Banner实现轮播图(RecyclerView添加Header)

本文详细介绍了如何在Android中使用Banner框架实现轮播图,包括添加依赖、设置数据、添加动画和样式,以及自定义Banner的属性。通过示例代码展示了如何在RecyclerView中添加Header并实现轮播功能,同时提供了Demo下载和源码链接。
摘要由CSDN通过智能技术生成

一、简介

Banner能实现循环播放多个广告图片和手动滑动循环等功能。因为原生ViewPager并不支持循环翻页, 要实现循环还得需要自己去动手。Banner框架可以进行不同样式、不同动画设置, 以及完善的api方法能满足大部分软件首页轮播图效果的需求。

有一篇博客讲了一些,可以参考banner框架,一个比较齐全的框架;

此项目的Github地址链接

本篇教程依然涉及一些其他方面的东西,如果有问题,可以参考我之前的系列文章。

二、添加依赖

  1. build.gradle中添加依赖:

    dependencies{
        compile 'com.youth.banner:banner:1.4.9'  //最新版本
    }
    
  2. 添加权限:

    <!-- if you want to load images from the internet -->
    <uses-permission android:name="android.permission.INTERNET" /> 
    
    <!-- if you want to load images from a file OR from the internet -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    

三、解锁技能

1. 加载数据和刷新数据:

  • 首先,添加我们MainActivity的布局文件,里面含有一个SwipeRefreshLayout,包裹RecyclerView,SwipeRefreshLayout用来实现下拉刷新。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context="com.ping.bannerdemo.activity.MainActivity">
    
        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
            </android.support.v7.widget.RecyclerView>
    
        </android.support.v4.widget.SwipeRefreshLayout>
    
    </LinearLayout>
    
  • 然后,我们要在RecyclerView中添加俩种布局,一个Header存放Banner,另一个则是正常的item布局。先放上俩个布局的内容。

    //header.xml
    <?xml version="1.0" encoding="utf-8"?>
    <com.youth.banner.Banner
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
    //item_recycler.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width&#
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值