最近在用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. RDS 在线DDL诡异报错ERROR 1062 (23000): Duplicate entry

    RDS上执行报错如下: MySQL [ad_billing]> ALTER TABLE ad_spending ADD COLUMN impr bigint(20) NOT NULL DEFAU ...

  2. Ubuntu在图形界面和命令行界面都循环登录解决办法

    在做机器ip变化自动发送邮件的时候,在/etc/profile.d/目录下添加了一个脚本,重启的时候就循环登录了,无论是在图形界面还是命令行界面. 解决方法:利用系统U盘进入系统进行修改,具体如下: ...

  3. [Linux] nohup/setsid/& 让进程在后台可靠运行

    当用户注销(logout)或者网络断开时,终端会收到 HUP(hangup)信号从而关闭其所有子进程.因此,我们的解决办法就有两种途径:要么让进程忽略 HUP 信号,要么让进程运行在新的会话里从而成为 ...

  4. python函数作用域

    python中函数作用域 在python中,一个函数就是一个作用域 name = 'xiaoyafei' def change_name(): name = '肖亚飞' print('在change_ ...

  5. go语言学习 strings常用函数

    strings包中的函数用法 参考链接http://studygolang.com/articles/88 1.strings.replace() 函数原型 func Replace(str1, ol ...

  6. Mongodb中在已有Colloection插入/更新相关域值

    [{ "confident" : "no", "score" : 0.327355, "label" : "/ ...

  7. 【paper】KDD15 - Interpreting Advertiser Intent in Sponsored Search

    Interpreting Advertiser Intent in Sponsored Search   主要内容是搜索广告的相关性预估模型,使用learning to rank的方法.亮点在于使用了 ...

  8. 【angular之起步】安装

    人生只有眼前的苟且. 所以为了远方,最近在策划一个大阴谋------做一个自己的网站,杂而全的. 各种胡思乱想了一周,先把页面写完了,没辙,就这个不用费太多脑子. 然后开始重头戏,就卡死了. angu ...

  9. Canvas 与 SVG 的区别

    这个说实话,我只用过canvas画过一些简单的图形,复杂的不懂,之所以列出来,是因为之前在面试中有被问到,在这里mark一下,后期深化. 以下的内容全部来自于w3school http://www.w ...

  10. 快速排序的partition版本实现

    int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low-1; for (int j = low ...