LayoutInflater类中有多个inflate方法,这里简要说一下:
public View inflate(int resource, ViewGroup root)
public View inflate(int resource, ViewGroup root, boolean attachToRoot)
这两个方法的区别,以及第二个方法中boolean变量的作用。
以前在使用ListView,Adapter的getView方法中,经常使用第一个方法来加载布局文件:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
}
这时IDE会弹出warning:
Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout’s root element)
提示我们不要传递”null”作为root的参数,因为需要决定root节点的属性。
在这里,可以换用另一个inflate方法来避免这个warning:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// 使用三个参数的inflate方法,设定root参数为getView中的parent参数,设定attachToRoot为false
con