編輯:關於android開發
實現不改變activity,只切換View
抽取View界面的基類
利用面向對象多態的思路,實現通用
TitleManager.java 管理標題
package com.tsh.lottery.views; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RelativeLayout; import com.tsh.lottery.R; /** * 標題管理 * @author taoshihan * */ public class TitleManager { private RelativeLayout generalTitle,unloginTitle,loginTitle; private Button title_login_btn,title_unlogin_btn; private Activity activity; //單例 private static TitleManager instance=new TitleManager(); private TitleManager(){} public static TitleManager getInstance(){ return instance; } /** * 初始化標題 * @param activity */ public void init(Activity activity){ this.activity=activity; generalTitle=(RelativeLayout) activity.findViewById(R.id.title_general); loginTitle=(RelativeLayout) activity.findViewById(R.id.title_login); unloginTitle=(RelativeLayout) activity.findViewById(R.id.title_unlogin); title_login_btn=(Button) activity.findViewById(R.id.title_login_btn); title_unlogin_btn=(Button) activity.findViewById(R.id.title_unlogin_btn); setListener(); } /** * 設置點擊事件 */ private void setListener() { //登陸按鈕 title_login_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showLoginTitle(); BaseView indexView=new IndexView(activity); MiddleManager.getInstance().loadView(indexView); } }); //退出按鈕 title_unlogin_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showUnloginTitle(); BaseView loginView=new LoginView(activity); MiddleManager.getInstance().loadView(loginView); } }); } /** * 隱藏所有標題 */ public void hideTitle(){ generalTitle.setVisibility(View.GONE); unloginTitle.setVisibility(View.GONE); loginTitle.setVisibility(View.GONE); } /** * 顯示通用的標題 */ public void showGeneralTitle() { hideTitle(); generalTitle.setVisibility(View.VISIBLE); } /** * 顯示未登錄的標題 */ public void showUnloginTitle() { hideTitle(); unloginTitle.setVisibility(View.VISIBLE); } /** * 顯示已經登陸的標題 */ public void showLoginTitle() { hideTitle(); loginTitle.setVisibility(View.VISIBLE); } }
MiddleManager.java 管理主體內容
package com.tsh.lottery.views; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.RelativeLayout; import com.tsh.lottery.R; public class MiddleManager { private RelativeLayout mainContainer; //單例 private static MiddleManager manager=new MiddleManager(); private MiddleManager(){}; public static MiddleManager getInstance(){ return manager; } public void setMainContainer(RelativeLayout mainContainer) { this.mainContainer = mainContainer; } /** * 加載界面 */ public void loadView(BaseView baseView) { mainContainer.removeAllViews(); View child=baseView.getView(); mainContainer.addView(child); child.startAnimation(AnimationUtils.loadAnimation(mainContainer.getContext(), R.anim.left_to_right)); } }
BaseView.java
package com.tsh.lottery.views; import android.content.Context; import android.view.View; /** * 主體View基類 * @author taoshihan * */ public abstract class BaseView { public Context context; public BaseView(Context context) { this.context = context; } public abstract View getView(); }
IndexView.java
package com.tsh.lottery.views; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.TextView; /** * 會員界面 * @author taoshihan * */ public class IndexView extends BaseView{ public IndexView(Context context) { super(context); } @Override public View getView() { TextView tv=new TextView(context); LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); tv.setLayoutParams(params); tv.setText("我是會員界面"); return tv; } }
MainActivity.java 管理容器
package com.tsh.lottery; import java.lang.reflect.Field; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.view.ViewConfiguration; import android.view.animation.AnimationUtils; import android.widget.RelativeLayout; import com.tsh.lottery.views.BaseView; import com.tsh.lottery.views.IndexView; import com.tsh.lottery.views.LoginView; import com.tsh.lottery.views.MiddleManager; import com.tsh.lottery.views.TitleManager; public class MainActivity extends Activity { private RelativeLayout mainContainer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.il_main); getActionBar(); setOverflowMenuQues(); initTitle(); initMiddle(); } /** * 初始化主體 */ public void initMiddle() { mainContainer=(RelativeLayout) findViewById(R.id.il_main); BaseView loginView=new LoginView(this); MiddleManager.getInstance().setMainContainer(mainContainer); MiddleManager.getInstance().loadView(loginView); } /** * 初始化標題 */ private void initTitle() { TitleManager manager=TitleManager.getInstance(); manager.init(this); manager.showUnloginTitle(); }
安卓第四天筆記-Sqlite,安卓第四天-sqlite安卓第四天筆記-Sqlite 1.數據庫的創建運行與更新 1.1.創建一個類繼承SqliteOpenHelper &
01-安卓環境初搭建,01-安卓環境搭建需要的軟件如沒有提供下載鏈接,文章中會注明! 首先下載 eclipse 安裝!(筆者提供的鏈接中沒有Eclipse下載!) 安裝完
深入透析Android事件分發機制 一、 Android分發機制概述: Android如此受歡迎,就在於其優秀的交互性,這其中,Android優秀的事件分發機制功不
Android App 開發技能圖譜,androidapp 操作系統 Windows/MacOSX/Linux 編程語言 Java HTML/JS (Hybrid/