ViewTreeObserver Listener not called after a while

I have great difficulty using ViewTreeObserver. Everything works fine for some unpredictable period of time, and then the listener no longer works. This, of course, is because of what is written in the document: The returned ViewTreeObserver observer does not guarantee that it will remain valid for the lifetime of this view. Therefore, I repeat the setting every time I change the view:

   protected void shrinkToFit(final TextView t) {
    if(vto==null||!vto.isAlive()){
    vto = t.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            doAdjust(t);
        }
    });
    }
}

and this is how I call it:

TextView t = (TextView) findViewById(R.id.maindesc);

                t.setTextSize(Constants.MAINDESC_SIZE);

                String todisp_1 = tarifreadtemp.area_desc + ":"
                        + tarifreadtemp.area_tarifuserdesc;
                shrinkToFit(t);
                t.setText(todisp_1);
                t.invalidate();
+5
source share
2 answers

addOnGlobalLayoutListener , ( , , , ), , , .

+1

, onGlobalLayout . .

: , , onScroll , . , onGlobalLayout .

, reset .

, onScroll, , , reset .

0

All Articles