首先给大家展示靓照,对效果图感兴趣的朋友可以继续往下阅读哦。
VIEwPager这个小demo实现的是可以左右循环滑动图片,下面带索引,滑到最后一页在往右滑动就要第一页,第一页往左滑动就到最后一页,上面是效果图,用美女图片是我一贯的作风,呵呵
1. 首先看一些layout下的xml
<FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" > <androID.support.v4.vIEw.VIEwPager androID:ID="@+ID/vIEwPager" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" /> <relativeLayout androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical" > <linearLayout androID:ID="@+ID/vIEwGroup" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_alignParentBottom="true" androID:layout_marginBottom="30dp" androID:gravity="center_horizontal" androID:orIEntation="horizontal" > </linearLayout> </relativeLayout> </FrameLayout>
使用VIEwPager首先需要引入androID-support-v4.jar这个jar包。自己不要忘记加
package com.example.vIEwpagerdemo; import androID.app.Activity; 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.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.vIEw.VIEwGroup.LayoutParams; import androID.Widget.ImageVIEw; import androID.Widget.linearLayout; public class TwoActivity extends Activity implements OnPagechangelistener{ /** * VIEwPager */ private VIEwPager vIEwPager; /** * 装点点的ImageVIEw数组 */ private ImageVIEw[] tips; /** * 装ImageVIEw数组 */ private ImageVIEw[] mImageVIEws; /** * 图片资源ID */ private int[] imgIDArray ; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); VIEwGroup group = (VIEwGroup)findVIEwByID(R.ID.vIEwGroup); vIEwPager = (VIEwPager) findVIEwByID(R.ID.vIEwPager); //载入图片资源ID imgIDArray = new int[]{R.drawable.item01,R.drawable.item02,R.drawable.item03,R.drawable.item04,R.drawable.item05,R.drawable.item06,R.drawable.item07,R.drawable.item08}; //将点点加入到VIEwGroup中 tips = new ImageVIEw[imgIDArray.length]; for(int i=0; i<tips.length; i++){ ImageVIEw imageVIEw = new ImageVIEw(this); imageVIEw.setLayoutParams(new LayoutParams(10,10)); tips[i] = imageVIEw; if(i == 0){ tips[i].setBackgroundResource(R.drawable.page_indicator_focused); }else{ tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused); } linearLayout.LayoutParams layoutParams = new linearLayout.LayoutParams(new VIEwGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); layoutParams.leftmargin = 5; layoutParams.rightmargin = 5; group.addVIEw(imageVIEw,layoutParams); } //将图片装载到数组中 mImageVIEws = new ImageVIEw[imgIDArray.length]; for(int i=0; i<mImageVIEws.length; i++){ ImageVIEw imageVIEw = new ImageVIEw(this); mImageVIEws[i] = imageVIEw; imageVIEw.setBackgroundResource(imgIDArray[i]); } //设置Adapter vIEwPager.setAdapter(new MyAdapter()); //设置监听,主要是设置点点的背景 vIEwPager.setonPagechangelistener(this); //设置VIEwPager的默认项,设置为长度的100倍,这样子开始就能往左滑动 vIEwPager.setCurrentItem((mImageVIEws.length) * 100); } /** * * @author xiaanming * */ public class MyAdapter extends PagerAdapter{ @OverrIDe public int getCount() { return Integer.MAX_VALUE; } @OverrIDe public boolean isVIEwFromObject(VIEw arg0,Object arg1) { return arg0 == arg1; } @OverrIDe public voID destroyItem(VIEw container,int position,Object object) { ((VIEwPager)container).removeVIEw(mImageVIEws[position % mImageVIEws.length]); } /** * 载入图片进去,用当前的position 除以 图片数组长度取余数是关键 */ @OverrIDe public Object instantiateItem(VIEw container,int position) { ((VIEwPager)container).addVIEw(mImageVIEws[position % mImageVIEws.length],0); return mImageVIEws[position % mImageVIEws.length]; } } @OverrIDe public voID onPageScrollStateChanged(int arg0) { } @OverrIDe public voID onPageScrolled(int arg0,float arg1,int arg2) { } @OverrIDe public voID onPageSelected(int arg0) { setimagebackground(arg0 % mImageVIEws.length); } /** * 设置选中的tip的背景 * @param selectItems */ private voID setimagebackground(int selectItems){ for(int i=0; i<tips.length; i++){ if(i == selectItems){ tips[i].setBackgroundResource(R.drawable.page_indicator_focused); }else{ tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused); } } } }
喜欢的朋友直接下载代码哦。代码下载
上面的代码中,当只有3张图片或者2张图片的时候,滑动存在BUG问题的修改如下
destroyItem(VIEw container,Object object)方法中不removeVIEw
@OverrIDe public voID destroyItem(VIEw container,Object object) { // ((VIEwPager)container).removeVIEw(mImageVIEws[position % mImageVIEws.length]); } instantiateItem(VIEw container,int position)增加异常扑捉[java] vIEw plaincopy在CODE上查看代码片派生到我的代码片@OverrIDe public Object instantiateItem(VIEw container,int position) { try { ((VIEwPager)container).addVIEw(mImageVIEws[position % mImageVIEws.length],0); }catch(Exception e){ //handler something } return mImageVIEws[position % mImageVIEws.length]; }
以上代码给大家展示了AndroID 利用VIEwPager实现图片可以左右循环滑动效果,希望大家喜欢。
总结以上是内存溢出为你收集整理的Android 利用ViewPager实现图片可以左右循环滑动效果附代码下载全部内容,希望文章能够帮你解决Android 利用ViewPager实现图片可以左右循环滑动效果附代码下载所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)