Chr*_*rry 5 android android-fragments
如果不采用臭名昭着的onGlobalLayoutListener()解决方案,并且不必实现自定义View,我可以将片段中的生命周期事件放入并确保所有Fragment的视图都有一个大小?
作为必然结果,我还希望此生命周期事件适用于ViewPager中的Fragments.
我认为不会有任何片段生命周期事件来确保所有视图都有大小。
我通常会做的是,在 onActivityCreated() 中使用 OnLayoutChangeListener 。像这样,
getView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
getView().removeOnLayoutChangeListener(this);
// Check the size of Views here.
}
});
Run Code Online (Sandbox Code Playgroud)