我有一个标志视图,它是一个包含单个
ImageView的全屏片段.
标志图像完全可见后,我必须执行一些操作.
标志图像完全可见后,我必须执行一些操作.
@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; }
我的确切问题是onGlobalLayout被调用两次这个视图层次结构.
我知道onGlobalLayout在View.java的performTraversal中被调用,因此这是预期的.
对于单个单子视图的单个用户的用例,我想区分视图属性,使得在标识图像完全可见后[onGlobalLayout被调用两次]即可调用doSpecialLogic.
请建议一些想法.
解决方法
每次视图布局或可见性更改时,OnGlobalLayoutListener都将被调用.也许您在doSpecialLogic调用中重置视图?
编辑
正如@ Guille89所指出的那样,两个调用使得onGlobalLayout被调用两次
编辑
正如@ Guille89所指出的那样,两个调用使得onGlobalLayout被调用两次
无论如何,如果你想调用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();