实现无限循环
在getCount()方法中,返回一个很大的值,Integer.MAX_VALUE
在instantiateItem()方法中,获取当前VIEw的索引时,进行取于 *** 作,传递进来的int position是个非常大的数,对他进行求余数
在destroyItem()方法中,同样
在onPageSelected()监听方法中,对传递进来的索引进行取于
反向的无限循环
调用VIEwPager对象的setCurrentItem()方法,设置第一次进来时候的当前页,参数:int数字,我们把它定位到Integer.MAX_VALUE的中间,并且保证第一条是当前位置,Integer.MAX_VALUE/2-((Integer.MAX_VALUE/2)%banners.size())
目前有个左划空白页的问题没解决
package com.tsh.myvIEwpager;import java.util.ArrayList; java.util.List; androID.app.Activity; androID.os.Bundle; androID.support.v4.vIEw.PagerAdapter; androID.support.v4.vIEw.VIEwPager; androID.support.v4.vIEw.VIEwPager.OnPagechangelistener; androID.vIEw.VIEw; androID.vIEw.VIEwGroup; androID.Widget.ImageVIEw; androID.Widget.linearLayout; androID.Widget.TextVIEw;public class MainActivity extends Activity { private VIEwPager vp_banner; linearLayout ll_points; TextVIEw tv_Title; private List<VIEw> banners; private String[] Titles=new String[]{ "新闻标题1","新闻标题2" }; private List<ImageVIEw> points=new ArrayList<ImageVIEw>(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // 初始化 vp_banner = (VIEwPager) findVIEwByID(R.ID.vp_banner); ll_points = (linearLayout) findVIEwByID(R.ID.ll_points); tv_Title = (TextVIEw) findVIEwByID(R.ID.tv_Title); banner部分 banners = new ArrayList<>(); ImageVIEw img1 = new ImageVIEw(this); img1.setimageResource(R.drawable.a); banners.add(img1); ImageVIEw img2 = ); img2.setimageResource(R.drawable.b); banners.add(img2); ImageVIEw img3 = ); img3.setimageResource(R.drawable.c); banners.add(img3); 小图标 ll_points=(linearLayout) findVIEwByID(R.ID.ll_points); for(int i=0;i<banners.size();i++){ ImageVIEw image=); image.setimageResource(R.drawable.point_selector); linearLayout.LayoutParams params=new linearLayout.LayoutParams(5,5); params.rightmargin=4; image.setLayoutParams(params); if(i==0){ image.setEnabled(true); }else{ image.setEnabled(false); } points.add(image); ll_points.addVIEw(image); } 设置适配器 vp_banner.setAdapter( MyPagerAdapter()); vp_banner.setCurrentItem(Integer.MAX_VALUE/2-((Integer.MAX_VALUE/2)%banners.size())); vp_banner.setonPagechangelistener( OnPagechangelistener() { @OverrIDe voID onPageSelected(int arg0) { tv_Title.setText(Titles[arg0%banners.size()]); 小图标处理 for(ImageVIEw point:points){ point.setEnabled(); } points.get(arg0%banners.size()).setEnabled(); } @OverrIDe voID onPageScrolled(int arg0,float arg1,1)"> arg2) { Todo auto-generated method stub } @OverrIDe voID onPageScrollStateChanged( arg0) { } }); } private class MyPagerAdapter PagerAdapter { @OverrIDe voID destroyItem(VIEwGroup container,1)"> position,Object object) { System.out.println("销毁:"+position); container.removeVIEw((VIEw)object); object=null; } @OverrIDe public Object instantiateItem(VIEwGroup container,1)"> position) { System.out.println("初始化:"+position); position=position%banners.size(); 解决右划出错 if(banners.get(position).getParent()!=){ container.removeVIEw(banners.get(position)); }{ container.addVIEw(banners.get(position)); } return banners.get(position); } @OverrIDe getCount() { Integer.MAX_VALUE; } @OverrIDe boolean isVIEwFromObject(VIEw arg0,Object arg1) { return arg0 == arg1; } }}
总结
以上是内存溢出为你收集整理的[android] 轮播图-无限循环全部内容,希望文章能够帮你解决[android] 轮播图-无限循环所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)