在Android開發中使用ViewPager,通常用在啓動引導界面,或者在使用Tab頁面。java
大體步驟爲:android
1、定義ViewPager對象、數據源對象(好比:List集合)app
2、實現PagerAdapter,重寫裏面的方法,好比通常實現如下四個方法:ide
3、通常實現OnPageChangeListener接口,重寫方法,ui
/* 當頁面在滑動的時候會調用此方法,在滑動被中止以前,此方法回一直獲得 調用。其中三個參數的含義分別爲: position :當前頁面,及你點擊滑動的頁面 positionOffset:當前頁面偏移的百分比 positionOffsetPixels:當前頁面偏移的像素位置 */ @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { Log.d("555", "onPageScrolled當前點擊頁面: " + position + "--當前頁面偏移的百分百: " + positionOffset + "--當前頁面偏移的像素位置: " + positionOffsetPixels); if (position == 3 && positionOffsetPixels >= 0) { Intent intent = new Intent(this, MainActivity.class); startActivity(intent); } } /* 此方法是頁面跳轉完後獲得調用,position是你當前選中的頁面的Position(位置編號) */ @Override public void onPageSelected(int position) { mImg[0].setBackgroundResource(R.drawable.page_indicator); mImg[1].setBackgroundResource(R.drawable.page_indicator); mImg[2].setBackgroundResource(R.drawable.page_indicator); mImg[3].setBackgroundResource(R.drawable.page_indicator); mImg[position].setBackgroundResource(R.drawable.page_indicator_focused); Log.d("555", "onPageSelected" + position); } /* 此方法是在狀態改變的時候調用 有三種狀態(0,1,2)。state ==1的時辰默示正在滑動, state==2的時辰默示滑動完畢了,state==0的時辰默示什麼都沒作。 當頁面開始滑動的時候,三種狀態的變化順序爲(1,2,0) */ @Override public void onPageScrollStateChanged(int state) { Log.d("555", "onPageScrollStateChanged狀態: " + state); }
在引導界面使用時,通常會用到this
這種小點,在滑動到第幾個頁面時,小點相應的突顯出來。在onPageSelected()方法中寫其改變的代碼。code
下面是總體代碼:對象
package com.li.uitext; import android.app.Activity; import android.content.Intent; 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.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.ImageView; import java.util.ArrayList; import butterknife.Bind; import butterknife.ButterKnife; /** * 啓動頁 * Created by useradmin on 2016/1/27. */ public class StartPagerActivity extends Activity implements OnPageChangeListener { @Bind(R.id.start_viewpager) ViewPager startViewpager; @Bind(R.id.start_img1) ImageView mImg1; @Bind(R.id.start_img2) ImageView mImg2; @Bind(R.id.start_img3) ImageView mImg3; @Bind(R.id.start_img4) ImageView mImg4; private StartPagerAdapter mPagerAdapter; private ArrayList<View> mView; private ImageView[] mImg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.startpager_activity); ButterKnife.bind(StartPagerActivity.this); mImg = new ImageView[]{mImg1, mImg2, mImg3, mImg4}; mView = new ArrayList<View>(); View layout1 = LayoutInflater.from(StartPagerActivity.this).inflate(R.layout.startimg1, null); View layout2 = LayoutInflater.from(StartPagerActivity.this).inflate(R.layout.startimg2, null); View layout3 = LayoutInflater.from(StartPagerActivity.this).inflate(R.layout.startimg3, null); View layout4 = LayoutInflater.from(StartPagerActivity.this).inflate(R.layout.startimg4, null); mView.add(layout1); mView.add(layout2); mView.add(layout3); mView.add(layout4); mImg[0].setBackgroundResource(R.drawable.page_indicator_focused); mPagerAdapter = new StartPagerAdapter(); startViewpager.setAdapter(mPagerAdapter); startViewpager.setOnPageChangeListener(this); } /* 當頁面在滑動的時候會調用此方法,在滑動被中止以前,此方法回一直獲得 調用。其中三個參數的含義分別爲: position :當前頁面,及你點擊滑動的頁面 positionOffset:當前頁面偏移的百分比 positionOffsetPixels:當前頁面偏移的像素位置 */ @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { Log.d("555", "onPageScrolled當前點擊頁面: " + position + "--當前頁面偏移的百分百: " + positionOffset + "--當前頁面偏移的像素位置: " + positionOffsetPixels); if (position == 3 && positionOffsetPixels >= 0) { Intent intent = new Intent(this, MainActivity.class); startActivity(intent); } } /* 此方法是頁面跳轉完後獲得調用,position是你當前選中的頁面的Position(位置編號) */ @Override public void onPageSelected(int position) { mImg[0].setBackgroundResource(R.drawable.page_indicator); mImg[1].setBackgroundResource(R.drawable.page_indicator); mImg[2].setBackgroundResource(R.drawable.page_indicator); mImg[3].setBackgroundResource(R.drawable.page_indicator); mImg[position].setBackgroundResource(R.drawable.page_indicator_focused); // for (int i = 0; i < mImg.length; i++) { // Log.d("555","for循環第"+position+"次"); // if (i == position) { // mImg[i].setBackgroundResource(R.drawable.page_indicator_focused); // } else { // mImg[i].setBackgroundResource(R.drawable.page_indicator); // } // } Log.d("555", "onPageSelected" + position); } /* 此方法是在狀態改變的時候調用 有三種狀態(0,1,2)。state ==1的時辰默示正在滑動, state==2的時辰默示滑動完畢了,state==0的時辰默示什麼都沒作。 當頁面開始滑動的時候,三種狀態的變化順序爲(1,2,0) */ @Override public void onPageScrollStateChanged(int state) { Log.d("555", "onPageScrollStateChanged狀態: " + state); } class StartPagerAdapter extends PagerAdapter { /* 返回數據源大小 */ @Override public int getCount() { return mView.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } /* 刪除頁面 */ @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView(mView.get(position)); } /* 添加新的頁面 */ @Override public Object instantiateItem(ViewGroup container, int position) { ((ViewPager) container).addView(mView.get(position), 0); return mView.get(position); } } }