hyljava

处理多个fragment之间replace刷新问题

 处理多个fragment之间replace刷新问题[转]
 每次创建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刷新问题  欢迎留言。 

posted on 2014-02-22 22:55 何云隆 阅读(532) 评论(0)  编辑  收藏 所属分类: Android


只有注册用户登录后才能发表评论。


网站导航: