最近在用View inflate(Context context, int resource, ViewGroup root)方法时,在第三个参数root上碰到了点麻烦。

一般在写ListView的adapter时,会这样加载自定义列

View imageLayout = inflate(getContext(),R.layout.item_album_pager, null);
...
viewGroup.addView(imageLayout);

如果这样写,调用imageLayout时就可能出问题

//在inflate时就把layout加入viewGroup
View imageLayout = inflate(getContext(),R.layout.item_album_pager, viewGroup);

这是由于,inflate方法在第三个参数root不为空时,返回的View就是root,而当root为空时,返回的才是加载的layout的根节点。看api解释:

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.
Returns:
The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.

本人便是在ViewPager的adapter中,使用了root不为空的inflate方法,在返回layout时出错了。

@Override
public Object instantiateItem(ViewGroup viewGroup, int position) { View imageLayout = inflate(getContext(),R.layout.item_album_pager, viewGroup);
....
//这里实际上把整个viewGroup返回了,导致出错
return imageLayout;
}

这里举例的是View类自带的inflate方法,它本质调用的就是LayoutInflater类的 View inflate(int resource, ViewGroup root)方法。

public static View inflate(Context context, int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}

Android关于inflate的root参数的更多相关文章

  1. android LayoutInflater.inflate()的参数介绍

    LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...

  2. Android LayoutInflater.inflate使用上的问题解惑

    最近在在使用LayoutInflater.inflate方法时遇到了一些问题,以前没有仔细看过此类的使用方法,故将其记录下来,方便日后查阅. 相信大家都知道LayoutInflater.inflate ...

  3. Android之Inflate()

      Inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能.最近做的一个项目就是这一点让我迷茫了好几天. Android上还有一个与Inflate( ...

  4. struts中的ignoreHierarchy 参数和root 参数

    struts2 1.ignoreHierarchy 参数:表示是否忽略等级,也就是继承关系,比如:TestAction继承于BaseAction,那么TestAction中返回的json字符串默认是不 ...

  5. 怎样使Android应用程序获得root权限

    Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 写这篇文章前,首先要感谢 Simon_fu ,他的两篇关于 root 权 ...

  6. Android Studio 重写方法时参数命名异常

    Android Studio 重写方法时参数命名异常 Android Studio 重写方法时参数名称乱掉可以通过下载相应源码解决

  7. Android 设备,如何root,执行adb shell,查看设备中的数据库信息等【转】

    原文地址: Android 设备,如何root,执行adb shell,查看设备中的数据库信息等

  8. Android系统权限和root权限

    Android系统权限和root权限http://www.verydemo.com/demo_c189_i277.html

  9. Android 上SuperUser获取ROOT权限原理解析

    Android 上SuperUser获取ROOT权限原理解析 一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. ...

随机推荐

  1. 将glassfish 添加到服务中 ,自启

    将glassfish 添加到服务中 ,自启. 命令: sc create wuziServer binPath= D:\wuzi\wuzi-start.bat start= auto

  2. 基于EasyDSS流媒体服务器实现的直播流管理与鉴权的后台方案

    本文转自EasyDSS团队Marvin的博客:http://blog.csdn.net/marvin1311/article/details/73548929 最新版本的EasyDSS流媒体解决方案, ...

  3. git pull时出现vim窗口的解决办法

    最近常用到git来push代码到origin development分支上,也出现了几次vim窗口,防止忘记还是记录下来比较好: 首先按ESC键退出编辑状态,然后按shift+;键,再按wq!保存退出 ...

  4. python爬虫入门(3)-环境搭建

    下载集成包 链接:http://pan.baidu.com/s/1pKD2zBP 密码:f75b 这里采用python2.7.9   安装步骤:1.安装python2.7(默认安装即可) 2.打开“运 ...

  5. MySQL开放外部链接

    问题 指令空格 执行命令netstat -nat | grep :3306的时候,注意grep后一定要有空格!!! 该指令的功能为:让服务器接受远程连接.不过这样只能用于本地连接. 真正外部链接详见 ...

  6. Microsoft Office Powerpoint、Visio 已停止工作解决办法

    现象:在使用visio的过程中经常会出现“Microsoft office visio已停止工作”只能将visio关闭:windows可以尝试恢复您的信息并重新启动该程序.office的其他组件不会出 ...

  7. IAuthenticationManager.SignOut 退不了

      AuthenticationManager.SignOut(); 这个退不了,然后就加上   AuthenticationManager.SignOut( DefaultAuthenticatio ...

  8. 并查集实现Tarjan算法

    本文是对http://noalgo.info/476.html的一点理解,特别是对其中 int father[mx]: //节点的父亲 int ancestor[mx]; //已访问节点集合的祖先 这 ...

  9. C# Post发送数据返回页面结果

    public string GetPage(string posturl, string postData) { Stream outstream = null; Stream instream = ...

  10. IntelliJ-IDEA中mybatis三剑客

    一.mybatis-generator的使用 作用:根据数据库自动生成pojo.dao和xml文件. 1.引入mybatis-generator pom.xml中引入配置: