package com.example.testviewpagerandtabhost;


import java.util.ArrayList;

import java.util.List;



import android.os.Bundle;

import android.os.Parcelable;

import android.app.Activity;

import android.app.LocalActivityManager;

import android.content.Context;

import android.content.Intent;

import android.graphics.BitmapFactory;

import android.graphics.Matrix;

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.view.Menu;

import android.view.View;

import android.view.ViewGroup;

import android.view.animation.Animation;

import android.view.animation.TranslateAnimation;

import android.widget.ImageView;

import android.widget.TabHost;

import android.widget.TextView;


public class MainActivity extends Activity {


Context context = null;

LocalActivityManager manager = null;

ViewPager pager = null;

TextView t1,t2,t3;

private int offset = 0;// 动画图片偏移量

private int currIndex = 0;// 当前页卡编号

private int bmpW;// 动画图片宽度

private ImageView cursor;// 动画图片


@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


context = MainActivity.this;

manager = new LocalActivityManager(this , true);

manager.dispatchCreate(savedInstanceState);


InitImageView();

initTextView();

initPagerViewer();


}

/**

* 初始化标题

*/

private void initTextView() {

t1 = (TextView) findViewById(R.id.text1);

t2 = (TextView) findViewById(R.id.text2);

t3 = (TextView) findViewById(R.id.text3);


t1.setOnClickListener(new MyOnClickListener(0));

t2.setOnClickListener(new MyOnClickListener(1));

t3.setOnClickListener(new MyOnClickListener(2));

}

/**

* 初始化PageViewer

*/

private void initPagerViewer() {

pager = (ViewPager) findViewById(R.id.viewpage);

final ArrayList<View> list = new ArrayList<View>();

Intent intent = new Intent(context, A.class);

list.add(getView("A", intent));

Intent intent2 = new Intent(context, B.class);

list.add(getView("B", intent2));

Intent intent3 = new Intent(context, C.class);

list.add(getView("C", intent3));


pager.setAdapter(new MyPagerAdapter(list));

pager.setCurrentItem(0);

pager.setOnPageChangeListener(new MyOnPageChangeListener());

}

/**

* 初始化动画

*/

private void InitImageView() {

cursor = (ImageView) findViewById(R.id.cursor);

bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.roller)

.getWidth();// 获取图片宽度

DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

int screenW = dm.widthPixels;// 获取分辨率宽度

offset = (screenW / 3 - bmpW) / 2;// 计算偏移量

Matrix matrix = new Matrix();

matrix.postTranslate(offset, 0);

cursor.setImageMatrix(matrix);// 设置动画初始位置

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

/**

* 通过activity获取视图

* @param id

* @param intent

* @return

*/

private View getView(String id, Intent intent) {

return manager.startActivity(id, intent).getDecorView();

}


/**

* Pager适配器

*/

public class MyPagerAdapter extends PagerAdapter{

List<View> list =  new ArrayList<View>();

public MyPagerAdapter(ArrayList<View> list) {

this.list = list;

}


@Override

public void destroyItem(ViewGroup container, int position,

Object object) {

ViewPager pViewPager = ((ViewPager) container);

pViewPager.removeView(list.get(position));

}


@Override

public boolean isViewFromObject(View arg0, Object arg1) {

return arg0 == arg1;

}


@Override

public int getCount() {

return list.size();

}

@Override

public Object instantiateItem(View arg0, int arg1) {

ViewPager pViewPager = ((ViewPager) arg0);

pViewPager.addView(list.get(arg1));

return list.get(arg1);

}


@Override

public void restoreState(Parcelable arg0, ClassLoader arg1) {


}


@Override

public Parcelable saveState() {

return null;

}


@Override

public void startUpdate(View arg0) {

}

}

/**

* 页卡切换监听

*/

public class MyOnPageChangeListener implements OnPageChangeListener {


int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量

int two = one * 2;// 页卡1 -> 页卡3 偏移量

//计算动态图片的位置并设置

@Override

public void onPageSelected(int arg0) {

Animation animation = null;

switch (arg0) {

case 0:

if (currIndex == 1) {

animation = new TranslateAnimation(one, 0, 0, 0);

} else if (currIndex == 2) {

animation = new TranslateAnimation(two, 0, 0, 0);

}

break;

case 1:

if (currIndex == 0) {

animation = new TranslateAnimation(offset, one, 0, 0);

} else if (currIndex == 2) {

animation = new TranslateAnimation(two, one, 0, 0);

}

break;

case 2:

if (currIndex == 0) {

animation = new TranslateAnimation(offset, two, 0, 0);

} else if (currIndex == 1) {

animation = new TranslateAnimation(one, two, 0, 0);

}

break;

}

currIndex = arg0;

animation.setFillAfter(true);// True:图片停在动画结束位置

animation.setDuration(300);

cursor.startAnimation(animation);

}


@Override

public void onPageScrollStateChanged(int arg0) {

//状态有三个0空闲,1是增在滑行中,2目标加载完毕

//public static final int SCROLL_STATE_IDLE = 0;

//public static final int SCROLL_STATE_DRAGGING = 1;

//public static final int SCROLL_STATE_SETTLING = 2;


}

//从页面1滚动到2的过程

@Override

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

}

}

/**

* 头标点击监听

*/

public class MyOnClickListener implements View.OnClickListener {

private int index = 0;


public MyOnClickListener(int i) {

index = i;

}


@Override

public void onClick(View v) {

pager.setCurrentItem(index);

}

};

}



主要时获取不同intent的视图。。 然后切换的的时候,将该视图增加到ViewPager中,切换下一个的时候移除本ViewPager。


ViewPager使用_ViewPager