1. 程式人生 > >ViewPager結合Activity的使用及父Activity中呼叫子Activity的方法

ViewPager結合Activity的使用及父Activity中呼叫子Activity的方法

主要程式碼如下:

ViewPager實現主要為介面卡 MyPagerAdapter及頁面切換監聽MyOnPageChangeListener

注意實現其中的Override方法,呼叫子Activity的方式在MyOnPageChangeListener的 onPageSelected中實現

引用OneActivity中的

oneActivityDo();
及TwoActivity中的
oneActivityDo();
具體實現就不詳細列出了,只做個方法呼叫

其中呼叫子Activity中的方法主要實現為:

loadCurActivity(int arg0)
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyViewPagerActivity extends Activity {
	private ArrayList<View> mlistview = new ArrayList<View>(); //viewpager中的內容
	private LocalActivityManager mactivityManager = null;
	private ViewPager mvp_content = null;
	private String[] mlistTag = {"one","two"}; //activity標識
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mactivityManager = new LocalActivityManager(this, true);
		mactivityManager.dispatchCreate(savedInstanceState);
		initPagerView();
	}
	private initPagerView(){
		Intent intent = new Intent(getApplicationContext(), OneActivity.class);
		View v1 = getView(mlistTag[0], intent);
		mlistview.add(v1);
		Intent intent2 = new Intent(getApplicationContext(), TwoActivity.class);
		View v2 = getView(mlistTag[1], intent);
		mlistview.add(v2);
		
		mvp_content = (ViewPager) findViewById(R.id.vp_content);

		mvp_content.setAdapter(new MyPagerAdapter(mlistview));
		mvp_content.setCurrentItem(0);
		mvp_content.setOnPageChangeListener(new MyOnPageChangeListener());
	}
	/**
	 * Pager介面卡
	 */
	public class MyPagerAdapter extends PagerAdapter {
		
		List<View> listview =  new ArrayList<View>();
		public MyPagerAdapter(List<View> list) {
			this.listview = list;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object obj) {
			container.removeView(listview.get(position));
		}


		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(listview.get(position),0);
			return listview.get(position);
		}

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

		@Override
		public boolean isViewFromObject(View v, Object obj) {
			return v == obj;
		}
	}
	/**
	 * 頁卡切換監聽
	 */
	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int arg0) {
			int pos = 0;//記錄hsv_column滾動位置
			Log.i("onPageSelected", "position="+arg0);
			loadCurActivity(arg0);
		}
		
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}
	}
	
	/**
	 * 通過activity獲取檢視
	 * 
	 * @param id
	 * @param intent
	 * @return
	 */
	private View getView(String id, Intent intent) {
		return mactivityManager.startActivity(id, intent).getDecorView();
	}
	
	/**
	 * 
	 * @param arg0:頁面位置
	 * @function:呼叫子Activity中的方法
	 * 
	 */
	private void loadCurActivity(int arg0){
		Activity curActivity = mactivityManager.getActivity(mlistTag[arg0]);
		switch(arg0){
		case 0:
			if(curActivity != null && curActivity instanceof OneActivity){
				((OneActivity)curActivity).oneActivityDo();
			}
			break;
		case 1:
			if(curActivity != null && curActivity instanceof TwoActivity){
				((TwoActivity)curActivity).twoActivityDo();
			}
			break;
		}
	}
}