直接上马,添加依赖:
implementation 'com.bigkoo:convenientbanner:2.0.5'
布局:
<com.bigkoo.convenientbanner.ConvenientBanner
android:id="@+id/main_banner"
android:layout_width="match_parent"
android:layout_height="350px"
app:canLoop="true">
</com.bigkoo.convenientbanner.ConvenientBanner>
设置banner:(imglist 传的数据是图片,imgs 点击时所用code。我加了一个if判断,如果只有一个图片就不翻页了)
public void banners(final ArrayList<String> imglist, List<BannerDto<BannerImageDto>> imgs) {
// setBannerIndicatorLocation();
Log.e("imglist","imglist.size() = "+imglist.size());
if (imglist.size() == 1) {
//开始自动翻页
convenientBanner.setPages(new CBViewHolderCreator() {
@Override
public Object createHolder() {
return new NetworkImageHolderView();
}
}, imglist)
//设置指示器是否可见
.setPointViewVisible(true)
//设置两个点图片作为翻页指示器,不设置则没有指示器,可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
.setPageIndicator(new int[]{R.mipmap.lb_nor, R.mipmap.lb_sel})
//设置指示器的方向(左、中、右)
.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
//设置点击监听事件
.setOnItemClickListener(this)
//设置手动影响(设置了该项无法手动切换)
.setManualPageable(true);
} else {
//开始自动翻页
convenientBanner.setPages(new CBViewHolderCreator() {
@Override
public Object createHolder() {
return new NetworkImageHolderView();
}
}, imglist)
//设置指示器是否可见
.setPointViewVisible(true)
//设置自动切换(同时设置了切换时间间隔)
.startTurning(2000)
//设置两个点图片作为翻页指示器,不设置则没有指示器,可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
.setPageIndicator(new int[]{R.mipmap.lb_nor, R.mipmap.lb_sel})
//设置指示器的方向(左、中、右)
.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
//设置点击监听事件
.setOnItemClickListener(this)
//设置手动影响(设置了该项无法手动切换)
.setManualPageable(true);
}
//设置翻页的效果,不需要翻页效果可用不设
//setPageTransformer(Transformer.DefaultTransformer); // 集成特效之后会有白屏现象,新版已经分离,如果要集成特效的例子可以看Demo的点击响应。
convenientBanner.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int position) {
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(imgs.get(position).getImgs().get(0).getHttpUrl());
m.find();
if (SZTSGTS.equals(returnResultMultiple(imgs.get(position).getImgs().get(0).getHttpUrl()))) {
Log.e("group", "m.group(); = " + m.group());
Map<String, String> map = new HashMap<>();
map.put(SZTSGTS, m.group());
startActivity(activity, ExerciseActivity.class, map);
} else {
Map map = new HashMap();
map.put("webviewurl", imgs.get(position).getImgs().get(0).getUrl());
map.put("title", imgs.get(position).getImgs().get(0).getName());
startActivity(activity, WebViewActivity.class, map);
}
}
});
}