废话就不多说了,直接上效果图和代码
fry.Activity01
package fry; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import com.example.viewPager.R; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.LinearLayout; public class Activity01 extends Activity implements OnPageChangeListener,OnClickListener{ private ViewPager viewPager; private Listviews;//用来存放放进ViewPager里面的ImageView private int[] imageID;// private ImagePagerAdapter adapter;//适配器 private LinearLayout linearLayout; private int currentPoint;//当前被选中中页面的下标 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); initData(); initView(); } //初始化view void initView(){ adapter=new ImagePagerAdapter(views); viewPager=(ViewPager) findViewById(R.id.viewPager); viewPager.setAdapter(adapter); linearLayout=(LinearLayout) findViewById(R.id.linearLayout); initPoint(); viewPager.setOnPageChangeListener(this); } //初始化数据 void initData(){ //将图片的id全部放到imageID集合中 imageID=new int[]{R.drawable.guide1,R.drawable.guide2,R.drawable.guide3,R.drawable.guide4}; views=new ArrayList (); //给iamgeView设置LayoutParams参数 LayoutParams layoutParams=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); for(int i:imageID){ //不确定1 ImageView imageView=new ImageView(this); imageView.setImageResource(i); imageView.setLayoutParams(layoutParams); views.add(imageView); } } /* * 初始化页面下方的点 */ private void initPoint(){ ImageView[] imageView=new ImageView[imageID.length]; for(int i=0;i
fry.ImagePagerAdapter
package fry; import java.util.ArrayList; import java.util.List; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; public class ImagePagerAdapter extends PagerAdapter{ private Listviews; public ImagePagerAdapter(List views) { this.views=views; } @Override public int getCount() { // TODO Auto-generated method stub return this.views.size(); } /** * position是在viewPager中显示图片的位置 * 我对应的图片放到对应的位置就好了 * instantiateItem和destroyItem是对应的 * 一个是创建item,一个是销毁item */ @Override public Object instantiateItem(ViewGroup container, int position) { ((ViewPager)container).addView(views.get(position)); return views.get(position); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager)container).removeView(views.get(position)); } }
/viewPager/res/layout/activity01.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
1、多个控件(这里指小圆点)可以用List来一起访问
2、可用动态的控件设置tag来区分控件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。