ViewPager用于实现多页面的切换效果,该类继承于ViewGroup,存在于Google的兼容包里:android-support-v4.jar,使用时记得构建路径,ViewPager使用非常简单,下面用一个简单示例来演示:

Activity:

package com.home.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.home.view.MyView;
import com.home.viewpager.R;

public class ViewPagerTestActivity extends Activity {
// 声明ViewPager对象
private ViewPager viewPager;
// 声明存放在ViewPager里面的View的集合
private List<View> viewList = new ArrayList<View>();
// 声明自定义组件
private MyView myView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 实例化自定义组件
myView = new MyView(this);
// 得到界面上的ViewPager实例
viewPager = (ViewPager) findViewById(R.id.main_viewpager);
// 为viewList集合添加View
viewList.add(getLayoutInflater().inflate(R.layout.firstview, null));
viewList.add(getLayoutInflater().inflate(R.layout.secondview, null));
viewList.add(myView);
PagerAdapter adapter = new PagerAdapter() {
// 用来初始化ViewPager中的view组件
@Override
public Object instantiateItem(View container, int position) {
((ViewGroup) container).addView(viewList.get(position));
return viewList.get(position);
}

// 当页面需要销毁时从ViewPager中去掉
@Override
public void destroyItem(View container, int position, Object object) {
((ViewGroup) container).removeView(viewList.get(position));
}

// ViewPager中存放的页面个数
@Override
public int getCount() {
return viewList.size();
}

// 判断切换的两个界面是否是同一个
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
};
// 为viewPager设置适配器
viewPager.setAdapter(adapter);
}

}


自定义组件类(MyView):

package com.home.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MyView(Context context) {
super(context);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
canvas.drawColor(Color.BLUE);
paint.setColor(Color.RED);
canvas.drawCircle(30, 30, 30, paint);
canvas.drawText("第三个页面", 270, 480, paint);
}
}


Activity的布局XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
android:id="@+id/main_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>

</RelativeLayout>


firstview.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:background="#FF00FF"
android:gravity="center" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个页面" />

</LinearLayout>


secondview.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:background="#00FF00"
android:gravity="center" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个页面" />

</LinearLayout>