[android] 切换界面的通用处理

Posted 陶士涵的菜地

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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();
    }

 

 

 

以上是关于[android] 切换界面的通用处理的主要内容,如果未能解决你的问题,请参考以下文章

Android中切换标签片段之间的延迟

在Android Studio片段之间切换时地图片段不隐藏

Android 逆向Android 逆向通用工具开发 ( Android 平台运行的 cmd 程序类型 | Android 平台运行的 cmd 程序编译选项 | 编译 cmd 可执行程序 )(代码片段

android事件处理概述

Android选项卡片段不调用OnCreateView从tab2切换到tab1

Android排行榜 记录