最近在用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. EasyPusher/EasyDarwin支持H.265 RTSP/RTP直播推流与分发播放

    前言描述 随着大屏时代和高清时代的到来,人们已经不再满足于VGA.CIF这种小分辨率了,取而代之的是720P.1080P.4K级的视频传输,虽然我们国家的基础带宽一直在上升,但普遍情况下,传输高清视频 ...

  2. HAWQ + MADlib 玩转数据挖掘之(三)——向量

    一.定义 这里不讨论向量严格的数学定义.在Madlib中,可以把向量简单理解为矩阵.矩阵是Madlib中数据的基本格式,当矩阵只有一维时,就是向量,1行n列的矩阵称为行向量,m行1列的矩阵称为列向量, ...

  3. ng-if 判断条件中不能 使用变量名字拼接,switch可以

  4. 单机ZooKeeper配置

    1.创建zoo.cfg copy D:\zookeeper3.4.6\conf\zoo_sample.cfg zoo.cfg 修改追加如下内容 dataDir=D:/zookeeper3.4.6/da ...

  5. 自己整合的SSM框架 maven坐标大全

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. 让svn具有分布式的功能。

    最近开发遇到了个难事.公司的svn库不能随便提交,必须要经过验证.但是平时修改太多,如果不提交到svn说不定前面被删掉的代码后面又需要了.svn自带的relocate和switch都不能达到要求.找遍 ...

  7. ubuntu 安装nodejs/npm

    安装发行稳定版 Ubuntu 默认仓库里带有Node.js,版本较旧,这并不是最新版,但是应该很稳定.执行如下命令:   1 2 sudo apt-getupdate sudo apt-getinst ...

  8. LG1429 平面最近点对(加强版)

    题意 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的 2≤n≤200000 分析 参照3A17K的题解. 我们充分发扬人类智慧: 将所有点全部绕原点旋转 ...

  9. sysbench fileio 压力测试

    备注:   使用的是yum 安装   1. 安装 yum install -y sysbench 2. 命令 fileio options: --file-num=N number of files ...

  10. android 自己定义checkbox 背景图无效的问题

    http://blog.csdn.net/zuolongsnail/article/details/7106586  正常的定义能够參考这个网址  可是我參考它以后发现我执行时候 根本不工作嘛  结果 ...