1. 程式人生 > >Android 使用ViewPager真正實現左右無限滑動(附原始碼)

Android 使用ViewPager真正實現左右無限滑動(附原始碼)

package com.viewpager;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.app.Activity;

public class MainActivity01 extends Activity implements OnPageChangeListener {

	private ViewPager viewPager;
	static final int arrays[] = { R.drawable.guide1, R.drawable.guide2,
			R.drawable.guide3, R.drawable.guide4, R.drawable.guide5 };
	private List<ImageView> views;
	private int currentPage = 0;
	private ImageView imageView;

	private MyViewPagerAdapter viewPagerAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initWidget();
	}

	private void initWidget() {
		viewPager = (ViewPager) findViewById(R.id.viewpager);

		views = new ArrayList<ImageView>();
		for (int i = 0; i < 3; i++) {
			imageView = new ImageView(this);
			imageView.setLayoutParams(new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.MATCH_PARENT));
			views.add(imageView);
		}

		initImageData();

		viewPagerAdapter = new MyViewPagerAdapter();
		viewPager.setAdapter(viewPagerAdapter);
		viewPager.setCurrentItem(1);
		viewPager.setOnPageChangeListener(this);
	}

	private void initImageData() {
		for (int i = 0; i < 3; i++) {
			imageView = views.get(i);
			if (i == 0) {
				imageView.setImageResource(arrays[arrays.length - 1]);
			} else {
				imageView.setImageResource(arrays[i - 1]);
			}
		}
	}

	class MyViewPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return views.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			imageView = views.get(position);
			container.addView(imageView);
			return imageView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			container.removeView((View) object);
		}

	}

	// 當滑動狀態改變時呼叫
	@Override
	public void onPageScrollStateChanged(int state) {
		// TODO Auto-generated method stub
		System.out.println("--onPageScrollStateChanged--state--:" + state);
		switch (state) {
		// 在滾動完成後
		case ViewPager.SCROLL_STATE_IDLE:
			int currentItem = viewPager.getCurrentItem();

			System.out.println("--currentItem--00--:" + currentItem);
			System.out.println("--currentPage--00--:" + currentPage);
			if (viewPager.getCurrentItem() == 1) {
				// 如果位置沒有變終止迴圈
				break;
			}

			if (viewPager.getCurrentItem() > 1) {
				currentPage++;
			} else {
				currentPage--;
			}

			System.out.println("--currentPage--11--:" + currentPage);
			if (currentPage == arrays.length) {
				currentPage = 0;
			}

			if (currentPage == -1) {
				currentPage = arrays.length - 1;
			}

			System.out.println("--currentPage--22--:" + currentPage);

			if (currentPage == 0) {
				views.get(0).setImageResource(arrays[arrays.length - 1]);
			} else {
				views.get(0).setImageResource(arrays[currentPage - 1]);
			}

			views.get(1).setImageResource(arrays[currentPage]);

			if (currentPage == arrays.length - 1) {
				views.get(2).setImageResource(arrays[0]);
			} else {
				views.get(2).setImageResource(arrays[currentPage + 1]);
			}

			viewPager.setCurrentItem(1, false);

			currentItem = viewPager.getCurrentItem();

			System.out.println("--currentItem--11--:" + currentItem);
			break;
		}
	}

	// 噹噹前頁面被滑動時呼叫
	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {
		// TODO Auto-generated method stub
		// System.out.println("--onPageScrolled--position--:" + position);
	}

	// 當新的頁面被選中時呼叫
	@Override
	public void onPageSelected(int position) {
		// TODO Auto-generated method stub
		System.out.println("--onPageSelected--position--:" + position);
	}
}