Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android viewTreeObserver.addOnGlobalLayoutListener、onGlobalLayout、infinte循环_Android - Fatal编程技术网

Android viewTreeObserver.addOnGlobalLayoutListener、onGlobalLayout、infinte循环

Android viewTreeObserver.addOnGlobalLayoutListener、onGlobalLayout、infinte循环,android,Android,我在两个文本视图的父视图上使用viewTreeObserver.addOnGlobalLayoutListener,onGlobalLayout,以获得一个文本视图的宽度和另一个文本视图的高度 private int row_height; private int column_width; private RelativeLayout RL; @Override protected void onCreate(Bundle savedInstanceState) { super.on

我在两个文本视图的父视图上使用
viewTreeObserver.addOnGlobalLayoutListener
onGlobalLayout
,以获得一个文本视图的宽度和另一个文本视图的高度

 private int row_height;
private int column_width;
private RelativeLayout RL;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RL = (RelativeLayout) findViewById(R.id.RL);
    final TextView textView = (TextView) findViewById(R.id.textView33);
    final TextView textView2 = (TextView) findViewById(R.id.textView25);
    ViewTreeObserver viewTreeObserver = RL.getViewTreeObserver();
    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            column_width = textView2.getWidth();
            row_height = textView.getHeight();
            textView.setText("column_width" + column_width + "\nrow_height" + row_height);
            CreateEvent(0, 6, 4);
            CreateEvent(0, 8, 6);
            CreateEvent(0, 7, 7);
            CreateEvent(0, 5, 2);
        }
    });

    //CreateEvent(0,6,4); //not working
}
问题是
CreateEvent
被无限次调用,这会减慢我的应用程序的速度,那么如何解决这个问题呢


我想做的是,当布局完全显示出来时,我保存
textView25的宽度和
textView33的高度,以便在
CreateEvent
中使用它们,并且只有当布局从
纵向
更改为
横向
时,它们才会更改,只要收到回调,就立即删除侦听器-

viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {

        //Add this line
        viewTreeObserver.removeOnGlobalLayoutListener(this);

        column_width = textView2.getWidth();
        row_height = textView.getHeight();
        textView.setText("column_width" + column_width + "\nrow_height" + row_height);
        CreateEvent(0, 6, 4);
        CreateEvent(0, 8, 6);
        CreateEvent(0, 7, 7);
        CreateEvent(0, 5, 2);
    }
});

收到回调后立即删除侦听器-

viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {

        //Add this line
        viewTreeObserver.removeOnGlobalLayoutListener(this);

        column_width = textView2.getWidth();
        row_height = textView.getHeight();
        textView.setText("column_width" + column_width + "\nrow_height" + row_height);
        CreateEvent(0, 6, 4);
        CreateEvent(0, 8, 6);
        CreateEvent(0, 7, 7);
        CreateEvent(0, 5, 2);
    }
});

我也有同样的问题。正如另一位回答者所建议的那样,我试图直接从ViewTreeObserver对象中删除OnGlobalYoutListener,但它给出了一个错误。最后,我从视图中抓取了树观察者,并从中删除了OnGlobalLayoutListener,如下所示:

private class GLListener implements ViewTreeObserver.OnGlobalLayoutListener
{
    @Override
    public void onGlobalLayout()
    {
        adView.getViewTreeObserver().removeOnGlobalLayoutListener(glListener);  //or "this"
        /*
         *CODE HERE RUNS ONCE
         */
    }
}

GLListener是我实例化的一个类,作为OnGloballYoutListener对象添加到adView,它是(正如您可能已经猜到的)adView。我这样做是为了避免嵌套类。你可以匿名。无论哪种方式,使用“this”都可以代替像我一样的对象引用。

我也遇到了同样的问题。正如另一位回答者所建议的那样,我试图直接从ViewTreeObserver对象中删除OnGlobalYoutListener,但它给出了一个错误。最后,我从视图中抓取了树观察者,并从中删除了OnGlobalLayoutListener,如下所示:

private class GLListener implements ViewTreeObserver.OnGlobalLayoutListener
{
    @Override
    public void onGlobalLayout()
    {
        adView.getViewTreeObserver().removeOnGlobalLayoutListener(glListener);  //or "this"
        /*
         *CODE HERE RUNS ONCE
         */
    }
}

GLListener是我实例化的一个类,作为OnGloballYoutListener对象添加到adView,它是(正如您可能已经猜到的)adView。我这样做是为了避免嵌套类。你可以匿名。无论哪种方式,都可以使用“this”来代替像我一样的对象引用。

错误:此ViewTreeObserver不活动,再次调用getViewTreeObserver(),然后再次调用它-
RL.getViewTreeObserver().RemoveOnGlobalYoutListener(this)这不起作用。它给出了Atef Hares提到的错误。再次调用它只会产生相同的错误,假设您捕获了第一个,并且应用程序没有崩溃。错误:此ViewTreeObserver不活动,请再次调用getViewTreeObserver(),然后再次调用它-
RL.getViewTreeObserver().RemoveOnGloballYoutListener(此)这不起作用。它给出了Atef Hares提到的错误。再次调用它只会产生相同的错误,假设您捕获了第一个,并且应用程序没有崩溃。