zoukankan      html  css  js  c++  java
  • 带视图缓存的动态布局

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.LinearLayout;
    
    public class PadTestActivity extends Activity {
    
        View chacheView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // setContentView(R.layout.main);
    
            LayoutInflater inflater = LayoutInflater.from(this);
            final View mainView = inflater.inflate(R.layout.main, null);
            // Add first page
            LinearLayout myFirst = (LinearLayout) inflater.inflate(R.layout.first, null).findViewById(R.id.myFirst);
            LinearLayout layoutFirst = (LinearLayout) mainView.findViewById(R.id.LayoutFirst);
            layoutFirst.removeAllViews();
            layoutFirst.addView(myFirst); // Show the page first
            
            // Add second page
            LinearLayout mySecond = (LinearLayout) inflater.inflate(R.layout.second, null).findViewById(R.id.mySecond);
            LinearLayout layoutSecond = (LinearLayout) mainView.findViewById(R.id.LayoutSecond);
            layoutSecond.removeAllViews();
            layoutSecond.addView(mySecond);
            
            // Add third page
            LinearLayout myThird = (LinearLayout) inflater.inflate(R.layout.third, null).findViewById(R.id.myThird);
            LinearLayout layoutThird = (LinearLayout) mainView.findViewById(R.id.LayoutThird);
            layoutThird.removeAllViews();
            layoutThird.addView(myThird);
    
            setContentView(mainView);
            
            chacheView = myFirst;
    
            Button btnDelete = (Button) mainView.findViewById(R.id.btnDelete);
            btnDelete.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
    
                    LinearLayout layoutFirst = (LinearLayout) mainView.findViewById(R.id.LayoutFirst);
                    layoutFirst.removeAllViews();
                }
            });
            
            Button btnAdd = (Button) mainView.findViewById(R.id.btnAdd);
            btnAdd.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    
                    LinearLayout layoutFirst = (LinearLayout) mainView.findViewById(R.id.LayoutFirst);                
                    if (chacheView != null) {
                        layoutFirst.addView(chacheView);
                    }                            
                }
            });
        }
        
    }

     欢迎关注公众号"Devin说",会不定期更新技术知识。
  • 相关阅读:
    关于 “支持“XXXDBContext”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库” 的解决办法
    ASP.NET MVC 添加Model并连接到本地数据库
    C#中的if if...和if-else if
    Git和Github的使用教程
    JavaEE和Tomcat环境
    将windows系统新建的文本文档默认编码为UTF-8
    WIN10缺少.NET Framework3.5解决办法
    上传图片预览设置src不显示
    IOS new Date() 时间转换失败问题以及其他问题
    是否支持css3
  • 原文地址:https://www.cnblogs.com/devinzhang/p/2475421.html
Copyright © 2011-2022 走看看