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 的方法有什么區(qū)別呢蜂嗽;
首先我們通過看源碼得知View梯找。inflate()方法實際上調(diào)用的也是LayoutInflater.from(int resource, ViewGroup root, boolean attachToRoo);
其中最主要的二者區(qū)別就在于ViewGrouproot 這個參數(shù)沒有傳丹弱;
下面我們來看看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.
作用:填充一個新的視圖層次結(jié)構(gòu)從指定的XML資源文件中
reSource:View的layout的ID
root:生成的層次結(jié)構(gòu)的根視圖
return填充的層次結(jié)構(gòu)的根視圖。如果參數(shù)root提供了,那么root就是根視圖晶渠;否則填充的XML文件的根就是根視圖罕伯。
其余幾個重載的inflate函數(shù)類似曲伊。
那么root因為第二傳的是null所以他的parent 就是傳進去的 xml resource;第一個傳了root 所以他的parent就是ViewGroup追他;
所以就會導致啟動的應用
1.
2.