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.

listview或者recycleview错位_第1张图片

2.

listview或者recycleview错位_第2张图片

你可能感兴趣的:(listview或者recycleview错位)