Android onGlobalLayout区分各种调用
我有一个logo视图,它是一个包含单个Android onGlobalLayout区分各种调用,android,android-view,android-viewgroup,Android,Android View,Android Viewgroup,我有一个logo视图,它是一个包含单个ImageView的全屏片段。 我必须在徽标图像完全可见后执行一些操作 下面的代码用于调用特殊任务 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ImageView logoImageMaster = n
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
被调用了两次
我知道View.java的performTraversal
中调用了onGlobalLayout
,因此这是意料之中的
对于单亲视图和单子视图的用例,我想区分视图属性,以便在徽标图像完全可见后,调用一次doSpecialLogic
。
请提供一些建议。每当视图布局或可见性更改时,都会调用OnGlobalYoutListener。也许您在doSpecialLogic调用中重置了视图??
编辑
正如@Guille89所指出的,两个set调用导致OnGloballYout被调用两次
无论如何,如果您只想调用OnGlobalLayoutListener一次,而不需要它做任何其他事情,那么在doSpecialLogic()调用之后删除它怎么样
if(Build.VERSION.SDK\u INT
似乎对通过imageView完成的每个集合调用一次
logoImageMaster.setImageResource(resID);
logoImageMaster.setImageDrawable(d);
您应该尝试在android中使用kotlin插件
此布局侦听器通常用于在测量视图后执行某些操作,因此通常需要等待宽度和高度大于0。在你的例子中,我们可能想用这个观点做点什么
图像视图
因此,对函数进行了泛化,以便任何扩展视图的对象都可以使用该函数,并且可以从该函数访问其所有特定函数和属性
[kotlin]
inline fun <T: View> T.afterMeasured(crossinline f: T.() -> Unit) {
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (measuredWidth > 0 && measuredHeight > 0) {
viewTreeObserver.removeOnGlobalLayoutListener(this)
f()
}
}
})
}
[/kotlin]
[kotlin]
测得的内联f:T.()->单位{
viewTreeObserver.addOnGlobalLayoutListener(对象:viewTreeObserver.OnGlobalLayoutListener{
覆盖Loballayout(){
如果(测量宽度>0和测量高度>0){
viewTreeObserver.removeOnGlobalLayoutListener(此)
f()
}
}
})
}
[/kotlin]
注意:
确保布局中正确描述了ImageView。也就是说,它的布局宽度和布局高度不能是包裹内容。此外,其他视图不得导致此ImageView的大小为0。抱歉。我不允许使用任何外部插件Hi,我仍然得到两次,现在我只设置了一次,请查看问题的更新Hi,我仍然得到两次,现在我只设置了一次,请查看问题的更新否,我没有按照文件onGlobalLayout重置doSpecialLogic
中的视图“当全局布局状态或视图树中视图的可见性更改时调用回调方法”,因此视图必须再次重新加载,这将调用onGlobalLayout两次。刚刚意识到,您不想停止对onGlobalLayout的两次调用,而是希望使用doSpecialLoagic()调用一次。在doSpecialLogic之前设置一个布尔标志如何?例如,如果(!flag){doSpecialLogic;flag=true;}我知道,我设置了一个标志,那么我需要记录这个。
[kotlin]
inline fun <T: View> T.afterMeasured(crossinline f: T.() -> Unit) {
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (measuredWidth > 0 && measuredHeight > 0) {
viewTreeObserver.removeOnGlobalLayoutListener(this)
f()
}
}
})
}
[/kotlin]