Fragment onViewCreated 的作用
发布日期:2021-06-30 08:02:16 浏览次数:3 分类:技术文章

本文共 1343 字,大约阅读时间需要 4 分钟。

onViewCreated 这个也是自己经常使用的到的,今天礼拜天在家复习知识点 这里记录一下

Fragment 是 3.0版本 API 11 的时候引入的,刚开始的时候并没有onViewCreated ,

它是在API 13的时候引入的 估计这个就是为啥Fragment 生命周期里面没有onViewCreated吧

但是onViewCreated 在Fragment 是有体现的

自己的测试如下

那我们使用呢如下

@Nullable    @Override    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.order_food_fragment_layout, container, false);        Log.e("=======onCreateView","onCreateView");        return view;    }    @Override    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {        super.onViewCreated(view, savedInstanceState);        Log.e("=========onViewCreated","onViewCreated");        textView = view.findViewById(R.id.food);        textView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {            }        });    }

我们使用的时候也很便 它返回fragment要显示的布局view ,这样我们在指定布局的时候就简单的多了 ,在findViewById前面添加一个view 即可

最后看下文档上面提到的

Called immediately after has returned, but before any saved state has been restored in to the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point.

 

 

 

转载地址:https://huxiaomu.blog.csdn.net/article/details/100187169 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Tablayout 多个界面使用一个fragment 的实例
下一篇:NET::ERR_CACHE_MISS

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月22日 12时04分39秒