在Fragment中添加Handler:
private static class BannerHandler extends Handler {
@Override
public void handleMessage(Message msg) {
if (msg.what == MSG_SWITCH_PIC) {
if (vpBanner != null) {
if (switchPictureAdapter != null) {
vpBanner.setCurrentItem(vpBanner.getCurrentItem() + 1);
this.sendEmptyMessageDelayed(MSG_SWITCH_PIC,
BANNER_TIME_PEROID);
}
}
}
}
}
初始化:
bannerHandler = new BannerHandler();
bannerHandler.sendEmptyMessageDelayed(MSG_SWITCH_PIC,
BANNER_TIME_PEROID);
mImgUrls.clear();
mImgUrls.add("https://www.baidu.com/img/bd_logo1.png");
mImgUrls.add("https://www.baidu.com/img/bd_logo1.png");
mImgUrls.add("https://www.baidu.com/img/bd_logo1.png");
switchPictureAdapter = new SwitchPictureAdapter(mActivity, vpBanner,
mImgUrls, view);
vpBanner.setAdapter(switchPictureAdapter);
if (mImgUrls.size() > 1) {
vpBanner.setCurrentItem(1);
}
轮播图的适配器:
public class SwitchPictureAdapter extends PagerAdapter {
private Context mContext;
private List mImageViews;
private List mImgUrls;
private LinearLayout mPoints;
private ViewPager mViewPage;
public SwitchPictureAdapter(Context mContext, ViewPager page,
List mImgUrls, View view) {
super();
this.mContext = mContext;
this.mImgUrls = mImgUrls;
this.mImageViews = new ArrayList();
this.mViewPage = page;
this.mPoints = (LinearLayout) view.findViewById(R.id.ll_point);
mPoints.removeAllViews();
initData();
initViews();
this.mViewPage.setOnPageChangeListener(new PagerListener());
}
private void initData() {
// 实现无限循环,另外添加两张图片
mImgUrls.add(0, mImgUrls.get(mImgUrls.size() - 1)); // 第一个位置添加最后一张
mImgUrls.add(mImgUrls.get(1));// 最后一个位置添加第0张
// 添加点
for (int i = 0; i < mImgUrls.size() - 2; i++) {
ImageView point = new ImageView(mContext);
int dp = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 8, mContext.getResources()
.getDisplayMetrics());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
dp, dp);
int margin = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 4, mContext.getResources()
.getDisplayMetrics());
params.setMargins(margin, 0, margin, 0);
point.setLayoutParams(params);
if (i == 0) {
point.setBackgroundResource(R.drawable.point_red);
} else {
point.setBackgroundResource(R.drawable.point_normal);
}
mPoints.addView(point);
}
}
private void initViews() {
if (mImgUrls != null) {
for (int i = 0; i < mImgUrls.size(); i++) {
ImageView view = new ImageView(mContext);
view.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
ImageLoadUtil.loadImage(mImgUrls.get(i), view);
mImageViews.add(view);
if (mImgUrls.size() <= 3) {// 说明只有一张图片
break;
}
}
}
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView view = mImageViews.get(position);
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeAllViews();
}
container.addView(view);
return view;
}
@Override
public int getCount() {
if (mImgUrls == null) {
return 0;
}
if (mImgUrls.size() <= 3) {
return 1;
}
return mImgUrls.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((ImageView) object);
}
private class PagerListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int pos) {
if (mImageViews.size() > 1) { // 图片多余一张才轮播
if (pos < 1) {
pos = mImageViews.size() - 2;
mViewPage.setCurrentItem(pos, false);
} else if (pos > mImageViews.size() - 2) {
mViewPage.setCurrentItem(1, false);
pos = 1;
}
for (int i = 0; i < mPoints.getChildCount(); i++) {
if (pos == i + 1) {
mPoints.getChildAt(i).setBackgroundResource(
R.drawable.point_red);
} else {
mPoints.getChildAt(i).setBackgroundResource(
R.drawable.point_normal);
}
}
}
}
}
}
手指移动不播放
/**
* 手滑动banner时不自动播放
*/
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.vp_banner) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
bannerHandler.removeMessages(MSG_SWITCH_PIC);
break;
case MotionEvent.ACTION_MOVE:
bannerHandler.removeMessages(MSG_SWITCH_PIC);
break;
case MotionEvent.ACTION_UP:
bannerHandler.sendEmptyMessageDelayed(MSG_SWITCH_PIC,
BANNER_TIME_PEROID);
break;
case MotionEvent.ACTION_CANCEL:
bannerHandler.sendEmptyMessageDelayed(MSG_SWITCH_PIC,
BANNER_TIME_PEROID);
break;
default:
break;
}
}
return false;
}