[android] 切换界面的通用处理详解手机开发

实现不改变activity,只切换View

抽取View界面的基类

利用面向对象多态的思路,实现通用

[android] 切换界面的通用处理详解手机开发 

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(); 
    }

 

 

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/app/5385.html

(0)
上一篇 2021年7月16日 23:47
下一篇 2021年7月16日 23:47

相关推荐

发表回复

登录后才能评论