ViewPager轮播图自动无限循环滑动,手指按住停止滑动


ViewPager轮播图是很常见的功能了,但是项目总是用到,总结下加强记忆.

以下用到的都是再我公司的项目中抽取出来的

1,首先是adapter

这里有两点:

a,getCount()的返回值是Integer.MAX_VALUE;

b,instantiateItem使用到的位置是position % _myList.size();


package com.teekart.app.travel;

import im.yixin.sdk.util.BitmapUtil;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.lidroid.xutils.BitmapUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.teekart.app.R;
import com.teekart.app.beans.QueryProductsInfo.Msg.ProductMsg;
import com.teekart.app.bookcourse.OpenBigPicActivity;

class PicPagerAdapter extends PagerAdapter {

	private ArrayList _myList;
	private Context context;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;

	public PicPagerAdapter(Context context ,ArrayList myList) {
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
		.showImageForEmptyUri(R.drawable.list_placeholder)
		.showImageOnFail(R.drawable.list_placeholder)
		.resetViewBeforeLoading(true).cacheOnDisk(true)
		.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
		.bitmapConfig(Bitmap.Config.RGB_565).considerExifParams(true)
		.displayer(new FadeInBitmapDisplayer(300))
		.bitmapConfig(Bitmap.Config.RGB_565)
		.imageScaleType(ImageScaleType.IN_SAMPLE_INT).build();
		_myList = myList;
		this.context = context;
	}

	@Override
	public int getCount() {
//		return _myList.size();
		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) {
		container.removeView((View) object);
	}

	/**
	 * 初始化条目
	 */
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		ImageView imageView = new ImageView(context);
		imageView.setScaleType(ScaleType.CENTER_CROP);
		imageView.setTag(position);
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			int position=(Integer) v.getTag();
			Intent intent=new Intent(context,OpenBigPicActivity.class);
			intent.putExtra("position", position);
			intent.putExtra("urlList", _myList);
			context.startActivity(intent);
			}
		});
		
		imageLoader.displayImage(_myList.get(position % _myList.size()), imageView, options);
//		imageLoader.displayImage(_myList.get(position), imageView, options);
		// 1. 将要显示的界面加到容器中
		container.addView(imageView);

		// 2. 返回给控制器, 执行super方法会报错
		return imageView;
	}

}


2,添加顶部banner图.

/**
	 * 添加顶部的banner图
	 */
	private void addTopPic() {
	
		topImglist = new ArrayList();
		topImglist = travelDetailInfo.topGallerys;

		ArrayList mListData = new ArrayList();
		ImageView iv;
		for (int i = 0; i < topImglist.size(); i++) {
			iv = new ImageView(this);
			imageLoader.displayImage(topImglist.get(i), iv,options);
//			BitmapUtils bitmapUtil = new BitmapUtils(this);
//			bitmapUtil.display(iv, topImglist.get(i));
			mListData.add(iv);

		}

		vp_travel.setAdapter(new PicPagerAdapter(this, topImglist));
		tv_page.setText(1+"/"+topImglist.size());
		vp_travel.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				 int previousSelectPosition = position  % topImglist.size();;
				tv_page.setText(previousSelectPosition+1+"/"+topImglist.size());
//				tv_page.setText(position+1+"/"+topImglist.size());
			}

			@Override
			public void onPageScrollStateChanged(int state) {
			}

			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
			}

		});
		vp_travel.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
//				case MotionEvent.ACTION_DOWN:
//					System.out.println("ddddddddddddd");
//					toloop = false;
//					break;
				case MotionEvent.ACTION_MOVE:
					System.out.println("mmmmmmmmmm");
					toloop = false;
					break;
				case MotionEvent.ACTION_UP:
					System.out.println("uuuuuuuuuuuuuuuuuuuu");
					toloop = true;
					break;

				default:
					break;
				}
				
				return false;
			}
		});
	}



3,自动切换功能实现并设置当前位置

private void setTopImgGo() {
		// 自动切换页面功能  
       new Thread(new Runnable() {  
 
       

		@Override  
           public void run() {  
               while (isLoop) {  
                   SystemClock.sleep(3000);  
                   if (toloop) {
                	   handler.sendEmptyMessage(0);  
                   }
               }  
           }  
       }).start();  
       /** 
        * 2147483647 / 2 = 1073741820 - 1  
        * 设置ViewPager的当前项为一个比较大的数,以便一开始就可以左右循环滑动 
        */  
       int n = Integer.MAX_VALUE / 2 % topImglist.size();  
       int itemPosition = Integer.MAX_VALUE / 2 - n;  
         
       vp_travel.setCurrentItem(itemPosition);
	}


4,最后效果图


ViewPager轮播图自动无限循环滑动,手指按住停止滑动_第1张图片






你可能感兴趣的:(Android)