listview或者recycleview错位

LayoutInflater mInflater = (LayoutInflater) viewGroup.getContext()

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//  View view = View.inflate(viewGroup.getContext(), R.layout.item_main_house, false);

View view = mInflater.inflate(R.layout.item_main_house,viewGroup, false);

1.LayoutInflater.from(RecylerActivity.this).inflate(R.layout.my_text_view,viewGroup,false);

2.View.inflate(RecylerActivity.this,R.layout.my_text_view, null);

这两种 inflate 的方法有什么区别呢;

首先我们通过看源码得知View。inflate()方法实际上调用的也是LayoutInflater.from(int resource, ViewGroup root, boolean attachToRoo);

其中最主要的二者区别就在于ViewGrouproot 这个参数没有传;

下面我们来看看inflate方法的介绍

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

Added in API level 1

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters

resource

ID for an XML layout resource to load (e.g., R.layout.main_page)

root

Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)

attachToRoot

Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Returns

The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

作用:填充一个新的视图层次结构从指定的XML资源文件中

reSource:View的layout的ID

root:生成的层次结构的根视图

return填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。

其余几个重载的inflate函数类似。

那么root因为第二传的是null所以他的parent 就是传进去的 xml resource;第一个传了root 所以他的parent就是ViewGroup;

所以就会导致启动的应用

1.

2.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容