自己简单封装了一个带hint的轮播ViewPager,用来展示app首页的Banner,先看效果图吧。
ezgif-1-437f7aee24.gif- dependencies(依赖)
compile 'com.coldmoqiuli:banners:1.0.0'
使用起来非常方便~~2至3行代码就能实现上述效果,只要传入一个list就可以(其中是你需要展示的ImageView),当然也可以自己在这基础上添加各种Transform动画。
bannerView = (BannerView) findViewById(R.id.banner);
bannerView.setViewList(viewList);
bannerView.startLoop(true);
实现原理相信大家应该都知道,就是可能懒得去封装这种东西。
具体思路:
- 继承自FrameLayout,在下层填充一个ViewPager
-
2.根据ViewPager的itemCount去动态生成底部hint小圆点
if (mLinearPosition.getChildCount() != viewSize) {
int diffCnt = mLinearPosition.getChildCount() - viewSize;
boolean needAdd = diffCnt < 0;
diffCnt = Math.abs(diffCnt);
for (int i = 0; i < diffCnt; i++) {
if (needAdd) {
ImageView img = new ImageView(getContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.rightMargin = getResources().getDimensionPixelOffset(R.dimen.dimen_9dp);
img.setLayoutParams(layoutParams);
img.setBackgroundResource(R.drawable.banner_point);
mLinearPosition.addView(img);
} else {
mLinearPosition.removeViewAt(0);
}
}
}
- 3.通过Handler去控制轮播的频率,重新设置ViewPager的currentItem以及hint小圆点的背景
private static class BannerHandler extends Handler {
private WeakReference<BannerView> weakReference = null;
public BannerHandler(BannerView bannerView) {
super(Looper.getMainLooper());
this.weakReference = new WeakReference<BannerView>(bannerView);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (this.weakReference == null) {
return;
}
BannerView bannerView = this.weakReference.get();
if (bannerView == null || bannerView.mViewPager == null || bannerView.mViewPager.getAdapter() == null || bannerView.mViewPager.getAdapter().getCount() <= 0) {
sendEmptyMessageDelayed(MSG_LOOP, LOOP_INTERVAL);
return;
}
int curPos = bannerView.mViewPager.getCurrentItem();
curPos = (curPos + 1) % bannerView.mViewPager.getAdapter().getCount();
bannerView.mViewPager.setCurrentItem(curPos);
sendEmptyMessageDelayed(MSG_LOOP, LOOP_INTERVAL);
}
}
- 4.具体细节,如无限轮播adapter的创建,handler中的弱引用,启动销毁loop等等
网友评论
public void setViewList(List<YourImageBean> viewList) {
this.viewList = viewList;
if (viewList != null && viewList.size() != 0) {
viewSize = viewList.size();
if (viewSize == 2) {
viewList.add(viewList.get(0));
viewList.add(viewList.get(1));
}
}
List<View> imageViews = new ArrayList<>();
for (int i = 0; i < viewList.size(); i++) {
ImageView image = new ImageView(getContext());
image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//设置显示格式
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setImageResource(viewList.get(i));
imageViews.add(image);
}
BannerAdapter bannerAdapter = new BannerAdapter(imageViews);
setAdapter(bannerAdapter);
}
把new ImageView的操作放在bannerview里面就可以了,直接list.get会出错
public void setViewList(List<View> viewList) {
this.viewList = viewList;
if (viewList != null && viewList.size() != 0) {
viewSize = viewList.size();
if (viewSize == 1){
viewList.add(viewList.get(0));
}else if(viewSize == 2){
viewList.add(viewList.get(0));
viewList.add(viewList.get(1));
}
BannerAdapter bannerAdapter = new BannerAdapter(viewList);
setAdapter(bannerAdapter);
}
}