一、简介
Banner能实现循环播放多个广告图片和手动滑动循环等功能。因为原生ViewPager并不支持循环翻页, 要实现循环还得需要自己去动手。Banner框架可以进行不同样式、不同动画设置, 以及完善的api方法能满足大部分软件首页轮播图效果的需求。
有一篇博客讲了一些,可以参考banner框架,一个比较齐全的框架;
此项目的Github地址:链接。
本篇教程依然涉及一些其他方面的东西,如果有问题,可以参考我之前的系列文章。
二、添加依赖
build.gradle中添加依赖:
dependencies{ compile 'com.youth.banner:banner:1.4.9' //最新版本 }
添加权限:
<!-- 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&#