Android BannerView通用封装详解,android banner轮播

6
回复
638
查看
[复制链接]

400

主题

1104

帖子

336

安币

手工艺人

发表于 2018-5-8 11:58:46 | 显示全部楼层 |阅读模式

            

        之前封装过一个,但总觉得不够优雅,就有了这个通用封装,很简洁,不知道够不够优雅,不过原来那个有跟随指示器和丝滑滑动效果,感兴趣可以看一下。

        封装过程

        1、自定义属性

        selectpoint:选中指示器图标
normalpoint:未选中指示器图标
pointwidth:图标宽度
switchtime:轮播间隔事件
location:指示器位置,下中或下右

[Java] 查看源文件 复制代码
<declare-styleable name="newbannerview">
    <attr name="selectpoint" format="reference" />
    <attr name="normalpoint" format="reference" />
    <attr name="pointwidth" format="dimension" />
    <attr name="switchtime" format="integer" />
    <attr name="location">
      <enum name="center" value="0" />
      <enum name="right" value="1" />
    </attr>
  </declare-styleable>

        2、初始化view
初始化viewpager和指示器组合view

        3、绑定数据源
通过setimagedata设置轮播图数据源

        4、绑定点击事件
通过onpageclicklistener绑定点击事件

        5、开启关闭轮播
start和stop方法开启和关闭轮播

        用法

        xml中

[Java] 查看源文件 复制代码
<com.goldou.lovesee.view.newbannerview
    android:id="@+id/bannerview"
    android:layout_width="match_parent"
    app:selectpoint="@drawable/red_point"
    app:normalpoint="@drawable/gray_point"
    app:switchtime="4000"
    app:location="right"
    android:layout_height="200dp" />

        activity中

[Java] 查看源文件 复制代码
int[] imagelist = {r.drawable.me_top, r.drawable.me_top, r.drawable.me_top, r.drawable.me_top};
    newbannerview bannerview = view.findviewbyid(r.id.bannerview);
    bannerview.setimagedata(imagelist);
    bannerview.start();
    bannerview.setonpageclicklistener(new newbannerview.onpageclicklistener() {
      @override
      public void onpageclick(int position) {
        toast.maketext(getactivity(), position + "", toast.length_short).show();
      }
    });

        bannerview

[Java] 查看源文件 复制代码
public class newbannerview extends relativelayout implements view.onclicklistener {
  private context context;
  private int selectpoint, normalpoint;
  private float pointwidth = 7;
  private int location;
  private int center = 0, right = 1;
  private int lastposition = 0;
  private viewpager viewpager;
  private int switchtime = 5000;
  private int[] images;
  private onpageclicklistener onpageclicklistener;

  private handler handler = new handler(new handler.callback() {
    @override
    public boolean handlemessage(message msg) {
      if (msg.what == 101) {
        viewpager.setcurrentitem(viewpager.getcurrentitem() + 1);
        start();
      }
      return false;
    }
  });

  public newbannerview(context context) {
    this(context, null);
  }

  public newbannerview(context context, attributeset attrs) {
    this(context, attrs, 0);
  }

  public newbannerview(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
    this.context = context;
    initattr(attrs);
  }

  private void initattr(attributeset attrs) {
    typedarray array = context.obtainstyledattributes(attrs, r.styleable.newbannerview);
    selectpoint = array.getresourceid(r.styleable.newbannerview_selectpoint, r.mipmap.ic_launcher_round);
    normalpoint = array.getresourceid(r.styleable.newbannerview_normalpoint, r.mipmap.ic_launcher_round);
    pointwidth = array.getdimension(r.styleable.newbannerview_pointwidth, pointwidth);
    location = array.getinteger(r.styleable.newbannerview_location, right);
    switchtime = array.getinteger(r.styleable.newbannerview_switchtime, switchtime);
    array.recycle();
  }

  public void setimagedata(final int[] images) {
    this.images = images;
    final linearlayout ll_point = new linearlayout(context);
    layoutparams layoutparams = new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content);
    layoutparams.addrule(align_parent_bottom, relativelayout.true);
    layoutparams.bottommargin = 20;
    if (location == center) {
      layoutparams.addrule(center_horizontal, relativelayout.true);
    } else {
      layoutparams.addrule(align_parent_right, relativelayout.true);
      layoutparams.rightmargin = 20;
    }
    linearlayout.layoutparams params1 = new linearlayout.layoutparams(uiutil.dip2px(pointwidth), uiutil.dip2px(pointwidth));
    linearlayout.layoutparams params2 = new linearlayout.layoutparams(uiutil.dip2px(pointwidth), uiutil.dip2px(pointwidth));
    params1.leftmargin = 0;
    params2.leftmargin = uiutil.dip2px(pointwidth);
    for (int i = 0; i < images.length; i++) {
      imageview point = new imageview(context);
      if (i == 0) {
        point.setbackgroundresource(selectpoint);
        point.setlayoutparams(params1);
      } else {
        point.setbackgroundresource(normalpoint);
        point.setlayoutparams(params2);
      }
      ll_point.addview(point);
    }

    viewpager = new viewpager(context);
    viewpager.setadapter(new banneradapter());
    viewpager.addonpagechangelistener(new viewpager.onpagechangelistener() {
      @override
      public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {

      }

      @override
      public void onpageselected(int position) {
        position = position % images.length;
        if (lastposition == position) {
          return;
        }
        ll_point.getchildat(position).setbackgroundresource(selectpoint);
        ll_point.getchildat(lastposition).setbackgroundresource(normalpoint);
        lastposition = position;
      }

      @override
      public void onpagescrollstatechanged(int state) {

      }
    });
    layoutparams layoutparams1 = new layoutparams(layoutparams.match_parent, layoutparams.match_parent);
    addview(viewpager, layoutparams1);
    addview(ll_point, layoutparams);
  }

  public void start() {
    handler.sendemptymessagedelayed(101, switchtime);
  }

  public void stop() {
    handler.removecallbacksandmessages(null);
  }

  private int getcurrentitem() {
    return viewpager.getcurrentitem() % images.length;
  }

  @override
  public void onclick(view view) {
    onpageclicklistener.onpageclick(getcurrentitem());
  }

  public interface onpageclicklistener {
    void onpageclick(int position);
  }

  public void setonpageclicklistener(onpageclicklistener onpageclicklistener) {
    this.onpageclicklistener = onpageclicklistener;
  }

  private class banneradapter extends pageradapter {

    @override
    public int getcount() {
      return integer.max_value;
    }

    @override
    public boolean isviewfromobject(view view, object object) {
      return view == object;
    }

    @override
    public object instantiateitem(viewgroup container, int position) {
      position = position % images.length;
      imageview imageview = new imageview(context);
      imageview.setimageresource(images[position]);
      imageview.setscaletype(imageview.scaletype.fit_xy);
      imageview.setonclicklistener(newbannerview.this);
      container.addview(imageview);
      return imageview;
    }

    @override
    public void destroyitem(viewgroup container, int position, object object) {
      container.removeview((view) object);
    }
  }
}







        

429

主题

1140

帖子

1907

安币

手工艺人

发表于 2018-5-8 12:30:17 | 显示全部楼层
感谢分享,楼主V5~

0

主题

9179

帖子

2379

安币

Android大神

Rank: 6Rank: 6

发表于 2018-5-8 13:16:26 | 显示全部楼层
帮帮顶顶!!

434

主题

1104

帖子

473

安币

手工艺人

发表于 2018-5-8 13:48:27 | 显示全部楼层
楼主是好人,回个帖会有安币吗?

9

主题

9589

帖子

1779

安币

Android大神

Rank: 6Rank: 6

发表于 2018-5-8 14:43:04 | 显示全部楼层
感谢分享,楼主V5~

443

主题

1174

帖子

1960

安币

手工艺人

发表于 2018-5-8 15:31:20 | 显示全部楼层
支持楼主,支持安卓巴士!

6

主题

9548

帖子

2876

安币

Android大神

Rank: 6Rank: 6

发表于 2018-5-8 15:58:26 | 显示全部楼层
支持楼主,支持安卓巴士!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

领先的中文移动开发者社区
18620764416
7*24全天服务
意见反馈:1294855032@qq.com

扫一扫关注我们

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粤ICP备15117877号 )