美文网首页
Android fragment 缓存view

Android fragment 缓存view

作者: Themores | 来源:发表于2015-09-08 17:44 被阅读1073次
private View rootView;// 缓存Fragment view@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.v("onCreateView", "onCreateView");
    if (rootView == null) {
        rootView = inflater.inflate(R.layout.fragment_main_tab, null);
    }
    // 缓存的rootView需要判断是否已经被加过parent,
    // 如果有parent需要从parent删除,要不然会发生这个rootview已经有parent的错误。
    ViewGroup parent = (ViewGroup) rootView.getParent();
    if (parent != null) {
        parent.removeView(rootView);
    }
    return rootView;
}

相关文章

网友评论

      本文标题:Android fragment 缓存view

      本文链接:https://www.haomeiwen.com/subject/iffpcttx.html