【安卓】android Fragment View的缓存以实现重复利用

来源:互联网 发布:薄膜键盘推荐 知乎 编辑:程序博客网 时间:2024/06/10 06:22
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

0 0
原创粉丝点击