返回
顶部

修改密码

android stackover flow problem 不同的UI之间需要相互切换

+1

-1

收藏

+1

-1

点赞0

评论0

在做android UI 的时候,遇到了一个问题,因为不同的UI之间需要相互切换。所以不加思索的写了下面的程式public class FirstLayout extends LinearLayout { public FirstLayout (Context context) { super(context); initial(context); } void…

在做android UI 的时候,遇到了一个问题,因为不同的UI之间需要相互切换。所以不加思索的写了下面的程式

public class  FirstLayout extends LinearLayout {
    public FirstLayout (Context context) {
        super(context);
        initial(context);
    }      
    void initialize(final Context context,t) {
       
            addView(new SecondLayout(context, eventlist));
       
    }   

}

public class  SecondLayout extends LinearLayout  {
  
    public SecondLayout (Context context) {
        super(context);
        initial(context);
    }      
    void initialize(Context context) {    
            initial(new FirstLayout(contextt));  
    }   
}

在这种情况下,比如说,我们点击button,页面进行跳转。但是会进行不断的入栈操作,最终导致stack overflow.

所以为了避免这种情况的出现,要进行另外一种操作,在定义个整体布局,然后分开处理

public class FatherClass  extends LinearLayout{

    private LinearLayout sonLayout = null;

    private LinearLayout daughterLayout = null;

    private Button AButton = null;

    private Button BButton = null;

    public FatherClass (Context context) {

            supper(context);

            initial(context);

    }

    initial(Context context) {

        AButton.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    removeView(sonLayout )
                    addView(daughterLayout);
                }
        });

    BButton.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    removeView(daughterLayout)
                    addView(sonLayout);
                }
        });
    }

    /*generate sonLayout*/

    void addSonLayout(Context){

    sonLayout = new LinearLayout(context);

     .........

    }

/*generate daughterLayout*/

    void addDaughterLayout(Context){

        daughterLayout = new LinearLayout(context);

     .........

    }

}

其中,用的是removeView ,删除这样一个子布局,而不是用removeAllViews。这样做的好处就是可以做到局部处理。

从而避免了嵌套入栈操作。所以这一点在多页面布局的时候应该考虑到。


扫一扫在手机打开

评论
已有0条评论
0/150
提交
热门评论
相关推荐
如何编写高效的Android代码
  • 程序优化
  • 2022-05-20 18:33
  • 0 0 0
+1
Android编程中for循环性能
  • 程序优化
  • 2022-05-20 18:33
  • 0 0 0
+1
Android内存泄漏的原因讲解
  • 程序优化
  • 2022-05-20 18:33
  • 0 0 0
+1
android 优化耗电量性能
  • 程序优化
  • 2022-05-20 18:33
  • 0 0 0
+1
Android App 性能优化 安卓开发教程
  • 程序优化
  • 2022-05-20 18:33
  • 0 0 0
+1
今日要闻
换一批
热点排行