【android】实现手指滑动来切换activity(转)

MainActivity.java外部引用 原始文档
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package misc.demo;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;

public class MainActivity extends ActivityGroup {
	private View oneView;
	private View twoView;
	private View threeView;
	private ArrayList<View> views;
	private ViewPager mViewPager;//多页面滑动切换效果
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		views = new ArrayList<View>();
		mViewPager = (ViewPager)findViewById(R.id.main_viewpager);        
	    mViewPager.setOnPageChangeListener(null);
		initView();
		addViews();
		
		PagerAdapter mPagerAdapter = new PagerAdapter() {
			
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}
			
			@Override
			public int getCount() {
				return views.size();
			}
			
			@Override
			public void destroyItem(View container, int position, Object object) {
				((ViewPager)container).removeView(views.get(position));
			}
			
			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager)container).addView(views.get(position));
				return views.get(position);
			}
		};
		
		mViewPager.setAdapter(mPagerAdapter);
	}

	/**
	 * 将相应的Activity转换成View对象
	 */
	public void initView(){
		oneView=getViews(OneActivity.class,"one");
		twoView=getViews(TwoActivity.class,"one");
		threeView=getViews(ThreeActivity.class,"one");
	}

	/**
	 * 将转换成View后的Activity添加到列表中
	 */
	public void addViews(){
		
		views.add(oneView);
		views.add(twoView);
		views.add(threeView);
	}
	/**
	 * 获取要跳转的Activity对应的View
	 * @param cls目的Activity
	 * @param pageid目的Activity的唯一id
	 * @return 返回一个View类型的变量
	 */
	public View getViews(Class<?> cls,String pageid){
		return getLocalActivityManager().startActivity(pageid, new Intent(MainActivity.this,cls).addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)).getDecorView();
	}
}
posted @ 2013-10-24 14:40  penink  阅读(4016)  评论(0编辑  收藏  举报