自己简单封装了一个带hint的轮播ViewPager,用来展示app首页的Banner,先看效果图吧。
- 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等等