Skip to content

4. 懒加载、优化Animation性能

YoKey edited this page Dec 21, 2017 · 1 revision

懒加载

在使用Fragment过程中,有些场景需要懒加载,比如FragmentAdapter的懒加载、同级Fragment切换的懒加载,库中自0.8版本提供了onLazyInitView(Bundle saveInstanceState)方法使用。

public class LazyFragment extends BaseFragment {
    /**
     * 懒加载
     */
    public void onLazyInitView(@Nullable Bundle savedInstanceState){
        // todo
    }
}

优化Animation性能

在复杂Fragment页面,第一次start时,会导致该Fragment因复杂初始化和动画的同时进行,导致动画卡顿问题,库中提供一个解决方案:onEnterAnimationEnd(Bundle saveInstanceState)的回调方法。

该方法会在转场动画结束后调用,如果没有动画则在onActivityCreated时调用,此时在onEnterAnimtionEnd(Bundle saveInstanceState)里初始化复杂数据,可以避免保证Fragment动画的流畅。

public View onCreateView(...) {
    ...
    // 这里仅给toolbar设置标题,返回箭头等轻量UI的操作
    initView();
    return view;
}

@Override
protected void onEnterAnimationEnd(Bundle saveInstanceState) {
     // 这里设置Listener、各种Adapter、请求数据等等
    initLazyView();
}

此外,竖直动画在视觉上会比横向动画更流畅