android – onGlobalLayout区分各种调用

前端之家收集整理的这篇文章主要介绍了android – onGlobalLayout区分各种调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个标志视图,它是一个包含单个 ImageView的全屏片段.
标志图像完全可见后,我必须执行一些操作. @H_502_3@以下代码用于调用特殊任务

@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;
    }
@H_502_3@我的确切问题是onGlobalLayout被调用两次这个视图层次结构.

@H_502_3@我知道onGlobalLayout在View.java的performTraversal中被调用,因此这是预期的.

@H_502_3@对于单个单子视图的单个用户的用例,我想区分视图属性,使得在标识图像完全可见后[onGlobalLayout被调用两次]即可调用doSpecialLogic.
请建议一些想法.

解决方法

每次视图布局或可见性更改时,OnGlobalLayoutListener都将被调用.也许您在doSpecialLogic调用中重置视图?
编辑
正如@ Guille89所指出的那样,两个调用使得onGlobalLayout被调用两次 @H_502_3@无论如何,如果你想调用OnGlobalLayoutListener只需一次,不需要它的任何其他,如何删除它后doSpecialLogic()调用

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
             //noinspection deprecation
              logoImageMaster.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } else {
             logoImageMaster.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
activityInterface.doSpecialLogic();

猜你在找的Android相关文章