onLayoutChange()被调用太多次 [英] onLayoutChange() called too many times
本文介绍了onLayoutChange()被调用太多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
视图初始化后,我尝试更新缩略图图像并知道其大小
I am trying to update a thumbnail image once the view is initialized and knows its sizes
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View pagerView = inflater.inflate(R.layout.fragment_reference, container, false);
......
mPhotoView = pagerView.findViewById(R.id.reference_photo);
mPhotoView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
Log.d(TAG,"Reference Pager PhotoView Layout Change."+" left: "+left+" top: "+top+" right: "+right+ " bottom: "+bottom);
updatePhotoView();
});
......
}
但是 Logcat 显示它已被连续调用四次:
but Logcat shows it has been called four times in a row:
Reference Pager PhotoView Layout Change. left: 0 top: 0 right: 720 bottom: 436
Reference Pager PhotoView Layout Change. left: 0 top: 0 right: 720 bottom: 436
Skipped 59 frames! The application may be doing too much work on its main thread.
Reference Pager PhotoView Layout Change. left: 0 top: 0 right: 720 bottom: 436
Reference Pager PhotoView Layout Change. left: 0 top: 0 right: 720 bottom: 436
什么原因导致这种情况发生?如何避免这种情况?
What causes that to happen? How do I avoid that?
推荐答案
什么原因导致这种情况发生?
What causes that to happen?
您从未删除侦听器.这导致多次调用 onLayoutChange()
.在视图上调用 removeOnLayoutChangeListener()
,如下所示:
You never removed the listener. This results in onLayoutChange()
being invoked multiple times. Call removeOnLayoutChangeListener()
on the view as follows:
mPhotoView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
v.removeOnLayoutChangeListener(this); // this prevents the callback to be invoked multiple times
Log.d(TAG,"Reference Pager PhotoView Layout Change."+" left: "+left+" top: "+top+" right: "+right+ " bottom: "+bottom);
updatePhotoView();
});
这篇关于onLayoutChange()被调用太多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文