ViewPager實現引導頁 廢話就不多說了,直接上效果圖和代碼 fry.Activity01 fry.ImagePagerAdapter /viewPager/res/layout/activity01.xml 1、多個控制項(這裡指小圓點)可以用List來一起訪問 2、可用動態的控制項設置tag來區 ...
ViewPager實現引導頁
廢話就不多說了,直接上效果圖和代碼
fry.Activity01
1 package fry; 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.ListIterator; 8 9 import com.example.viewPager.R; 10 11 import android.app.Activity; 12 import android.os.Bundle; 13 import android.support.v4.view.ViewPager; 14 import android.support.v4.view.ViewPager.OnPageChangeListener; 15 import android.view.View; 16 import android.view.View.OnClickListener; 17 import android.view.ViewGroup.LayoutParams; 18 import android.widget.ImageView; 19 import android.widget.LinearLayout; 20 21 public class Activity01 extends Activity implements OnPageChangeListener,OnClickListener{ 22 private ViewPager viewPager; 23 private List<View> views;//用來存放放進ViewPager裡面的ImageView 24 private int[] imageID;// 25 private ImagePagerAdapter adapter;//適配器 26 private LinearLayout linearLayout; 27 private int currentPoint;//當前被選中中頁面的下標 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 // TODO Auto-generated method stub 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity01); 33 initData(); 34 initView(); 35 36 } 37 //初始化view 38 void initView(){ 39 adapter=new ImagePagerAdapter(views); 40 viewPager=(ViewPager) findViewById(R.id.viewPager); 41 viewPager.setAdapter(adapter); 42 linearLayout=(LinearLayout) findViewById(R.id.linearLayout); 43 initPoint(); 44 viewPager.setOnPageChangeListener(this); 45 } 46 47 //初始化數據 48 void initData(){ 49 //將圖片的id全部放到imageID集合中 50 imageID=new int[]{R.drawable.guide1,R.drawable.guide2,R.drawable.guide3,R.drawable.guide4}; 51 views=new ArrayList<View>(); 52 //給iamgeView設置LayoutParams參數 53 LayoutParams layoutParams=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 54 for(int i:imageID){ 55 //不確定1 56 ImageView imageView=new ImageView(this); 57 imageView.setImageResource(i); 58 imageView.setLayoutParams(layoutParams); 59 views.add(imageView); 60 } 61 62 63 } 64 65 /* 66 * 初始化頁面下方的點 67 */ 68 private void initPoint(){ 69 ImageView[] imageView=new ImageView[imageID.length]; 70 for(int i=0;i<linearLayout.getChildCount();i++){ 71 imageView[i]=(ImageView) linearLayout.getChildAt(i); 72 imageView[i].setImageResource(R.drawable.point_normal); 73 imageView[i].setOnClickListener(this); 74 imageView[i].setTag(i); 75 } 76 currentPoint=0; 77 imageView[currentPoint].setImageResource(R.drawable.point_select); 78 } 79 /* 80 * 將頁面下方的點全部置為未選中狀態 81 */ 82 private ImageView[] clearPoint(){ 83 ImageView[] imageView=new ImageView[imageID.length]; 84 for(int i=0;i<linearLayout.getChildCount();i++){ 85 imageView[i]=(ImageView) linearLayout.getChildAt(i); 86 imageView[i].setImageResource(R.drawable.point_normal); 87 } 88 return imageView; 89 } 90 91 @Override 92 public void onPageScrollStateChanged(int arg0) { 93 // TODO Auto-generated method stub 94 95 } 96 97 @Override 98 public void onPageScrolled(int arg0, float arg1, int arg2) { 99 // TODO Auto-generated method stub 100 101 } 102 103 /* 104 * 頁面被選中時候執行的方法 105 * (non-Javadoc) 106 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int) 107 */ 108 @Override 109 public void onPageSelected(int position) { 110 // TODO Auto-generated method stub 111 ImageView[] imageView= clearPoint(); 112 imageView[position].setImageResource(R.drawable.point_select); 113 } 114 115 //小圓點點擊事件 116 @Override 117 public void onClick(View v) { 118 // TODO Auto-generated method stub 119 int i=(Integer) v.getTag(); 120 viewPager.setCurrentItem(i); 121 } 122 123 }
fry.ImagePagerAdapter
1 package fry; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.support.v4.view.PagerAdapter; 7 import android.support.v4.view.ViewPager; 8 import android.view.View; 9 import android.view.ViewGroup; 10 11 public class ImagePagerAdapter extends PagerAdapter{ 12 13 private List<View> views; 14 15 16 17 public ImagePagerAdapter(List<View> views) { 18 this.views=views; 19 } 20 21 @Override 22 public int getCount() { 23 // TODO Auto-generated method stub 24 return this.views.size(); 25 } 26 /** 27 * position是在viewPager中顯示圖片的位置 28 * 我對應的圖片放到對應的位置就好了 29 * instantiateItem和destroyItem是對應的 30 * 一個是創建item,一個是銷毀item 31 */ 32 @Override 33 public Object instantiateItem(ViewGroup container, int position) { 34 ((ViewPager)container).addView(views.get(position)); 35 return views.get(position); 36 } 37 38 @Override 39 public boolean isViewFromObject(View arg0, Object arg1) { 40 // TODO Auto-generated method stub 41 return arg0==arg1; 42 } 43 44 @Override 45 public void destroyItem(ViewGroup container, int position, Object object) { 46 ((ViewPager)container).removeView(views.get(position)); 47 } 48 49 }
/viewPager/res/layout/activity01.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <android.support.v4.view.ViewPager 8 android:id="@+id/viewPager" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 > 12 13 </android.support.v4.view.ViewPager> 14 15 <LinearLayout 16 android:id="@+id/linearLayout" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:orientation="horizontal" 20 android:layout_gravity="bottom|center" 21 > 22 <ImageView 23 android:id="@+id/point1" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:src="@drawable/point_select" 27 android:padding="25dip" 28 /> 29 <ImageView 30 android:id="@+id/point2" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:src="@drawable/point_normal" 34 android:padding="25dip" 35 /> 36 <ImageView 37 android:id="@+id/point3" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:src="@drawable/point_normal" 41 android:padding="25dip" 42 /> 43 <ImageView 44 android:id="@+id/point4" 45 android:layout_width="wrap_content" 46 android:layout_height="wrap_content" 47 android:src="@drawable/point_normal" 48 android:padding="25dip" 49 /> 50 51 </LinearLayout> 52 53 </FrameLayout>
1、多個控制項(這裡指小圓點)可以用List來一起訪問
2、可用動態的控制項設置tag來區分控制項