Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onGlobalLayout differentiate between various invocations

I have a logo view, which is a full screen fragment containing single ImageView. I have to perform some operations after the logo image is completely visible.

Following code is used to invoke the special task

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ImageView logoImageMaster = new ImageView(getContext());
        //logoImageMaster.setImageResource(resID); //even after removing this, i am getting the callback twice
        try {
            // get input stream
            InputStream ims = getActivity().getAssets().open("product_logo.png");
            // load image as Drawable
            Drawable d = Drawable.createFromStream(ims, null);
            // set image to ImageView
            logoImageMaster.setImageDrawable(d);
        }
        catch(IOException ex) {

        }
        logoImageMaster.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {  //FIXME get called twice. Check this out, no info to distinguish first from second
//                Log.e("PANEL", "onGlobalLayout of Logo IV ---------------------------------");
                    activityInterface.doSpecialLogic();
            }
        });
        return logoImageMaster;
    }

My exact problem is, onGlobalLayout is called twice for this view hierarchy.

I know that onGlobalLayout is invoked in performTraversal of View.java hence this is expected.

For my use case of Single parent with Single child view, I want to distinguish the view attributes such that doSpecialLogic is called once[onGlobalLayout is called twice] , after the logo image is completely made visible. Please suggest some ideas.

like image 724
nmxprime Avatar asked Aug 02 '16 11:08

nmxprime


2 Answers

OnGlobalLayoutListener gets called every time the view layout or visibility changes. Maybe you reset the views in your doSpecialLogic call??
edit as @Guille89 pointed out, the two set calls cause onGlobalLayout to be called two times

Anyhow, if you want to call OnGlobalLayoutListener just once and don't need it for anything else, how about removing it after doSpecialLogic() call??

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
             //noinspection deprecation
              logoImageMaster.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } else {
             logoImageMaster.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
activityInterface.doSpecialLogic();
like image 56
Sarosh Khatana Avatar answered Oct 20 '22 19:10

Sarosh Khatana


It seems to be called one time for each set done over the imageView

logoImageMaster.setImageResource(resID);

logoImageMaster.setImageDrawable(d);
like image 36
Guille89 Avatar answered Oct 20 '22 21:10

Guille89