本文是基于viewpager实现的无限自动轮播banner,供大家参考,具体内容如下
分为四步去实现:
第一步是有限手动轮播;
第二步是无限轮播;
第三步是自动轮播;
第四步是指示器适配
第一步:有限手动轮播实现
布局:
adapter实现:
public class banneradapter extends pageradapter { private listbannerlist; public banneradapter(list bannerlist) { this.bannerlist = bannerlist; } @override public int getcount() { return bannerlist.size(); } @override public boolean isviewfromobject(@nonnull view view, @nonnull object object) { return view == object; } @nonnull @override public object instantiateitem(@nonnull viewgroup container, int position) { imageview bannerimageview = new imageview(container.getcontext()); viewgroup.layoutparams lp = new viewgroup.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent); bannerimageview.setlayoutparams(lp); bannerimageview.setscaletype(imageview.scaletype.fit_xy); glide.with(container.getcontext()).load(bannerlist.get(position)).into(bannerimageview); container.addview(bannerimageview); return bannerimageview; } @override public void destroyitem(@nonnull viewgroup container, int position, @nonnull object object) { container.removeview((view) object); } }
activity中:
// scrollview中viewpager一定要设置高度,此处根据图片的宽高比来设定高度 int bannerwidth = (utils.getscreenwidth(getcontext()) - utils.dip2pixel(getcontext(), 24)); linearlayout.layoutparams lp = (linearlayout.layoutparams) bannerview.getlayoutparams(); lp.width = linearlayout.layoutparams.match_parent; lp.height = (int) (bannerwidth * 90f / 345); bannerview.setlayoutparams(lp); bannerview.setadapter(new banneradapter(geturllist()));
注意:scrollview包裹viewpager时,viewpager的高度一定要有确定值,否则内容无法加载出来,可以在xml中指定,也可以代码设定,但一定要有确定值。
第二步:无限轮播
无限轮播只需要在有限轮播的基础上,做以下两个改动点,修改getcount返回值且在加载数据时获取正确的数据源即可
public class banneradapter extends pageradapter { private listbannerlist; public banneradapter(list bannerlist) { this.bannerlist = bannerlist; } @override public int getcount() { // return bannerlist.size(); // before return integer.max_value; // now } @override public boolean isviewfromobject(@nonnull view view, @nonnull object object) { return view == object; } @nonnull @override public object instantiateitem(@nonnull viewgroup container, int position) { imageview bannerimageview = new imageview(container.getcontext()); int realposition = position % bannerlist.size(); // 获取要加载数据的真实位置 viewgroup.layoutparams lp = new viewgroup.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent); bannerimageview.setlayoutparams(lp); bannerimageview.setscaletype(imageview.scaletype.fit_xy); // glide.with(container.getcontext()).load(bannerlist.get(position)).into(bannerimageview); // before glide.with(container.getcontext()).load(bannerlist.get(realposition)).into(bannerimageview); // now container.addview(bannerimageview); return bannerimageview; } @override public void destroyitem(@nonnull viewgroup container, int position, @nonnull object object) { container.removeview((view) object); } }
修改完发现banner只能向右无限轮播,第一次左滑滑不动,这个时候我们强制设置viewpager位置在中间就可以解决这个问题了
bannerview.setadapter(new banneradapter(geturllist())); bannerview.setcurrentitem(geturllist().size() * 5);
第三步:自动轮播
handler每隔轮播间隔发送消息,设置viewpager为下一个位置
private runnable bannerrunnable = new runnable() { @override public void run() { bannerview.setcurrentitem(bannerview.getcurrentitem() 1); mhandler.postdelayed(bannerrunnable, 3000); } }; bannerview.addonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { } @override public void onpageselected(int position) { // 手滑动到某一位置,重新开始计时 start(); } @override public void onpagescrollstatechanged(int state) { } }); private void start() { mhandler.removecallbacksandmessages(null); mhandler.postdelayed(bannerrunnable, 3000); }
第四步:添加指示器
指示器样式及表现可以自己去根据需求实现,以相对简单和常见的小圆圈指示器为例,添加和banner数量相同的小圆圈,小圆圈设置selector,在选中时为黑色选中样式,在非选中时为灰色默认样式,根据当前选中的banner的实际position,设置指示器的selected属性,从而展示不同的样式
private void initindicator() { for (int i = 0; i < geturllist().size(); i ) { view view = new view(getactivity()); linearlayout.layoutparams lp = new linearlayout.layoutparams(utils.dip2pixel(getactivity(), 6), utils.dip2pixel(getactivity(), 6)); lp.rightmargin = utils.dip2pixel(getactivity(), 8); view.setlayoutparams(lp); view.setbackgroundresource(r.drawable.selector_indicator_view); view.setselected(i == 0); llindicatorview.addview(view); } } private void initbannerview() { bannerview.addonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { } @override public void onpageselected(int position) { // 手滑动到某一位置,重新开始计时 realposition = 0; realposition = position % geturllist().size(); // 根据当前滑动到的banner设置指示器的状态 for (int i = 0; i < llindicatorview.getchildcount(); i ) { llindicatorview.getchildat(i).setselected(i == realposition); } start(); } @override public void onpagescrollstatechanged(int state) { } }); }
附:utils文件
public class utils { public static void setfullscreen(activity activity) { if (build.version.sdk_int >= build.version_codes.m) { viewgroup decorview = (viewgroup) activity.getwindow().getdecorview(); decorview.setsystemuivisibility(view.system_ui_flag_layout_fullscreen | view.system_ui_flag_light_status_bar); activity.getwindow().setstatusbarcolor(color.transparent); } } public static int dip2pixel(context context, float n) { int value = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, n, context.getresources().getdisplaymetrics()); return value; } /** * 获取屏幕宽度 * @param context * @return 屏幕宽度 */ public static int getscreenwidth(context context) { windowmanager wm = (windowmanager) context .getsystemservice(context.window_service); displaymetrics outmetrics = new displaymetrics(); wm.getdefaultdisplay().getmetrics(outmetrics); return outmetrics.widthpixels; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。