1. 程式人生 > >自己創造的一個APP架構,只用一個Activity

自己創造的一個APP架構,只用一個Activity

全部頁面都是View,View內又可以有子View,它們不斷切換能造成絕大多數APP的效果。

最大的好處就是隻有一個Activity,管理起來容易。

如果認為記憶體不夠,可以適當銷燬一些View,手工管理記憶體(其實也很簡單)。

MainActivity.java

package com.activity;

import com.subview.SubFrameView;
import com.view.FrameView;
import com.view.GuideView;
import com.view.MainView;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AnimationUtils;

public class MainActivity extends Activity {
	FrameView currentView;
	
	public GuideView guideView;
	public MainView mainView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		FrameView.act=this;
		SubFrameView.act=this;
		
		setView(guideView=new GuideView());
	}
	
	/**
	 * 設定頁面
	 * @param view
	 */
	public void setView(FrameView view){
		View v=view.getView();
		
		setContentView(v);
		view.reInit();
		currentView=view;
	}
	
	/**
	 * 設定頁面,動畫
	 * @param view
	 */
	public void setView(FrameView view, int outAnim, int inAnim) {

		if (currentView == null) {
			
		} else {
			View outView = currentView.getView();
			View inView = view.getView();
			
			outView.startAnimation(AnimationUtils.loadAnimation(this, outAnim));
			inView.startAnimation(AnimationUtils.loadAnimation(this, inAnim));
		}
		
		setView(view);
	}
	
    public boolean onKeyDown(int kc,KeyEvent ev){
		if (kc == KeyEvent.KEYCODE_BACK && ev.getAction() == KeyEvent.ACTION_DOWN) {
			currentView.back();
        }
        return false;
    }
    
    protected void onResume(){
    	super.onResume();
    	currentView.onResume();
    }
	
	protected void onPause(){
		super.onPause();
		currentView.onPause();
	}
	
}
FrameView.java
package com.view;

import com.activity.MainActivity;
import com.subview.SubFrameView;

import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;

public abstract class FrameView {

	View my;
	public static MainActivity act;
	
	public View getView(){
		if(my==null){
			createView();
		}
		
		return my;
	}
	
	//在此方法中初始化my
	protected abstract void createView();
	
	//銷燬
	public void destoryView(){
		my=null;
	}
	
	//back鍵返回
	public abstract void back();
	
	//每次setView()時會自動呼叫,有需要就重寫
	public void reInit(){
		
	}
	
	//有需要就重寫
	public void onResume(){
		
	}
	
	public void onPause(){
		
	}
	
	
	//sub view
	ViewGroup viewGroup;//先設定viewGroup(子view的容器),再使用
	SubFrameView<? extends FrameView> currentSubView;
	
	//設定子view
	public void setSubView(SubFrameView<? extends FrameView> subview){
		viewGroup.removeAllViews();
		
		View view=subview.getView();
		
		viewGroup.addView(view);
		subview.reInit();
		
		currentSubView=subview;
	}
	
	//設定子view,動畫
	public void setSubView(SubFrameView<? extends FrameView> subview, int outAnim, int inAnim){
		
		if (currentSubView == null) {
			
		} else {
			View outView = currentSubView.getView();
			View inView = subview.getView();
			
			outView.startAnimation(AnimationUtils.loadAnimation(act, outAnim));
			inView.startAnimation(AnimationUtils.loadAnimation(act, inAnim));
		}
		
		setSubView(subview);
	}
	
}


SubFrameView.java
package com.subview;

import android.view.View;

import com.activity.MainActivity;
import com.view.FrameView;

public abstract class SubFrameView<T extends FrameView> {

	View my;
	T parentView;//父view
	public static MainActivity act;
	
	public SubFrameView(T parentView){
		this.parentView=parentView;
	}
	
	public View getView(){
		if(my==null){
			createView();
		}
		
		return my;
	}
	
	protected abstract void createView();
	
	public void destoryView(){
		my=null;
	}
	
	public void reInit(){
		
	}
}


詳細demo程式碼: