Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> [android] 切換界面的通用處理,android界面

[android] 切換界面的通用處理,android界面

編輯:關於android開發

[android] 切換界面的通用處理,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();
    }

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved