废话就不多说了,直接上效果图和代码
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 List<VIEw> vIEws;//用来存放放进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<VIEw>(); //给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<linearLayout.getChildCount();i++){ imageVIEw[i]=(ImageVIEw) linearLayout.getChildAt(i); imageVIEw[i].setimageResource(R.drawable.point_normal); imageVIEw[i].setonClickListener(this); imageVIEw[i].setTag(i); } currentPoint=0; imageVIEw[currentPoint].setimageResource(R.drawable.point_select); } /* * 将页面下方的点全部置为未选中状态 */ private ImageVIEw[] clearPoint(){ ImageVIEw[] imageVIEw=new ImageVIEw[imageID.length]; for(int i=0;i<linearLayout.getChildCount();i++){ imageVIEw[i]=(ImageVIEw) linearLayout.getChildAt(i); imageVIEw[i].setimageResource(R.drawable.point_normal); } return imageVIEw; } @OverrIDe public voID onPageScrollStateChanged(int arg0) { // Todo auto-generated method stub } @OverrIDe public voID onPageScrolled(int arg0,float arg1,int arg2) { // Todo auto-generated method stub } /* * 页面被选中时候执行的方法 * (non-Javadoc) * @see androID.support.v4.vIEw.VIEwPager.OnPagechangelistener#onPageSelected(int) */ @OverrIDe public voID onPageSelected(int position) { // Todo auto-generated method stub ImageVIEw[] imageVIEw= clearPoint(); imageVIEw[position].setimageResource(R.drawable.point_select); } //小圆点点击事件 @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub int i=(Integer) v.getTag(); vIEwPager.setCurrentItem(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 List<VIEw> vIEws; public ImagePagerAdapter(List<VIEw> 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
<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <androID.support.v4.vIEw.VIEwPager androID:ID="@+ID/vIEwPager" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > </androID.support.v4.vIEw.VIEwPager> <linearLayout androID:ID="@+ID/linearLayout" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:orIEntation="horizontal" androID:layout_gravity="bottom|center" > <ImageVIEw androID:ID="@+ID/point1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:src="@drawable/point_select" androID:padding="25dip" /> <ImageVIEw androID:ID="@+ID/point2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:src="@drawable/point_normal" androID:padding="25dip" /> <ImageVIEw androID:ID="@+ID/point3" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:src="@drawable/point_normal" androID:padding="25dip" /> <ImageVIEw androID:ID="@+ID/point4" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:src="@drawable/point_normal" androID:padding="25dip" /> </linearLayout></FrameLayout>
1、多个控件(这里指小圆点)可以用List来一起访问
2、可用动态的控件设置tag来区分控件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的ViewPager实现漂亮的引导页全部内容,希望文章能够帮你解决ViewPager实现漂亮的引导页所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)