package com.example.viewpager01;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class MainActivity extends Activity {
private ViewPager viewPager;
private List<View> viewList;
private View view1, view2, view3; //
//Context context = null;
LocalActivityManager manager = null; //這裡要實現把Activity轉換為View
private int[] ids = { R.drawable.guide_page_1, R.drawable.guide_page_2,R.drawable.guide_page_3 };//存放圖片陣列,這裡要實現把圖片轉換為View
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.user_lader);
manager = new LocalActivityManager(this , true);
manager.dispatchCreate(savedInstanceState);
viewList=new ArrayList<View>();
for (int i = 0; i < ids.length; i++) {
ImageView iv = new ImageView(this);
iv.setImageResource(ids[i]);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
iv.setLayoutParams(params);
iv.setScaleType(ScaleType.FIT_XY);
viewList.add(iv);
}
Intent intent = new Intent(getApplicationContext(), A.class);
viewList.add(getView("A", intent));
// 1,
viewPager = (ViewPager) findViewById(R.id.view_pager);
// 2,PageAdapter 必須重寫的四個函式:
/*
* boolean isViewFromObject(View arg0, Object arg1) int getCount() void
* destroyItem(ViewGroup container, int position,Object object) Object
* instantiateItem(ViewGroup container, int position)
*/
viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
if(viewList==null)
{
return 0;
}
else
{
return viewList.size(); //getCount():返回要滑動的VIew的個數
}
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
//return arg0 == arg1; //isViewFromObject():對於這個函式就先不做解說,大家眼下先知道它要這樣重寫即可了。後面我們會對它進行改寫。
//System.out.println("position===isViewFromObject======"+(int)Integer.parseInt(arg1.toString())+(arg0 == viewList.get((int)Integer.parseInt(arg1.toString()))));
return arg0 == viewList.get((int)Integer.parseInt(arg1.toString()));////依據傳來的key,找到view,推斷與傳來的引數View arg0是不是同一個檢視
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position)); //做了兩件事。第一:將當前檢視加入到container中。第二:返回當前View
//return viewList.get(position);
//System.out.println("position===instantiateItem======"+position);
return position; // //把當前新增檢視的位置(position)作為Key傳過去
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
if(position==1)
{
viewList=null;
}
else
{
//System.out.println("position====destroyItem====="+position);
container.removeView(viewList.get(position)); //從當前container中刪除指定位置(position)的View;
}
}
});
}
private View getView(String id, Intent intent) {
return manager.startActivity(id, intent).getDecorView();
}
}
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class MainActivity extends Activity {
private ViewPager viewPager;
private List<View> viewList;
private View view1, view2, view3; //
//Context context = null;
LocalActivityManager manager = null; //這裡要實現把Activity轉換為View
private int[] ids = { R.drawable.guide_page_1, R.drawable.guide_page_2,R.drawable.guide_page_3 };//存放圖片陣列,這裡要實現把圖片轉換為View
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.user_lader);
manager = new LocalActivityManager(this , true);
manager.dispatchCreate(savedInstanceState);
viewList=new ArrayList<View>();
for (int i = 0; i < ids.length; i++) {
ImageView iv = new ImageView(this);
iv.setImageResource(ids[i]);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
iv.setLayoutParams(params);
iv.setScaleType(ScaleType.FIT_XY);
viewList.add(iv);
}
Intent intent = new Intent(getApplicationContext(), A.class);
viewList.add(getView("A", intent));
// 1,
viewPager = (ViewPager) findViewById(R.id.view_pager);
// 2,PageAdapter 必須重寫的四個函式:
/*
* boolean isViewFromObject(View arg0, Object arg1) int getCount() void
* destroyItem(ViewGroup container, int position,Object object) Object
* instantiateItem(ViewGroup container, int position)
*/
viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
if(viewList==null)
{
return 0;
}
else
{
return viewList.size(); //getCount():返回要滑動的VIew的個數
}
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
//return arg0 == arg1; //isViewFromObject():對於這個函式就先不做解說,大家眼下先知道它要這樣重寫即可了。後面我們會對它進行改寫。
//System.out.println("position===isViewFromObject======"+(int)Integer.parseInt(arg1.toString())+(arg0 == viewList.get((int)Integer.parseInt(arg1.toString()))));
return arg0 == viewList.get((int)Integer.parseInt(arg1.toString()));////依據傳來的key,找到view,推斷與傳來的引數View arg0是不是同一個檢視
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position)); //做了兩件事。第一:將當前檢視加入到container中。第二:返回當前View
//return viewList.get(position);
//System.out.println("position===instantiateItem======"+position);
return position; // //把當前新增檢視的位置(position)作為Key傳過去
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
if(position==1)
{
viewList=null;
}
else
{
//System.out.println("position====destroyItem====="+position);
container.removeView(viewList.get(position)); //從當前container中刪除指定位置(position)的View;
}
}
});
}
private View getView(String id, Intent intent) {
return manager.startActivity(id, intent).getDecorView();
}
}