|
| 1 | +# CustomBanner |
| 2 | +Android轮播图控件,支持任何View的轮播,而不仅仅是图片(ImageView),完全自定义的轮播指示器和轮播图样式,使用非常简单。下面是CustomBanner具体使用讲解: |
| 3 | +### 1、引入依赖 |
| 4 | +在Project的build.gradle在添加以下代码 |
| 5 | +``` |
| 6 | + allprojects { |
| 7 | + repositories { |
| 8 | + ... |
| 9 | + maven { url 'https://jitpack.io' } |
| 10 | + } |
| 11 | + } |
| 12 | +``` |
| 13 | +在Module的build.gradle在添加以下代码 |
| 14 | +``` |
| 15 | + dependencies { |
| 16 | + compile 'com.github.donkingliang:CustomBanner:1.0.0' |
| 17 | + } |
| 18 | +``` |
| 19 | +### 2、编写布局 |
| 20 | +``` |
| 21 | + <com.donkingliang.banner.CustomBanner |
| 22 | + xmlns:app="http://schemas.android.com/apk/res-auto" |
| 23 | + android:id="@+id/banner" |
| 24 | + android:layout_width="match_parent" |
| 25 | + android:layout_height="180dp" |
| 26 | + app:indicatorGravity="CENTER_HORIZONTAL" //指示器的位置 有左。中、右三个值 |
| 27 | + app:indicatorSelectRes="@drawable/shape_point_select" //指示器的选中的样式 |
| 28 | + app:indicatorUnSelectRes="@drawable/shape_point_unselect" />//指示器的未选中的样式 |
| 29 | +``` |
| 30 | +### 3、CustomBanner的方法使用 |
| 31 | +#### 1)、设置数据 |
| 32 | +``` |
| 33 | + mBanner.setPages(new CustomBanner.ViewCreator<String>() { |
| 34 | + @Override |
| 35 | + public View createView(Context context, int position) { |
| 36 | + //这里返回的是轮播图的项的布局 支持任何的布局 |
| 37 | + //position 轮播图的第几个项 |
| 38 | + ImageView imageView = new ImageView(context); |
| 39 | + return imageView; |
| 40 | + } |
| 41 | +
|
| 42 | + @Override |
| 43 | + public void updateUI(Context context, View view, int position, String data) { |
| 44 | + //在这里更新轮播图的UI |
| 45 | + //position 轮播图的第几个项 |
| 46 | + //view 轮播图当前项的布局 它是createView方法的返回值 |
| 47 | + //data 轮播图当前项对应的数据 |
| 48 | + Glide.with(context).load(data).into((ImageView) view); |
| 49 | + } |
| 50 | + }, beans); |
| 51 | +``` |
| 52 | +轮播图的布局支持任何的布局,轮播图的数据类型也是支持任何的数据类型,这里只是用ImageView和String举例而已。 |
| 53 | +#### 2)、其他方法的使用 |
| 54 | +``` |
| 55 | + //设置两个点图片作为翻页指示器,不设置则没有指示器, |
| 56 | + //这个方法跟在布局中设置app:indicatorSelectRes、app:indicatorUnSelectRes是一样的。 |
| 57 | + //第一个参数是指示器的选中的样式,第二个参数是指示器的未选中的样式 |
| 58 | + mBanner.setIndicatorRes(R.drawable.shape_point_select,R.drawable.shape_point_unselect); |
| 59 | +
|
| 60 | + //设置指示器的方向,这个方法跟在布局中设置app:indicatorGravity是一样的 |
| 61 | + mBanner.setIndicatorGravity(CustomBanner.IndicatorGravity.CENTER_HORIZONTAL) |
| 62 | +
|
| 63 | + //设置轮播图自动滚动轮播,参数是轮播图滚动的间隔时间 |
| 64 | + //轮播图默认是不自动滚动的,如果不调用这个方法,轮播图将不会自动滚动。 |
| 65 | + mBanner.startTurning(3600); |
| 66 | +
|
| 67 | + //停止轮播图的自动滚动 |
| 68 | + mBanner.stopTurning(); |
| 69 | +
|
| 70 | + //设置轮播图的滚动速度 |
| 71 | + mBanner.setScrollDuration(500); |
| 72 | +
|
| 73 | + //设置轮播图的点击事件 |
| 74 | + mBanner.setOnPageClickListener(new CustomBanner.OnPageClickListener<String>() { |
| 75 | + @Override |
| 76 | + public void onPageClick(int position, String str) { |
| 77 | + //position 轮播图的第几个项 |
| 78 | + //str 轮播图当前项对应的数据 |
| 79 | + } |
| 80 | + }); |
| 81 | +``` |
| 82 | +以上是CustomBanner的主要常用方法,更多方法请查看源码。 |
| 83 | +#### 3)、CustomBanner的很多方法都支持方法连缀,比如以下的方法可以这样调用。 |
| 84 | +``` |
| 85 | + mBanner.setPages(参数, 参数).setIndicatorRes(参数, 参数).setIndicatorGravity(参数).startTurning(参数); |
| 86 | +``` |
| 87 | +### 效果图 |
| 88 | + |
| 89 | + |
| 90 | +在我的博客中对该轮播图控件的实现和核心代码有详细的分析讲解,欢迎大家访问[我的博客](http://blog.csdn.net/u010177022) |
| 91 | +[Android轮播图控件的实现详解](http://blog.csdn.net/u010177022/article/details/61931488) |
0 commit comments