1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 tools:context=".MainActivity" >
6
7 <android.support.v4.view.ViewPager
8 android:id="@+id/viewpager"
9 android:layout_width="match_parent"
10 android:layout_height="200dp" />
11
12 <LinearLayout
13 android:layout_width="match_parent"
14 android:layout_height="wrap_content"
15 android:layout_alignBottom="@id/viewpager"
16 android:background="#33000000"
17 android:orientation="vertical" >
18
19 <!-- 滑动图片下面的文字 -->
20 <TextView
21 android:id="@+id/image_desc"
22 android:layout_width="match_parent"
23 android:layout_height="wrap_content"
24 android:gravity="center"
25 android:text="@string/app_name"
26 android:textColor="@android:color/white"
27 android:textSize="18sp" />
28
29 <!-- ..... -->
30 <LinearLayout
31 android:id="@+id/point_group"
32 android:orientation="horizontal"
33 android:layout_width="wrap_content"
34 android:layout_gravity="center_horizontal"
35 android:layout_height="wrap_content" >
36 </LinearLayout>
37
38 </LinearLayout>
39
40 </RelativeLayout>
1 package com.example.testdemo;
2
3 import java.util.ArrayList;
4 import android.app.Activity;
5 import android.os.Bundle;
6 import android.os.Handler;
7 import android.support.v4.view.PagerAdapter;
8 import android.support.v4.view.ViewPager;
9 import android.support.v4.view.ViewPager.OnPageChangeListener;
10 import android.view.View;
11 import android.view.ViewGroup;
12 import android.widget.ImageView;
13 import android.widget.LinearLayout;
14 import android.widget.TextView;
15
16 public class MainActivity extends Activity {
17
18 private ViewPager viewPager;
19 private LinearLayout pointGroup;
20 private TextView iamgeDesc;
21 private ArrayList<ImageView> imageList;
22 // 上一个页面的位置
23 protected int lastPosition;
24 // 判断是否自动滚动
25 private boolean isRunning = false;
26
27 // 图片资源ID
28 private final int[] imageIds = { R.drawable.a, R.drawable.b, R.drawable.c,
29 R.drawable.d, R.drawable.e };
30
31 // 图片标题集合
32 private final String[] imageDescriptions = { "巩俐不低俗,我就不能低俗",
33 "扑树又回来啦!再唱经典老歌引万人大合唱", "揭秘北京电影如何升级", "乐视网TV版大派送", "热血屌丝的反杀" };
34
35 @Override
36 protected void onCreate(Bundle savedInstanceState) {
37 super.onCreate(savedInstanceState);
38 setContentView(R.layout.activity_main);
39
40 viewPager = (ViewPager) findViewById(R.id.viewpager);
41 pointGroup = (LinearLayout) findViewById(R.id.point_group);
42 iamgeDesc = (TextView) findViewById(R.id.image_desc);
43 iamgeDesc.setText(imageDescriptions[0]);
44
45 imageList = new ArrayList<ImageView>();
46 for (int i = 0; i < imageIds.length; i++) {
47
48 // 初始化图片资源
49 ImageView image = new ImageView(this);
50 image.setBackgroundResource(imageIds[i]);
51 imageList.add(image);
52
53 // 添加指示点
54 ImageView point = new ImageView(this);
55 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
56 LinearLayout.LayoutParams.WRAP_CONTENT,
57 LinearLayout.LayoutParams.WRAP_CONTENT);
58
59 params.rightMargin = 20;
60 point.setLayoutParams(params);
61
62 point.setBackgroundResource(R.drawable.point_bg);
63 if (i == 0) {
64 point.setEnabled(true);
65 } else {
66 point.setEnabled(false);
67 }
68 pointGroup.addView(point);
69 }
70
71 viewPager.setAdapter(new MyPagerAdapter());
72
73 // viewPager.setCurrentItem(Integer.MAX_VALUE/2 -
74 // (Integer.MAX_VALUE/2%imageList.size())) ;
75
76 viewPager.setOnPageChangeListener(new OnPageChangeListener() {
77
78 @Override
79 /**
80 * 页面切换后调用
81 * position 新的页面位置
82 */
83 public void onPageSelected(int position) {
84
85 position = position % imageList.size();
86
87 // 设置文字描述内容
88 iamgeDesc.setText(imageDescriptions[position]);
89
90 // 改变指示点的状态
91 // 把当前点enbale 为true
92 pointGroup.getChildAt(position).setEnabled(true);
93 // 把上一个点设为false
94 pointGroup.getChildAt(lastPosition).setEnabled(false);
95 lastPosition = position;
96
97 }
98
99 @Override
100 /**
101 * 页面正在滑动的时候,回调
102 */
103 public void onPageScrolled(int position, float positionOffset,
104 int positionOffsetPixels) {
105 }
106
107 @Override
108 /**
109 * 当页面状态发生变化的时候,回调
110 */
111 public void onPageScrollStateChanged(int state) {
112
113 }
114 });
115
116 /*
117 * 自动循环: 1、定时器:Timer 2、开子线程 while true 循环 3、ColckManager 4、 用handler
118 * 发送延时信息,实现循环
119 */
120 isRunning = true;
121 // handler.sendEmptyMessageDelayed(0, 2000);
122 }
123
124 private Handler handler = new Handler() {
125 public void handleMessage(android.os.Message msg) {
126 // 让viewPager 滑动到下一页
127 viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
128 if (isRunning) {
129 handler.sendEmptyMessageDelayed(0, 2000);
130 }
131 };
132 };
133
134 protected void onDestroy() {
135 isRunning = false;
136 };
137
138 private class MyPagerAdapter extends PagerAdapter {
139
140 @Override
141 // 获得页面的总数
142 public int getCount() {
143 return Integer.MAX_VALUE;
144 }
145
146 @Override
147 /**
148 * 获得相应位置上的view
149 * container view的容器,其实就是viewpager自身
150 * position 相应的位置
151 */
152 public Object instantiateItem(ViewGroup container, int position) {
153 System.out.println("instantiateItem ::" + position);
154 // 给 container 添加一个view
155 container.addView(imageList.get(position % imageList.size()));
156 // 返回一个和该view相对的object
157 return imageList.get(position % imageList.size());
158 }
159
160 @Override // 判断 view和object的对应关系
161 public boolean isViewFromObject(View view, Object object) {
162 if (view == object) {
163 return true;
164 } else {
165 return false;
166 }
167 }
168
169 @Override // 销毁对应位置上的object
170 public void destroyItem(ViewGroup container, int position, Object object) {
171 System.out.println("destroyItem ::" + position);
172 container.removeView((View) object);
173 object = null;
174 }
175 }
176
177 }