[android] 切換介面的問題,android切換

來源:互聯網
上載者:User

[android] 切換介面的問題,android切換

1. 介面重複建立的問題

2. 中間容器每次切換,都會清空容器中的子物件問題

3. 點擊返回鍵的處理

 

解決重複建立的問題:

傳遞Class位元組碼對象,利用泛型來規定對象

判斷介面是否存在,如果存在重複使用,如果不存在,建立;記錄當前正在展示的介面

MainActivity.java

    /**     * 初始化主體     */    public void initMiddle() {        mainContainer=(RelativeLayout) findViewById(R.id.il_main);        MiddleManager.getInstance().setMainContainer(mainContainer);        MiddleManager.getInstance().loadView(LoginView.class);    }

MiddleManager.java

package com.tsh.lottery.views;import java.lang.reflect.Constructor;import java.util.HashMap;import java.util.Map;import android.content.Context;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;    }    //介面緩衝    private Map<String,BaseView> VIEWCACHE=new HashMap<String,BaseView>();    //當前介面    private BaseView currentView;    /**     * 載入介面     */    public void loadView(Class<? extends BaseView> baseViewClass) {                BaseView baseView=null;        //判斷是否存在        String key=baseViewClass.getSimpleName();        if(VIEWCACHE.containsKey(key)){            baseView=VIEWCACHE.get(key);        }else{            try {                Constructor<? extends BaseView> constructor;                constructor = baseViewClass.getConstructor(Context.class);                baseView=constructor.newInstance(mainContainer.getContext());            } catch (Exception e) {                e.printStackTrace();            }            VIEWCACHE.put(key, baseView);        }        mainContainer.removeAllViews();//        if(currentView!=null){//            SlideUtil.slideOut(currentView.getView(), 2000);//        }                View child=baseView.getView();        mainContainer.addView(child);        //SlideUtil.slideIn(child, 2000);        child.startAnimation(AnimationUtils.loadAnimation(mainContainer.getContext(), R.anim.left_to_right));        currentView=baseView;    }}

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{    private TextView textView;    /**     * 建構函式     * 經過之前的處理,這個建構函式只會執行一次     * @param context     */    public IndexView(Context context) {        super(context);        initView();    }    /**     * 初始化介面     */    private void initView() {        textView=new TextView(context);        LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);        textView.setLayoutParams(params);        textView.setText("我是會員介面");    }    @Override    public View getView() {        return textView;    }}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.