您的位置:首页 > 移动开发 > Android开发

Android轮播图控件CustomBanner的使用讲解

2017-03-14 20:29 609 查看
今天给大家讲解的是Android轮播图控件CustomBanner的使用。CustomBanner是我在GitHub上传的一个Android轮播图控件。在上一篇博客 《Android轮播图控件的实现详解》中,我详细分析了CustomBanner的实现思路和核心代码,还没有看过的同学建议先看一下,这样无论是你想自己实现一个轮播图控件,还是使用CustomBanner都大有好处。

现在我们开始讲解CustomBanner的具体使用,CustomBanner在GitHub的地址是:https://github.com/donkingliang/CustomBanner

1、引入依赖

在Project的build.gradle在添加以下代码

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}


在Module的build.gradle在添加以下代码

dependencies {
compile 'com.github.donkingliang:CustomBanner:1.1.0'
}


2、编写布局

<!-- 设置普通指示器 -->
<com.donkingliang.banner.CustomBanner
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="180dp"
app:indicatorStyle="ORDINARY"  //指示器类型 普通指示器
app:indicatorGravity="CENTER_HORIZONTAL" //指示器的位置 有左。中、右三个值
app:indicatorSelectRes="@drawable/shape_point_select" //指示器的选中的样式
app:indicatorUnSelectRes="@drawable/shape_point_unselect" //指示器的未选中的样式
app:indicatorInterval="5dp"/> //指示器的点的间隔

<!-- 设置数字指示器 -->
<com.donkingliang.banner.CustomBanner
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="180dp"
app:indicatorStyle="NUMBER" //指示器类型 数字指示器
app:indicatorGravity="RIGHT"/> //指示器的位置 有左。中、右三个值

<!-- 设置没有指示器 -->
<com.donkingliang.banner.CustomBanner
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="180dp"
app:indicatorStyle="NONE"/> //指示器类型 没有指示器


3、CustomBanner的方法使用

1)、设置数据

mBanner.setPages(new CustomBanner.ViewCreator<String>() {
@Override
public View createView(Context context, int position) {
//这里返回的是轮播图的项的布局 支持任何的布局
//position 轮播图的第几个项
ImageView imageView = new ImageView(context);
return imageView;
}

@Override
public void updateUI(Context context, View view, int position, String data) {
//在这里更新轮播图的UI
//position 轮播图的第几个项
//view 轮播图当前项的布局 它是createView方法的返回值
//data 轮播图当前项对应的数据
Glide.with(context).load(data).into((ImageView) view);
}
}, beans);


轮播图的布局支持任何的布局,轮播图的数据类型也是支持任何的数据类型,这里只是用ImageView和String举例而已。

2)、其他方法的使用

//设置指示器类型,有普通指示器(ORDINARY)、数字指示器(NUMBER)和没有指示器(NONE)三种类型。
//这个方法跟在布局中设置app:indicatorStyle是一样的
mBanner.setIndicatorStyle(CustomBanner.IndicatorStyle.ORDINARY);

//设置两个点图片作为翻页指示器,只有指示器为普通指示器(ORDINARY)时有用。
//这个方法跟在布局中设置app:indicatorSelectRes、app:indicatorUnSelectRes是一样的。
//第一个参数是指示器的选中的样式,第二个参数是指示器的未选中的样式。
mBanner.setIndicatorRes(R.drawable.shape_point_select,R.drawable.shape_point_unselect);

//设置指示器的指示点间隔,只有指示器为普通指示器(ORDINARY)时有用。
//这个方法跟在布局中设置app:indicatorInterval是一样的。
mBanner.setIndicatorInterval(20)

//设置指示器的方向。
//这个方法跟在布局中设置app:indicatorGravity是一样的。
mBanner.setIndicatorGravity(CustomBanner.IndicatorGravity.CENTER_HORIZONTAL)

//设置轮播图自动滚动轮播,参数是轮播图滚动的间隔时间
//轮播图默认是不自动滚动的,如果不调用这个方法,轮播图将不会自动滚动。
mBanner.startTurning(3600);

//停止轮播图的自动滚动
mBanner.stopTurning();

//设置轮播图的滚动速度
mBanner.setScrollDuration(500);

//设置轮播图的点击事件
mBanner.setOnPageClickListener(new CustomBanner.OnPageClickListener<String>() {
@Override
public void onPageClick(int position, String str) {
//position 轮播图的第几个项
//str 轮播图当前项对应的数据
}
});


以上是CustomBanner的主要常用方法,更多方法请查看源码。

3)、CustomBanner的很多方法都支持方法连缀,比如以下的方法可以这样调用。

mBanner.setPages(参数, 参数).setIndicatorRes(参数, 参数).setIndicatorGravity(参数).startTurning(参数);


效果图



CustomBanner的使用就介绍到这里了,大家在使用中如果发现什么问题或是有什么建议,欢迎评论留言。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息