每次创建fragment对象都会重新走onCreateView方法,所以多个fragment互相替换会重新刷新界面,
在application中创建一个View,保持onCreateVIew中创建的View
每次走onCreateView的时候判断application中是否保持了View,如果为null,重新inflater走initView和initData方法,不为nul得到父类,移除子View,不然有父id无法再加入布局中,
以下是代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
GalaxyApplication galaxyApplication = (GalaxyApplication) getActivity().getApplication();
View recommendView = galaxyApplication.getRecommendView();
if(recommendView != null){
ViewGroup group = (ViewGroup) recommendView.getParent();
group.removeAllViews();
return recommendView;
}
View fmRootView = inflater.inflate(R.layout.fragment_recommend, container,false);
initView(fmRootView);
initData();
galaxyApplication.setRecommendView(fmRootView);
Logger.d("fragment: ", "onCreateView");
return fmRootView;
}
如有好的方法,处理onCreateView刷新问题 欢迎留言。