android开发实例-ViewPager

之前看到的一些切换页面采用的方法是重写onScroll()方法,人工获取手势操作,比如滑动的方向速度等等以此来判断页面切换的方向。这个例子主要是采用ViewPager的方法在同一个activity里切换页面。

1.主界面的布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus"
    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:background="#FFFFFF" >

        <TextView
            android:id="@+id/text1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="编辑"
            android:textColor="#000000"
            android:textSize="22.0dip" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="内容"
            android:textColor="#000000"
            android:textSize="22.0dip" />


       
    </LinearLayout>

    <ImageView
        android:id="@+id/cursor"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/a" />

    <android.support.v4.view.ViewPager
        android:id="@+id/vPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:background="#000000"
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />

</LinearLayout>
View Code

 


2.ListActivity.java

package com.demo;

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

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
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.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Tab页面手势滑动切换以及动画效果
 * 
 * @author D.Winter
 * 
 */
public class ListActivity extends Activity {
	// ViewPager是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。
	// android-support-v4.jar
	private ViewPager mPager;//页卡内容
	private List<View> listViews; // Tab页面列表
	private ImageView cursor;// 动画图片
	private TextView t1, t2;// 页卡头标
	private int offset = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号
	private int bmpW;// 动画图片宽度
private EditText title;
private Button save;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		InitImageView();	/* 初始化动画*/
		InitTextView(); // 初始化头标
		InitViewPager();

	}

	/**
	 * 初始化头标
	 */
	private void InitTextView() {
		t1 = (TextView) findViewById(R.id.text1);
		t1.setText("新建");
		t2 = (TextView) findViewById(R.id.text2);
		t2.setText("列表");

		t1.setOnClickListener(new MyOnClickListener(0));
		t2.setOnClickListener(new MyOnClickListener(1));
		
	}
	
	/**
	 * 初始化ViewPager
	 */
	private void InitViewPager() {
		mPager = (ViewPager) findViewById(R.id.vPager);
		listViews = new ArrayList<View>();
		LayoutInflater mInflater = getLayoutInflater();
		listViews.add(mInflater.inflate(R.layout.lay1, null));
		listViews.add(mInflater.inflate(R.layout.lay2, null));
		//listViews.add(mInflater.inflate(R.layout.lay3, null));
		mPager.setAdapter(new MyPagerAdapter(listViews));
		mPager.setCurrentItem(0);//初始页面为第一个页面
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());//添加页面滑动切换动画监听事件
	}

	/**
	 * 初始化动画
	 * 标题栏下面的小光标的切换动画
	 *要添加MyOnPageChangeListener事件,小光标才能随之切换
	 */
	private void InitImageView() {
		cursor = (ImageView) findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)
				.getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		offset = (screenW / 2 - bmpW) / 2;// 计算偏移量
		Log.i("wxpDeb", "屏幕宽度:"+screenW+"图片宽度:"+String.valueOf(bmpW)+"  偏移量:"+String.valueOf(offset));
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置
	}

	
	
	/**
	 * 头标点击监听
	 */
	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;
//这是为标题栏添加点击事件,设置当前页面为index
		//相当于点击切换页面
		//注意和下面的MyOnPageChangeListener比较
		public MyOnClickListener(int i) {
			index = i;
		}

	
		public void onClick(View v) {
		
			mPager.setCurrentItem(index);
			

		}
	};
	

	/**
	 * 页卡切换监听
	 */
	public class MyOnPageChangeListener implements OnPageChangeListener {
/*这尼玛实际上就是为了给小光标添加动画啊,如果viewpager不添加这个事件照样可以切换但是光标却不滑动,
		也就是不能    显示    出当前到底是那个页面,或许可以换别的方法提示当前页面*/
		int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量,bmpw为图片宽度,offset为偏移量
		int two = one * 2;// 页卡1 -> 页卡2 偏移量
  
	
		public void onPageSelected(int arg0) {
			Log.w("wxpDeb","one:"+one+";two:"+two+";offset:"+offset);
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(one, 0, 0, 0);
//public TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 
					//来看看这个构造方法 第一个参数是x的起始位置,第二个参数是x的终止位置
					//假设selected为第一个页面,如果当前页面为一,表示从one的
				} 
//				else if (currIndex == 2) {
//					animation = new TranslateAnimation(two, 0, 0, 0);
//
//				}//如果有三个页面时,这段话就要填上去
				Log.w("wxpDeb","arg0为0时,"+"currIndex:"+currIndex);
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, one, 0, 0);
				}
//				else if (currIndex == 2) {
//					animation = new TranslateAnimation(two, one, 0, 0);
//				}//同上
				Log.w("wxpDeb","arg0为1时,"+"currIndex:"+currIndex);
				break;
				
			}
			currIndex = arg0;//意味选择哪个,即设置当前为哪个
			Log.w("wxpDeb","currIndex:"+currIndex);
			animation.setFillAfter(true);// True:图片停在动画结束位置
			animation.setDuration(300);//这个duration时间是指光标滑动的持续时间
		
			cursor.startAnimation(animation);
			//此处表名小光标的切换动画并不是和页面切换绑定,
			//(如果不添加MyOnPageChangeListener,点击标题栏也可切换页面,但是小光标并不移动)
			//而是和滑动切换事件绑定
		}

	
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageScrollStateChanged(int arg0) {
		}
	}
	/**
	 * ViewPager适配器
	 */
	public class MyPagerAdapter extends PagerAdapter {
		public List<View> mListViews;

		public MyPagerAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mListViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(mListViews.get(arg1), 0);
			return mListViews.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	

}

 

3.可以在各个页面里面添加新的布局文件
  这里第一个页面是lay1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#158684" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
        android:background="#EEEEEE">

    

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">

    <Button
        android:id="@+id/save"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#3399FF"
        android:text="保存" />

    </LinearLayout>


        <EditText
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
               android:text="new"
           >

        <requestFocus />
    </EditText>
    

    <EditText
        
        android:id="@+id/editcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ems="10"
         android:text=""
            android:gravity="top"  />
   
</LinearLayout>
</LinearLayout>
View Code

第二个页面是lay2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#1586FF" >
    
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
        android:background="#EEEEEE"
        >
    

    <Button
        android:id="@+id/json"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#3399FF"
        android:text="新建笔记" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:cacheColorHint="#00000000" >
    </ListView>
</LinearLayout>
</LinearLayout>
View Code

忘了。附图:

貌似注释写的有点渣渣嘛。。。睡觉去了。明天安心看书。

posted @ 2013-07-19 00:19  wisimer  阅读(279)  评论(0编辑  收藏  举报