码迷,mamicode.com
首页 > 移动开发 > 详细

Android利用ViewPager实现滑动广告板

时间:2014-06-26 18:36:58      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   class   blog   code   

•android-support-v4.jar,这是谷歌官方 给我们提供的一个兼容低版本Android设备的软件包,里面包囊了只有在Android3.0以上可以使用的api。而ViewPager就是其中之 一,利用它我们可以做很多事情,从最简单的导航,到页面切换菜单等等。

•ViewPager的功能就是可以使视图滑动,就像Lanucher左右滑动那样。
•本Demo向大家演示ViewPager的使用,并在用户未滑动View时,每隔5s钟自动切换到下一个View(循环切换),而当用户有Touch到View时取消5s自动切换功能。并有指示器指示当前处于哪一个View。
 
下面是运行的截图:
 
bubuko.com,布布扣
这个是非常常见的应用
下面给出实现的代码:
1.源文件
  1 ackage com.eyeandroid.viewpager;  
  2   
  3 import java.util.ArrayList;  
  4 import java.util.List;  
  5 import java.util.concurrent.atomic.AtomicInteger;  
  6   
  7 import android.annotation.SuppressLint;  
  8 import android.app.Activity;  
  9 import android.os.Bundle;  
 10 import android.os.Handler;  
 11 import android.os.Message;  
 12 import android.os.Parcelable;  
 13 import android.support.v4.view.PagerAdapter;  
 14 import android.support.v4.view.ViewPager;  
 15 import android.support.v4.view.ViewPager.OnPageChangeListener;  
 16 import android.view.MotionEvent;  
 17 import android.view.View;  
 18 import android.view.View.OnTouchListener;  
 19 import android.view.ViewGroup;  
 20 import android.view.ViewGroup.LayoutParams;  
 21 import android.widget.ImageView;  
 22   
 23 @SuppressLint("HandlerLeak")  
 24 public class MainActivity extends Activity {  
 25     private ImageView[] imageViews = null;  
 26     private ImageView imageView = null;  
 27     private ViewPager advPager = null;  
 28     private AtomicInteger what = new AtomicInteger(0);  
 29     private boolean isContinue = true;  
 30   
 31     @Override  
 32     public void onCreate(Bundle savedInstanceState) {  
 33         super.onCreate(savedInstanceState);  
 34         setContentView(R.layout.main);  
 35         initViewPager();  
 36     }  
 37   
 38   
 39     private void initViewPager() {  
 40         advPager = (ViewPager) findViewById(R.id.adv_pager);  
 41         ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup);  
 42   
 43 //      这里存放的是四张广告背景  
 44         List<View> advPics = new ArrayList<View>();  
 45   
 46         ImageView img1 = new ImageView(this);  
 47         img1.setBackgroundResource(R.drawable.advertising_default_1);  
 48         advPics.add(img1);  
 49   
 50         ImageView img2 = new ImageView(this);  
 51         img2.setBackgroundResource(R.drawable.advertising_default_2);  
 52         advPics.add(img2);  
 53   
 54         ImageView img3 = new ImageView(this);  
 55         img3.setBackgroundResource(R.drawable.advertising_default_3);  
 56         advPics.add(img3);  
 57   
 58         ImageView img4 = new ImageView(this);  
 59         img4.setBackgroundResource(R.drawable.advertising_default);  
 60         advPics.add(img4);  
 61           
 62 //      对imageviews进行填充  
 63         imageViews = new ImageView[advPics.size()];  
 64 //小图标  
 65         for (int i = 0; i < advPics.size(); i++) {  
 66             imageView = new ImageView(this);  
 67             imageView.setLayoutParams(new LayoutParams(20, 20));  
 68             imageView.setPadding(5, 5, 5, 5);  
 69             imageViews[i] = imageView;  
 70             if (i == 0) {  
 71                 imageViews[i]  
 72                         .setBackgroundResource(R.drawable.banner_dian_focus);  
 73             } else {  
 74                 imageViews[i]  
 75                         .setBackgroundResource(R.drawable.banner_dian_blur);  
 76             }  
 77             group.addView(imageViews[i]);  
 78         }  
 79   
 80         advPager.setAdapter(new AdvAdapter(advPics));  
 81         advPager.setOnPageChangeListener(new GuidePageChangeListener());  
 82         advPager.setOnTouchListener(new OnTouchListener() {  
 83               
 84             @Override  
 85             public boolean onTouch(View v, MotionEvent event) {  
 86                 switch (event.getAction()) {  
 87                 case MotionEvent.ACTION_DOWN:  
 88                 case MotionEvent.ACTION_MOVE:  
 89                     isContinue = false;  
 90                     break;  
 91                 case MotionEvent.ACTION_UP:  
 92                     isContinue = true;  
 93                     break;  
 94                 default:  
 95                     isContinue = true;  
 96                     break;  
 97                 }  
 98                 return false;  
 99             }  
100         });  
101         new Thread(new Runnable() {  
102   
103             @Override  
104             public void run() {  
105                 while (true) {  
106                     if (isContinue) {  
107                         viewHandler.sendEmptyMessage(what.get());  
108                         whatOption();  
109                     }  
110                 }  
111             }  
112   
113         }).start();  
114     }  
115   
116       
117     private void whatOption() {  
118         what.incrementAndGet();  
119         if (what.get() > imageViews.length - 1) {  
120             what.getAndAdd(-4);  
121         }  
122         try {  
123             Thread.sleep(5000);  
124         } catch (InterruptedException e) {  
125               
126         }  
127     }  
128   
129     private final Handler viewHandler = new Handler() {  
130   
131         @Override  
132         public void handleMessage(Message msg) {  
133             advPager.setCurrentItem(msg.what);  
134             super.handleMessage(msg);  
135         }  
136   
137     };  
138   
139     private final class GuidePageChangeListener implements OnPageChangeListener {  
140   
141         @Override  
142         public void onPageScrollStateChanged(int arg0) {  
143   
144         }  
145   
146         @Override  
147         public void onPageScrolled(int arg0, float arg1, int arg2) {  
148   
149         }  
150   
151         @Override  
152         public void onPageSelected(int arg0) {  
153             what.getAndSet(arg0);  
154             for (int i = 0; i < imageViews.length; i++) {  
155                 imageViews[arg0]  
156                         .setBackgroundResource(R.drawable.banner_dian_focus);  
157                 if (arg0 != i) {  
158                     imageViews[i]  
159                             .setBackgroundResource(R.drawable.banner_dian_blur);  
160                 }  
161             }  
162   
163         }  
164   
165     }  
166   
167     private final class AdvAdapter extends PagerAdapter {  
168         private List<View> views = null;  
169   
170         public AdvAdapter(List<View> views) {  
171             this.views = views;  
172         }  
173   
174         @Override  
175         public void destroyItem(View arg0, int arg1, Object arg2) {  
176             ((ViewPager) arg0).removeView(views.get(arg1));  
177         }  
178   
179         @Override  
180         public void finishUpdate(View arg0) {  
181   
182         }  
183   
184         @Override  
185         public int getCount() {  
186             return views.size();  
187         }  
188   
189         @Override  
190         public Object instantiateItem(View arg0, int arg1) {  
191             ((ViewPager) arg0).addView(views.get(arg1), 0);  
192             return views.get(arg1);  
193         }  
194   
195         @Override  
196         public boolean isViewFromObject(View arg0, Object arg1) {  
197             return arg0 == arg1;  
198         }  
199   
200         @Override  
201         public void restoreState(Parcelable arg0, ClassLoader arg1) {  
202   
203         }  
204   
205         @Override  
206         public Parcelable saveState() {  
207             return null;  
208         }  
209   
210         @Override  
211         public void startUpdate(View arg0) {  
212   
213         }  
214   
215     }  
216   
217 }  
2.布局文件
这个文件在屏幕上的图片显示具有至关重要的作用
 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 3     android:layout_width="fill_parent"  
 4     android:layout_height="fill_parent"  
 5     android:orientation="vertical" >  
 6   
 7     <RelativeLayout  
 8         android:layout_width="wrap_content"  
 9         android:layout_height="wrap_content"  
10         android:orientation="vertical" >  
11         <!-- ViewPager是一个控件 -->  
12         <android.support.v4.view.ViewPager  
13             android:id="@+id/adv_pager"  
14             android:layout_width="fill_parent"  
15             android:layout_height="160dp" >  
16               
17         </android.support.v4.view.ViewPager>  
18         <LinearLayout  
19             android:id="@+id/viewGroup"  
20             android:layout_below="@id/adv_pager"  
21             android:layout_width="fill_parent"  
22             android:layout_height="wrap_content"  
23             android:layout_marginTop="-25px"  
24             android:gravity="right"  
25             android:orientation="horizontal" >  
26         </LinearLayout>  
27     </RelativeLayout>  
28   
29 </LinearLayout>  

原博客地址:http://blog.csdn.net/dlutbrucezhang/article/details/8736807

Android利用ViewPager实现滑动广告板,布布扣,bubuko.com

Android利用ViewPager实现滑动广告板

标签:des   android   style   class   blog   code   

原文地址:http://www.cnblogs.com/ycxyyzw/p/3808573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!