public class MainActivity extends Activity {
ArrayList<View> list;
ImageView[] imageViews;
ViewGroup main, group;
ImageView imageView;
ViewPager viewPager;
boolean is = true;
Handler handler;
Message ms;
int i = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = getLayoutInflater();
list = new ArrayList<View>();
list.add(inflater.inflate(R.layout.item1, null));
list.add(inflater.inflate(R.layout.item2, null));
list.add(inflater.inflate(R.layout.item3, null));
imageViews = new ImageView[list.size()];
main = (ViewGroup) inflater.inflate(R.layout.activity_main, null);
group = (ViewGroup) main.findViewById(R.id.viewgroup);
viewPager = (ViewPager) main.findViewById(R.id.viewpager);
for (int i = 0; i < list.size(); i++) {
imageView = new ImageView(MainActivity.this);
imageView.setLayoutParams(new LayoutParams(20, 20));
// imageView.setLayoutParams(new LayoutParams(
// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageView.setPadding(10, 0, 10, 0);
imageViews<I><I> = imageView;
if (i == 0) {
imageViews
.setBackgroundResource(R.drawable.page_indicator_focused);
} else {
imageViews.setBackgroundResource(R.drawable.page_indicator);
}
group.addView(imageView);
}
setContentView(main);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.e("", "" + msg.arg1);
viewPager.setCurrentItem(msg.arg1);
}
};
viewPager.setAdapter(new MyAdapter());
new Thread(new Runnable() {
@Override
public void run() {
while (is) {
try {
ms = handler.obtainMessage();
ms.arg1 = i;
handler.sendMessage(ms);
Thread.sleep(2000);
i++;
if (i > 2) {
i = 0;
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
viewPager.setOnPageChangeListener(new MyListener());
class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
// 判断实View和Object是否为同一个View
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override//这个方法用来得到每个View
public Object instantiateItem(ViewGroup container, int position) {
container.addView(list.get(position));
return list.get(position);
}
@Override//用以控制当某个View不需要的时候的回收处理
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(list.get(position));
}
}
//viewpager滑动监听方法
class MyListener implements OnPageChangeListener {
@Override//状态有三个0空闲,1是增在滑行中,2目标加载完毕
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override//从1到2滑动,在1滑动前调用
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override//当viewpager页面被选择时调用的方法
public void onPageSelected(int position) {
for (int i = 0; i < imageViews.length; i++) {
imageViews[position]
.setBackgroundResource(R.drawable.page_indicator_focused);
if (position != i) {
imageViews
.setBackgroundResource(R.drawable.page_indicator);
}
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
is = false;
}
}
···
···