package com.example.viewpager;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
public class Guide extends Activity implements OnPageChangeListener {
private ViewPager vp;
private ViewPagerAdapter vpa;
private List<View> views;//背景图片
private ImageView[] dots;//选中点
private int[] ids = { R.id.iv1, R.id.iv2, R.id.iv3 };//图片控件显示点的
private int currentItem = 0;//当前显示图片
private ScheduledExecutorService scheduledExecutorService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide);
initView();
initDots();
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
vp.setCurrentItem(currentItem);// 切换当前显示的图片
};
};
private void initView() {
LayoutInflater lInflater = LayoutInflater.from(this);
views = new ArrayList<View>();
views.add(lInflater.inflate(R.layout.one, null));
views.add(lInflater.inflate(R.layout.two, null));
views.add(lInflater.inflate(R.layout.three, null));
vpa = new ViewPagerAdapter(views, this);
vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(vpa);
vp.setOnPageChangeListener(this);
}
private void initDots() {
dots = new ImageView[views.size()];
for (int i = 0; i < views.size(); i++) {
dots[i] = (ImageView) findViewById(ids[i]);
}
}
private class ScrollTask implements Runnable {
public void run() {
synchronized (vp) {
System.out.println("currentItem: " + currentItem);
currentItem = (currentItem + 1) % views.size();
handler.obtainMessage().sendToTarget(); // 通过Handler切换图片
}
}
}
@Override
protected void onStart() {
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
// 当Activity显示出来后,每两秒钟切换一次图片显示
scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 1, 2,
TimeUnit.SECONDS);
super.onStart();
}
@Override
protected void onStop() {
// 当Activity不可见的时候停止切换
scheduledExecutorService.shutdown();
super.onStop();
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
currentItem = arg0;
for (int i = 0; i < ids.length; i++) {
if (arg0 == i) {
dots[i].setImageResource(R.drawable.point);
} else {
dots[i].setImageResource(R.drawable.point_select);
}
}
}
}
viewPager适配器:
package com.example.viewpager;
import java.util.List;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
public class ViewPagerAdapter extends PagerAdapter {
private List<View> views;
private Context context;
public ViewPagerAdapter(List<View> views, Context context) {
super();
this.views = views;
this.context = context;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager)container).removeView(views.get(position));
}
@Override
public Object instantiateItem(View container, int position) {
((ViewPager)container).addView(views.get(position));
return views.get(position);
}
@Override
public int getCount() {
return views.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
}
guide.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000" >
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/point_select" >
</ImageView>
<ImageView
android:id="@+id/iv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/point" >
</ImageView>
<ImageView
android:id="@+id/iv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/point" >
</ImageView>
</LinearLayout>
</RelativeLayout>
one.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/guide_1" />
</LinearLayout>
版权声明:本文为sukkong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。