@kimo
2016-02-15T02:17:26.000000Z
字数 571
阅读 3445
android笔记 daygo
原文请点击这里
private View rootView;//缓存Fragment view@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {if(rootView==null){rootView=inflater.inflate(R.layout.tab_fragment, null);}//缓存的rootView需要判断是否已经被加过parent, 如果有parent需要从parent删除,要不然会发生这个rootview已经有parent的错误。ViewGroup parent = (ViewGroup) rootView.getParent();if (parent != null) {parent.removeView(rootView);}return rootView;}
Fragment之间切换时每次都会调用onCreateView方法,导致每次Fragment的布局都重绘,无法保持Fragment原有状态。
解决办法:在Fragment onCreateView方法中缓存View