android实现无限轮播

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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         @Override
108         public int getCount() {
109             // TODO Auto-generated method stub
110             return Integer.MAX_VALUE;
111         }
112 
113         @Override
114         public boolean isViewFromObject(View arg0, Object arg1) {
115             // TODO Auto-generated method stub
116             return arg0 == arg1;
117         }
118 
119         // @Override
120         // public void destroyItem(View container, int position, Object object)
121         // {
122         // // TODO Auto-generated method stub
123         // ((ViewGroup) container).removeView(images[position
124         // % images.length]);
125         // }
126         @Override
127         public void destroyItem(ViewGroup container, int position, Object object) {
128             // TODO Auto-generated method stub
129             container.removeView((View) object);
130         }
131 
132         /**
133          * 加载图片进去,用当前的position除以图片数组长度取余数是关键
134          */
135         @Override
136         public Object instantiateItem(ViewGroup container, int position) {
137             // TODO Auto-generated method stub
138             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                     @Override
148                     public void onClick(View arg0) {
149                         // TODO Auto-generated method stub
150                         DensityUtil.showToast(MActivity_Luenbo.this,
151                                 currentIten + "");
152                     }
153                 });
154                 iView.setOnTouchListener(new NewTouchListener());
155             } catch (Exception e) {
156                 // TODO: handle exception
157             }
158 
159             return iView;
160         }
161 
162     }
163 
164     /**
165      * ViewPager 的滑动监听
166      * 
167      * @author Administrator
168      * 
169      */
170     private class GuidePagerListener implements OnPageChangeListener {
171 
172         @Override
173         public void onPageScrollStateChanged(int arg0) {
174             // TODO Auto-generated method stub
175 
176         }
177 
178         @Override
179         public void onPageScrolled(int arg0, float arg1, int arg2) {
180             // TODO Auto-generated method stub
181             System.out.println(arg0 + "  dd " + arg1 + "  dddd  " + arg2);
182         }
183 
184         @Override
185         public void onPageSelected(int position) {
186             // TODO Auto-generated method stub
187             selectedItem = position;
188             System.out.println("第几个页面" + position + "  " + position
189                     % 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 Administrator
207      * 
208      */
209     private class NewTouchListener implements OnTouchListener {
210 
211         @Override
212         public boolean onTouch(View arg0, MotionEvent arg1) {
213             // TODO Auto-generated method stub
214             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 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 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     <FrameLayout
15         android:layout_width="match_parent"
16         android:layout_height="200dp" >
17 
18         <android.support.v4.view.ViewPager
19             android:id="@+id/vp_viewPager"
20             android:layout_width="match_parent"
21             android:layout_height="match_parent" />
22 
23         <RelativeLayout
24             android:layout_width="wrap_content"
25             android:layout_height="wrap_content"
26             android:layout_gravity="bottom|center"
27             android:layout_marginBottom="20dp" >
28 
29             <LinearLayout
30                 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>

 

以上是关于android实现无限轮播的主要内容,如果未能解决你的问题,请参考以下文章

android-自定义广告轮播Banner(无限循环实现)

Android之仿京东淘宝的自动无限轮播控件

android 图片轮播(banner)无限轮播

Android无限广告轮播 - 自定义BannerView

Android使用Path实现仿最新淘宝轮播广告底部弧形有锯齿的问题以及解决办法

Android ViewPager+轮播图