码迷,mamicode.com
首页 > 其他好文 > 详细

用viewpager实现图片轮播

时间:2015-11-21 15:51:09      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

应用中常常遇到图片轮播的需求,这时候就需要用到viewpager这个组件。viewpager是android support v4 中提供的一个组件。viewpager使用需要以下几步骤:

1.在布局文件中添加viewpager组件

1 <android.support.v4.view.ViewPager
2         android:id="@+id/vp_adv"
3         android:layout_width="match_parent"
4         android:layout_height="match_parent">
5     </android.support.v4.view.ViewPager>

ViewPager路径要写完整,否则会出现问题;

2.创建PagerAdapter,PagerAdapter是viewPager的内容提供者,这里我们的图片就是由PagerAdapter提供给viewpager展示的。同样PagerAdapter也在support v4包中。具体如下所示:

 1 public class AdvertisingAdapter extends PagerAdapter {
 2 
 3     private List<View> mList;
 4 
 5     public AdvertisingAdapter() {
 6         mList = new ArrayList<View>();
 7     }
 8 
 9     public void setData(List<View> list) {
10         if (list != null) {
11             mList.clear();
12             mList.addAll(list);
13             notifyDataSetChanged();
14         }
15     }
16 
17     @Override
18     public int getCount() {
19         return mList.size();
20     }
21 
22     @Override
23     public boolean isViewFromObject(View view, Object object) {
24         return object == view;
25     }
26 
27     @Override
28     public void destroyItem(ViewGroup container, int position, Object object) {
29         container.removeView(mList.get(position));
30     }
31 
32     @Override
33     public Object instantiateItem(ViewGroup container, int position) {
34         container.addView(mList.get(position), 0);
35         return mList.get(position);
36     }
37 }

3.初始化ViewPager,PagerAdapter

1     protected void setupView() {
2         mPagerAdapter = new AdvertisingAdapter();
3         mViewPager = (ViewPager) findViewById(R.id.vp_adv);
4         mViewPager .setAdapter(mPagerAdapter );
5         mAdvertising.addOnPageChangeListener(this);
6     }    

待PagerAdapter填充图片数据后,ViewPager便可以将图片展示出来。

4.添加“.”。基本所有应用使用到ViewPager都会使用“.”标识当前坐标,即展示到哪一页。如下代码添加点号:

添加布局文件

1 <LinearLayout
2         android:id="@+id/viewGroup"
3         android:layout_width="wrap_content"
4         android:layout_height="wrap_content"
5         android:layout_alignParentBottom="true"
6         android:layout_alignParentRight="true"
7         android:layout_marginBottom="20dp"
8         android:layout_marginRight="20dp"
9         android:orientation="horizontal"></LinearLayout>

在布局里面动态添加“点”的view

 1         imageViews = new ImageView[advPics.size()];
 2         ImageView imageView;
 3         for (int i = 0; i < advPics.size(); i++) {
 4             imageView = new ImageView(this);
 5             imageView.setLayoutParams(new LinearLayout.LayoutParams(50, 50));
 6             imageView.setPadding(15, 15, 15, 15);
 7             imageViews[i] = imageView;
 8             if (i == 0) {
 9                 imageViews[i].setImageResource(R.drawable.dot_focus);
10             } else {
11                 imageViews[i].setImageResource(R.drawable.dot_blur);
12             }
13             llDot.addView(imageViews[i]);
14         }

拖动ViewPager时,坐标点动态改变

 1   @Override
 2     public void onPageSelected(int position) {
 3         LogUtils.d("enter onPageSelected method");
 4         
 5         for (int i = 0; i < advPics.size(); i++) {
 6             imageViews[position].setImageResource(R.drawable.dot_focus);
 7             if (position != i) {
 8                 imageViews[i].setImageResource(R.drawable.dot_blur);
 9             }
10         }
11     }

5.自动轮播。大多数时候,我们展示图片时需要其自动轮播的。

启动线程,控制轮播时间,及发出轮播请求给UI线程

 1 private void initAutoThread() {
 2         Runnable runnable = new Runnable() {
 3             @Override
 4             public void run() {
 5                 while (true) {
 6                     if (!isPlayPaused.get()) {
 7                         sendUiMessage(MSG_UI_CHANGE_ADV_PIC, what.get(), 0, null);
 8                         what.incrementAndGet();
 9                         if (what.get() > advPics.size() - 1) {
10                             what.getAndAdd(-advPics.size());
11                         }
12                     }
13                     if (needExit.get()) {
14                         break;
15                     }
16                     try {
17                         Thread.sleep(8000);
18                     } catch (InterruptedException e) {
19                         e.printStackTrace();
20                     }
21                 }
22             }
23         };
24         Thread thread = new Thread(runnable);
25         thread.setName("Carousel");
26         thread.start();
27     }

UI 线程收到请求后,对显示的图片进行更改

1  mViewPager.setCurrentItem(msg.arg1);

 

用viewpager实现图片轮播

标签:

原文地址:http://www.cnblogs.com/pillowzhou/p/4983740.html

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