本文实例讲述了Android开发实现广告无限循环功能。分享给大家供大家参考,具体如下:
一、效果图:
二、代码实现:
/** * 新闻首页 * * @Project App_Card * @Package com.android.koomama.fragment.home * @author chenlin * @version 1.0 * @Date 2014年6月22日 * @Note TODO */ public class NewsHomeFragment extends BaseFragment implements OnTouchListener { private ImageHandler mHandler = new ImageHandler(new WeakReference(this)); private ViewPager mViewPager; // 自定义轮播图的资源 private int[] mImageResIds = { R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5 }; // 放轮播图片的ImageView 的list private List mImageList = new ArrayList (); // 放圆点的View的list private List mDotList = new ArrayList (); @Override public View initConvertView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_news_main, container, false); } @Override protected void onVisible(boolean isInit) { if (isInit) { initViews(); } } private void initViews() { // 初始化iewPager的内容 mViewPager = (ViewPager) mConvertView.findViewById(R.id.view_pager); LinearLayout dotLayout = (LinearLayout) mConvertView.findViewById(R.id.dotLayout); dotLayout.removeAllViews(); for (int i = 0; i < mImageResIds.length; i++) { ImageView imageView = new ImageView(getActivity()); imageView.setScaleType(ScaleType.FIT_XY); // view.setTag(mImageResId[i]); imageView.setImageResource(mImageResIds[i]); mImageList.add(imageView); View dotView = new View(getActivity()); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen.dot_width), getResources().getDimensionPixelSize(R.dimen.dot_width)); params.setMargins(4, 0, 4, 0); dotView.setLayoutParams(params); // if (i == 0) { // dotView.setBackgroundResource(R.drawable.dot_red); // } else { // dotView.setBackgroundResource(R.drawable.dot_gray); // } dotLayout.addView(dotView); mDotList.add(dotView); } mViewPager.setAdapter(new ImageAdapter(mImageList)); mViewPager.setOnPageChangeListener(new PageChangeListener()); mViewPager.setFocusable(true); mViewPager.setCurrentItem(Integer.MAX_VALUE / 2);// 默认在中间,使用户看不到边界 mViewPager.setOnTouchListener(this); // 开始轮播效果 mHandler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, ImageHandler.MSG_DELAY); } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mHandler.sendEmptyMessage(ImageHandler.MSG_KEEP_SILENT); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mHandler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, ImageHandler.MSG_DELAY); break; } return false; } private class PageChangeListener implements OnPageChangeListener { // 配合Adapter的currentItem字段进行设置。 @Override public void onPageSelected(int position) { // 这个是设置左滑切换底部view的边界,必须要设置 App.getInstance().setBorderViewPosition(position); mHandler.sendMessage(Message.obtain(mHandler, ImageHandler.MSG_PAGE_CHANGED, position, 0)); } @Override public void onPageScrolled(int position, float arg1, int arg2) { } // 覆写该方法实现轮播效果的暂停和恢复 @Override public void onPageScrollStateChanged(int state) { switch (state) { case ViewPager.SCROLL_STATE_DRAGGING: mHandler.sendEmptyMessage(ImageHandler.MSG_KEEP_SILENT); break; case ViewPager.SCROLL_STATE_IDLE: mHandler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, ImageHandler.MSG_DELAY); break; default: break; } } } private class ImageAdapter extends PagerAdapter { private List viewlist; public ImageAdapter(List viewlist) { this.viewlist = viewlist; } @Override public int getCount() { // 设置成最大,使用户看不到边界 return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // Warning:不要在这里调用removeView } @Override public Object instantiateItem(ViewGroup container, int position) { // 对ViewPager页号求模取出View列表中要显示的项 position %= viewlist.size(); if (position < 0) { position = viewlist.size() + position; } ImageView view = viewlist.get(position); // 如果View已经在之前添加到了一个父组件,则必须先remove,否则会抛出IllegalStateException。 ViewParent vp = view.getParent(); if (vp != null) { ViewGroup parent = (ViewGroup) vp; parent.removeView(view); } container.addView(view); // 此处可添加监听事件 return view; } } private static class ImageHandler extends Handler { /** * 请求更新显示的View。 */ protected static final int MSG_UPDATE_IMAGE = 1; /** * 请求暂停轮播。 */ protected static final int MSG_KEEP_SILENT = 2; /** * 请求恢复轮播。 */ protected static final int MSG_BREAK_SILENT = 3; /** * 记录最新的页号,当用户手动滑动时需要记录新页号,否则会使轮播的页面出错。 * 例如当前如果在第一页,本来准备播放的是第二页,而这时候用户滑动到了末页, * 则应该播放的是第一页,如果继续按照原来的第二页播放,则逻辑上有问题。 */ protected static final int MSG_PAGE_CHANGED = 4; // 轮播间隔时间 protected static final long MSG_DELAY = 3000; // 使用弱引用避免Handler泄露.这里的泛型参数可以不是Activity,也可以是Fragment等 private WeakReference weakReference; private int currentItem = 0; // private boolean isOnce = true; protected ImageHandler(WeakReference wk) { weakReference = wk; } @Override public void handleMessage(Message msg) { super.handleMessage(msg); final NewsHomeFragment fragment = weakReference.get(); if (fragment == null) { // Activity已经回收,无需再处理UI了 return; } // 第一次不删重复的消息 if (currentItem != 0) { // 检查消息队列并移除未发送的消息,这主要是避免在复杂环境下消息出现重复等问题。 if (fragment.mHandler.hasMessages(MSG_UPDATE_IMAGE)) { fragment.mHandler.removeMessages(MSG_UPDATE_IMAGE); } } switch (msg.what) { case MSG_UPDATE_IMAGE: currentItem++; fragment.mViewPager.setCurrentItem(currentItem); // 准备下次播放 fragment.mHandler.sendEmptyMessageDelayed(MSG_UPDATE_IMAGE, MSG_DELAY); break; case MSG_KEEP_SILENT: // 只要不发送消息就暂停了 break; case MSG_BREAK_SILENT: fragment.mHandler.sendEmptyMessageDelayed(MSG_UPDATE_IMAGE, MSG_DELAY); break; case MSG_PAGE_CHANGED: // 记录当前的页号,避免播放的时候页面显示不正确。 int position = msg.arg1; int lastIndex = 0; int index = position % fragment.mImageList.size(); for (int i = 0; i < fragment.mDotList.size(); i++) { fragment.mDotList.get(i).setBackgroundResource(R.drawable.dot_normal); } if (fragment.mDotList.get(index)!=null) { fragment.mDotList.get(index).setBackgroundResource(R.drawable.dot_red); } lastIndex = index; currentItem = position; break; default: break; } } } }
三、布局