public class HotFragment extends Fragment { private View mRootView;//缓存fragment View private RecyclerView mRecycleView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if(mRootView==null){ mRootView=inflater.inflate(R.layout.fragment_listview,null); } //缓存的rootView需要判断是否已经被加过parent, 如果有parent则从parent删除,防止发生这个rootview已经有parent的错误。 ViewGroup mViewGroup = (ViewGroup)mRootView.getParent(); if(mViewGroup!=null){ mViewGroup.removeView(mRootView); } return mRootView; }
}
接下来说明下
以上代码是为了实现在多个fragement 实现View的缓存
欢迎关注我的博客 和 github https://github.com/xingwen93