你的位置:首页 > 操作系统

[操作系统]android实现无限轮播


1 在ViewPager的适配器中的getCount()长度设置无限大Integer.MAX_VALUE

2  明白当前currentIten 为position % images.length;

3  设置一开始ViewPager的位置 viewPager.setCurrentItem((images.length) * 1000);这样刚开始就可以向左滑动了

4  在OnPageChangeListener ViewPager的滑动监听中设置小点的位置

5  设置Handler 定时滑动开启滑动

6 在onResume中发送空消息,在onPause中移除消息将所有的Callbacks和Messages全部清除掉,避免内存泄露。

如下是主代码 图片是本地的

 1 package com.demo.sb.main; 2  3 import java.util.ArrayList; 4 import com.demo.sb.utils.DensityUtil; 5 import com.demo.suibian.R; 6 import android.annotation.SuppressLint; 7 import android.app.Activity; 8 import android.os.Bundle; 9 import android.os.Handler; 10 import android.support.v4.view.PagerAdapter; 11 import android.support.v4.view.ViewPager; 12 import android.support.v4.view.ViewPager.OnPageChangeListener; 13 import android.view.MotionEvent; 14 import android.view.View; 15 import android.view.View.OnClickListener; 16 import android.view.View.OnTouchListener; 17 import android.view.ViewGroup; 18 import android.widget.ImageView; 19 import android.widget.LinearLayout; 20  21 public class MActivity_Luenbo extends Activity { 22  23   private ViewPager viewPager; 24   private LinearLayout group; 25   private int[] images = { R.drawable.a, R.drawable.c, R.drawable.d, 26       R.drawable.guide_1, R.drawable.guide_2 }; 27   private ArrayList<ImageView> mImageViewList; 28   private ImageView[] diandian;// ViewPager上的点点 29   private int selectedItem = 0; 30  31   @Override 32   protected void onCreate(Bundle savedInstanceState) { 33     // TODO Auto-generated method stub 34     super.onCreate(savedInstanceState); 35     setContentView(R.layout.mactivity_luenbo); 36     viewPager = (ViewPager) findViewById(R.id.vp_viewPager); 37     group = (LinearLayout) findViewById(R.id.ll_viewGroup); 38     init(); 39  40   } 41  42   @SuppressLint("HandlerLeak") 43   private Handler mHandler = new Handler() { 44     public void handleMessage(android.os.Message msg) { 45       viewPager.setCurrentItem(selectedItem + 1); 46       mHandler.sendEmptyMessageDelayed(0, 3000); 47     }; 48   }; 49  50   private void init() { 51     // TODO Auto-generated method stub 52     // 引导3个ViewPager 53     mImageViewList = new ArrayList<ImageView>(); 54     for (int i = 0; i < images.length; i++) { 55       ImageView imageView = new ImageView(this); 56       imageView.setBackgroundResource(images[i]); 57       mImageViewList.add(imageView); 58     } 59  60     // 将点点加入到ViewGroup中 61     diandian = new ImageView[images.length]; 62     for (int i = 0; i < images.length; i++) { 63       // 循环加入点点图片组 64       diandian[i] = new ImageView(this); 65       if (i == 0) { 66         diandian[i].setImageResource(R.drawable.page_indicator_focused); 67       } else { 68         diandian[i] 69             .setImageResource(R.drawable.page_indicator_unfocused); 70       } 71       diandian[i].setPadding(8, 8, 8, 8); 72       group.addView(diandian[i]); 73     } 74     viewPager.setAdapter(new GuideAdapter()); 75     // 设置监听,主要是设置点点的背景 76     viewPager.setOnPageChangeListener(new GuidePagerListener()); 77     // 设置ViewPager的默认项,设置为长度的1000倍,这样开始就能向左滑动了 78     viewPager.setCurrentItem((images.length) * 1000); 79   } 80  81   /** 82    * 每次当onResume有焦点时发送个空消息开始轮播 83   */ 84   @Override 85   protected void onResume() { 86     // TODO Auto-generated method stub 87     super.onResume(); 88     mHandler.sendEmptyMessageDelayed(0, 2000); 89   } 90  91   /** 92    * 当暂停时停止轮播 93   */ 94   @Override 95   protected void onPause() { 96     // TODO Auto-generated method stub 97     super.onPause(); 98     mHandler.removeCallbacksAndMessages(null); 99   }100 101   /**102    * ViewPager的适配器103    * 104   */105   private class GuideAdapter extends PagerAdapter {106 107     @Override108     public int getCount() {109       // TODO Auto-generated method stub110       return Integer.MAX_VALUE;111     }112 113     @Override114     public boolean isViewFromObject(View arg0, Object arg1) {115       // TODO Auto-generated method stub116       return arg0 == arg1;117     }118 119     // @Override120     // public void destroyItem(View container, int position, Object object)121     // {122     // // TODO Auto-generated method stub123     // ((ViewGroup) container).removeView(images[position124     // % images.length]);125     // }126     @Override127     public void destroyItem(ViewGroup container, int position, Object object) {128       // TODO Auto-generated method stub129       container.removeView((View) object);130     }131 132     /**133      * 加载图片进去,用当前的position除以图片数组长度取余数是关键134     */135     @Override136     public Object instantiateItem(ViewGroup container, int position) {137       // TODO Auto-generated method stub138       final int currentIten = position % images.length;139       ImageView iView = mImageViewList.get(currentIten);140       try {141 142         if (iView.getParent() == null) {143           container.addView(iView);144         }145         iView.setOnClickListener(new OnClickListener() {146 147           @Override148           public void onClick(View arg0) {149             // TODO Auto-generated method stub150             DensityUtil.showToast(MActivity_Luenbo.this,151                 currentIten + "");152           }153         });154         iView.setOnTouchListener(new NewTouchListener());155       } catch (Exception e) {156         // TODO: handle exception157       }158 159       return iView;160     }161 162   }163 164   /**165    * ViewPager 的滑动监听166    * 167    * @author Administrator168    * 169   */170   private class GuidePagerListener implements OnPageChangeListener {171 172     @Override173     public void onPageScrollStateChanged(int arg0) {174       // TODO Auto-generated method stub175 176     }177 178     @Override179     public void onPageScrolled(int arg0, float arg1, int arg2) {180       // TODO Auto-generated method stub181       System.out.println(arg0 + " dd " + arg1 + " dddd " + arg2);182     }183 184     @Override185     public void onPageSelected(int position) {186       // TODO Auto-generated method stub187       selectedItem = position;188       System.out.println("第几个页面" + position + " " + position189           % mImageViewList.size());190       for (int i = 0; i < diandian.length; i++) {191         if (i == position % images.length) {192           diandian[i]193               .setImageResource(R.drawable.page_indicator_focused);194         } else {195           diandian[i]196               .setImageResource(R.drawable.page_indicator_unfocused);197         }198       }199     }200 201   }202 203   /**204    * ViewPager的触摸事件当按下图片停止轮播205    * 206    * @author Administrator207    * 208   */209   private class NewTouchListener implements OnTouchListener {210 211     @Override212     public boolean onTouch(View arg0, MotionEvent arg1) {213       // TODO Auto-generated method stub214       switch (arg1.getAction()) {215       case MotionEvent.ACTION_DOWN:216         mHandler.removeCallbacksAndMessages(null);217         break;218       case MotionEvent.ACTION_CANCEL:219         mHandler.sendEmptyMessageDelayed(0, 3000);220         break;221       case MotionEvent.ACTION_UP:222         mHandler.sendEmptyMessageDelayed(0, 3000);223         break;224       default:225         break;226       }227       return true;228     }229 230   }231 232 }

 1 <? 2 <LinearLayout  3   android:layout_width="match_parent" 4   android:layout_height="match_parent" 5   android:orientation="vertical" > 6  7   <TextView 8     android:layout_width="match_parent" 9     android:layout_height="wrap_content"10     android:padding="10dp"11     android:text="@string/luenbo"12     android:textColor="#f00" />13 14   <FrameLayout15     android:layout_width="match_parent"16     android:layout_height="200dp" >17 18     <android.support.v4.view.ViewPager19       android:id="@+id/vp_viewPager"20       android:layout_width="match_parent"21       android:layout_height="match_parent" />22 23     <RelativeLayout24       android:layout_width="wrap_content"25       android:layout_height="wrap_content"26       android:layout_gravity="bottom|center"27       android:layout_marginBottom="20dp" >28 29       <LinearLayout30         android:id="@+id/ll_viewGroup"31         android:layout_width="wrap_content"32         android:layout_height="wrap_content"33         android:orientation="horizontal" />34     </RelativeLayout>35 36   </FrameLayout>37 38 </LinearLayout>