Android global note scheduler called repeatedly in android

Add global layout listener is called even if there is no listener connected. It gets into the loop in this situation, how can I edit the attributes without starting the global layout receiver in the loop? thank

final View getDecorView = activity.getWindow().getDecorView();
            getDecorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {

                    if (Build.VERSION.SDK_INT > 16) {
                        getDecorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } else {
                        getDecorView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }

                    final TextView textView3 = (TextView) decorView.findViewById(2131558456);
                    if (textView3 != null) {
                                textView3.setText("updated");                      textView3.setBackgroundColor(Color.parseColor("#444444"));
                    }

                    getDecorView.getViewTreeObserver().addOnGlobalLayoutListener(this);

                }
            });
+4
source share
2 answers

This view solves your problem:

final View decorView = activity.getWindow().getDecorView();
final TextView textView3 = (TextView) decorView.findViewById(2131558456);
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {

        if (Build.VERSION.SDK_INT > 16) {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        } else {
            decorView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }


        if (textView3 != null && !textView3.getText().equals("updated")) {
            textView3.setText("updated");
            textView3.setBackgroundColor(Color.parseColor("#444444"));
        }

        decorView.getViewTreeObserver().addOnGlobalLayoutListener(this);

    }
});

, textview3 , "", , "", , . : IMHO , , , TextView .

final View decorView = activity.getWindow().getDecorView();
final TextView textView3 = (TextView) decorView.findViewById(2131558456);
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    boolean updateTextview = true;

    @Override
    public void onGlobalLayout() {

        if (Build.VERSION.SDK_INT > 16) {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        } else {
            decorView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }


        if (textView3 != null && updateTextview) {
            updateTextview = false;
            textView3.setText("updated");
            textView3.setBackgroundColor(Color.parseColor("#444444"));
        }

        decorView.getViewTreeObserver().addOnGlobalLayoutListener(this);

    }
});

, textview3, , textview3, , ... .

+3

final ViewTreeObserver.OnGlobalLayoutListener listener = this;

                if (Build.VERSION.SDK_INT > 16) {
                    getDecorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    getDecorView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

                final TextView textView3 = (TextView) decorView.findViewById(2131558456);
                if (textView3 != null) {
                    textView3.setText("fert");
                    textView3.setBackgroundColor(Color.parseColor("#444444"));
                }

                getDecorView.post(new Runnable() {
                    @Override
                    public void run() {
                        getDecorView.getViewTreeObserver().addOnGlobalLayoutListener(listener);
                    }
                });
0

All Articles