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 Listviews;//用来存放放进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 (); 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
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 Listviews;14 15 16 17 public ImagePagerAdapter(List views) {18 this.views=views;19 }20 21 @Override22 public int getCount() {23 // TODO Auto-generated method stub24 return this.views.size();25 }26 /**27 * position是在viewPager中显示图片的位置28 * 我对应的图片放到对应的位置就好了29 * instantiateItem和destroyItem是对应的30 * 一个是创建item,一个是销毁item31 */32 @Override33 public Object instantiateItem(ViewGroup container, int position) {34 ((ViewPager)container).addView(views.get(position));35 return views.get(position);36 }37 38 @Override39 public boolean isViewFromObject(View arg0, Object arg1) {40 // TODO Auto-generated method stub41 return arg0==arg1;42 }43 44 @Override45 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 26 7 12 13 14 1522 52 5329 36 43 50 51
1、多个控件(这里指小圆点)可以用List来一起访问
2、可用动态的控件设置tag来区分控件