使用ViewPager這個類可以輕松實現多個頁面的滑動功能,而且相對viewgroup來講要簡單容易一些,在此之前我們需要導入Android-support-v4.jar這個jar包,是google提供的擴展包,位置路徑為android-sdk-windows\extras\android\support\v4\android-support-v4.jar,如果沒有找到,需要打開Android SDK Manager在Extras中進行下載,最後記得引入工程中並build path。
在本示例中,我參考了幾個demo整理出來的viewpager的最基本用法,制作了一個類似新聞導航功能的界面,每個頁面的下方有對應的項目符號,當頁面進行切換的時候項目符號也隨之變化,下面是實現後的圖片:
package com.cogent.viewpager;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;
public class ViewPagerGuideActivity extends Activity {
private ViewPager viewPager;
/**裝分頁顯示的view的數組*/
private ArrayList<View> pageViews;
private ImageView imageView;
/**將小圓點的圖片用數組表示*/
private ImageView[] imageViews;
//包裹滑動圖片的LinearLayout
private ViewGroup viewPics;
//包裹小圓點的LinearLayout
private ViewGroup viewPoints;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//設置無標題欄
//將要分頁顯示的View裝入數組中
LayoutInflater inflater = getLayoutInflater();
pageViews = new ArrayList<View>();
pageViews.add(inflater.inflate(R.layout.page01, null));
pageViews.add(inflater.inflate(R.layout.page02, null));
pageViews.add(inflater.inflate(R.layout.page03, null));
//創建imageviews數組,大小是要顯示的圖片的數量
imageViews = new ImageView[pageViews.size()];
//從指定的XML文件加載視圖
viewPics = (ViewGroup) inflater.inflate(R.layout.view_pics, null);
//實例化小圓點的linearLayout和viewpager
viewPoints = (ViewGroup) viewPics.findViewById(R.id.viewGroup);
viewPager = (ViewPager) viewPics.findViewById(R.id.guidePages);
//添加小圓點的圖片
for(int i=0;i<pageViews.size();i++){
imageView = new ImageView(ViewPagerGuideActivity.this);
//設置小圓點imageview的參數
imageView.setLayoutParams(new LayoutParams(20,20));//創建一個寬高均為20 的布局
imageView.setPadding(20, 0, 20, 0);
//將小圓點layout添加到數組中
imageViews[i] = imageView;
//默認選中的是第一張圖片,此時第一個小圓點是選中狀態,其他不是
if(i==0){
imageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);
}else{
imageViews[i].setBackgroundResource(R.drawable.page_indicator);
}
//將imageviews添加到小圓點視圖組
viewPoints.addView(imageViews[i]);
}
//顯示滑動圖片的視圖
setContentView(viewPics);
//設置viewpager的適配器和監聽事件
viewPager.setAdapter(new GuidePageAdapter());
viewPager.setOnPageChangeListener(new GuidePageChangeListener());
}
class GuidePageAdapter extends PagerAdapter{
//銷毀position位置的界面
@Override
public void destroyItem(View v, int position, Object arg2) {
// TODO Auto-generated method stub
((ViewPager)v).removeView(pageViews.get(position));
}
@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
//獲取當前窗體界面數
@Override
public int getCount() {
// TODO Auto-generated method stub
return pageViews.size();
}
//初始化position位置的界面
@Override
public Object instantiateItem(View v, int position) {
// TODO Auto-generated method stub
((ViewPager) v).addView(pageViews.get(position));
return pageViews.get(position);
}
// 判斷是否由對象生成界面
@Override
public boolean isViewFromObject(View v, Object arg1) {
// TODO Auto-generated method stub
return v == arg1;
}
@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
}
class GuidePageChangeListener implements OnPageChangeListener{
@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) {
// TODO Auto-generated method stub
for(int i=0;i<imageViews.length;i++){
imageViews[position].setBackgroundResource(R.drawable.page_indicator_focused);
//不是當前選中的page,其小圓點設置為未選中的狀態
if(position !=i){
imageViews[i].setBackgroundResource(R.drawable.page_indicator);
}
}
}
}
}
主頁面的布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/page_header"/>
<android.support.v4.view.ViewPager
android:id="@+id/guidePages"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/viewGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
標題欄的布局:
<?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="wrap_content"
android:orientation="horizontal"
android:background="@drawable/title"
android:gravity="center_vertical" >
<Button
android:id="@+id/btn_left"
android:layout_width="80dp"
android:layout_height="30dp"
android:text="返回"
android:textSize="15sp"
android:textColor="@android:color/white"
android:background="@drawable/header_btn"
android:layout_margin="5dp"/>
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="奧運快報"
android:textSize="18sp"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="center_horizontal"/>
<Button
android:id="@+id/btn_right"
android:layout_width="80dp"
android:layout_height="30dp"
android:text="首頁"
android:textSize="15sp"
android:textColor="@android:color/white"
android:background="@drawable/header_btn"
android:layout_margin="5dp"/>
</LinearLayout>
子頁面的布局:
<?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"
android:background="@android:color/white" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/pic_lindan"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="@drawable/divider_horizontal_line">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:textColor="@android:color/black"
android:text="@string/string_lindan"/>
</LinearLayout>
</LinearLayout>
其余子頁面的布局類。
下載該demo源碼
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2012年資料/11月/26日/Android ViewPager 實現多個頁面切換滑動