Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android(8) ViewPager頁面滑動切換

android(8) ViewPager頁面滑動切換

編輯:關於Android編程

一.ViewPager頁面滑動切換實現:

借鑒了別人的源碼,還是比較容易實現的,而且這種效果還是經常使用的,特此記錄一下:

效果圖:

\

\

主界面:

 

public class MainActivity extends Activity {

	private ViewPager mPager;// 頁卡內容
	private List listViews; // Tab頁面列表
	private ImageView cursor;// 動畫圖片
	private TextView t1, t2, t3;// 頁卡頭標
	private int offset = 0;// 動畫圖片偏移量
	private int currIndex = 0;// 當前頁卡編號
	private int bmpW;// 動畫圖片寬度

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab_main);
		InitImageView();
		InitTextView();
		InitViewPager();
	}

	// 初始選項
	private void InitTextView() {
		t1 = (TextView) findViewById(R.id.text1);
		t2 = (TextView) findViewById(R.id.text2);
		t3 = (TextView) findViewById(R.id.text3);

		t1.setOnClickListener(new MyOnClickListener(0));
		t2.setOnClickListener(new MyOnClickListener(1));
		t3.setOnClickListener(new MyOnClickListener(2));
	}

	// 初始化ViewPager
	private void InitViewPager() {
		mPager = (ViewPager) findViewById(R.id.vPager);
		listViews = new ArrayList();
		LayoutInflater mInflater = getLayoutInflater();
		listViews.add(mInflater
				.inflate(R.layout.activity_tab_one_pager, null));
		listViews.add(mInflater
				.inflate(R.layout.activity_tab_two_pager, null));
		listViews.add(mInflater
				.inflate(R.layout.activity_tab_three_pager, null));
		mPager.setAdapter(new MyViewPagerAdapter(listViews, this));
		//默認選中第一個頁面
		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	// 初始化動畫
	private void InitImageView() {
		cursor = (ImageView) findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(),
				R.drawable.icon_tab_page_bg).getWidth();// 獲取圖片寬度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 獲取分辨率寬度
		offset = (screenW / 3 - bmpW) / 2;// 計算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		cursor.setImageMatrix(matrix);// 設置動畫初始位置
	}

	// 頁卡監聽
	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		public void onClick(View v) {
			mPager.setCurrentItem(index);
		}
	};

	// 滑動監聽
	public class MyOnPageChangeListener implements OnPageChangeListener {

		int one = offset * 2 + bmpW;// 頁卡1 -> 頁卡2 偏移量
		int two = one * 2;// 頁卡1 -> 頁卡3 偏移量
        //arg0表示頁數
		public void onPageSelected(int arg0) {
			// TranslateAnimation位移動畫
			Animation animation = null;
			//表示從第一個頁卡跳轉到當前頁卡
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(one, 0, 0, 0);
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(two, 0, 0, 0);
				}
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, one, 0, 0);
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(two, one, 0, 0);
				}
				break;
			case 2:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, two, 0, 0);
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(one, two, 0, 0);
				}
				break;
			}
			currIndex = arg0;
			animation.setFillAfter(true);// true:表示圖片停在動畫結束位置
			animation.setDuration(300);
			cursor.startAnimation(animation);
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageScrollStateChanged(int arg0) {
		}
	}

}

viewPager適配器:
public class MyViewPagerAdapter extends PagerAdapter {
	private List mListViews;
	private Context context;

	public MyViewPagerAdapter(List mListViews, Context context) {
		this.mListViews = mListViews;
		this.context = context;
	}

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView(mListViews.get(arg1));
	}

	@Override
	public void finishUpdate(View arg0) {
	}

	@Override
	public int getCount() {
		return mListViews.size();
	}

	@Override
	public Object instantiateItem(View arg0, int arg1) {

		if (arg1 < 3) {
			((ViewPager) arg0).addView(mListViews.get(arg1 ), 0);
		}
		// 頁卡內點擊事件
		if (arg1 == 0) {
			Button btn = (Button) arg0.findViewById(R.id.btn);
			btn.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					showDialog(1);
				}
			});
		}
		if (arg1 == 1) {
			Button btn = (Button) arg0.findViewById(R.id.btn);
			btn.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					showDialog(2);
				}
			});
		}
		if (arg1 == 2) {
			Button btn = (Button) arg0.findViewById(R.id.btn);
			btn.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					showDialog(3);
				}
			});
		}
		return mListViews.get(arg1);
	}

	public void showDialog(String arg) {
		new AlertDialog.Builder(context).setTitle(說明)
				.setMessage(這是第+arg+個頁面)
				.setNegativeButton(確定, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				}).show();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == (arg1);
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
	}

}

xml:

 



三個頁面:

 

 



ok完成了。

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved