package com.itheima27.viewpagerdemo;

 import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView; public class MainActivity extends Activity implements OnPageChangeListener { private List<ImageView> imageViewList;
private TextView tvDescription;
private LinearLayout llPointGroup;
private int previousEnabledPosition = 0; // 前一个被选中的"点"的索引, 默认情况下: 0 private String[] imageDescriptionArray = {
"巩俐不低俗,我就不能低俗",
"扑树又回来啦!再唱经典老歌引万人大合唱",
"揭秘北京电影如何升级",
"乐视网TV版大派送",
"热血屌丝的反杀"
};
private ViewPager mViewPager; private boolean isStop = false; // 是否停止子线程, 不会停止 @Override
protected void onDestroy() {
isStop = true;
super.onDestroy();
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); init(); // 开启线程无限自动移动
Thread mThread = new Thread(new Runnable() {
@Override
public void run() {
while(!isStop) {
// 每隔2秒钟, 发送一条消息到主线程, 更新viewpager的界面
SystemClock.sleep(2000); runOnUiThread(new Runnable() {
@Override
public void run() {
// 此方法是在主线程中执行
mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
}
});
}
}
});
mThread.start();
} private void init() {
mViewPager = (ViewPager) findViewById(R.id.viewpager);
llPointGroup = (LinearLayout) findViewById(R.id.ll_point_group);
tvDescription = (TextView) findViewById(R.id.tv_image_description); imageViewList = new ArrayList<ImageView>(); int[] imageResIDs = {
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.e
}; ImageView iv;
View view;
LayoutParams params;
for (int id : imageResIDs) {
iv = new ImageView(this);
iv.setBackgroundResource(id);
imageViewList.add(iv); // 每循环一次, 添加一个点到Linearlayout中
view = new View(this);
view.setBackgroundResource(R.drawable.point_background);
params = new LayoutParams(5, 5);
params.leftMargin = 5;
view.setEnabled(false);
view.setLayoutParams(params); llPointGroup.addView(view); // 向线性布局中添加"点"
} mViewPager.setAdapter(new MyAdapter());
mViewPager.setOnPageChangeListener(this); // 初始化图片描述, 和哪一个点被选中
// tvDescription.setText(imageDescriptionArray[0]);
// llPointGroup.getChildAt(0).setEnabled(true); // 初始化ViewPager的默认position为Integer.Max_value的一半
int index = (Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2 % imageViewList.size());
mViewPager.setCurrentItem(index); // 设置当前viewpager选中的pager页, 会触发OnPageChangeListener.onPagerSelected } class MyAdapter extends PagerAdapter { @Override
public int getCount() {
return Integer.MAX_VALUE;
} /**
* 复用对象.
* true 复用对象
* false 用的是object
*/
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
} /**
* 销毁对象
* @param position 将要被销毁对象的索引位置
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(imageViewList.get(position % imageViewList.size()));
} /**
* 初始化一个view对象
* @param position 将要被创建的对象的索引位置
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
// 先把对象添加到viewpager中, 再返回当前对象
container.addView(imageViewList.get(position % imageViewList.size()));
return imageViewList.get(position % imageViewList.size());
}
} @Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub } @Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub } @Override
public void onPageSelected(int position) { // 取余后的索引
int newPosition = position % imageViewList.size(); // 根据索引设置图片的描述
tvDescription.setText(imageDescriptionArray[newPosition]); // 把上一个点设置为未选中
llPointGroup.getChildAt(previousEnabledPosition).setEnabled(false); // 根据索引设置哪一个点被选中
llPointGroup.getChildAt(newPosition).setEnabled(true); previousEnabledPosition = newPosition;
}
}
05-11 09:42