android数组运算,Android TypedArray——相同结构单元的操作

实践

页面上有很多结构相同的单元,只是填充的内容不一样,我想通过遍历的方法对每一个单元进行操作。如下操作就可以实现了。是不是很省心。关键点TypedArray。

TypedArray item = getContext().getResources().obtainTypedArray(R.array.guide_template_item);

for (int i = 0; i < Math.min(guideUnits.size(), MAX_COUNT); i++) {

GuideItem guideItem = (GuideItem) findViewById(item.getResourceId(i, 0));

......

}

// 用完之后,记得回收

item.recycle();

在arrays中配置如下:

@id/guide1

@id/guide2

@id/guide3

TypedArray

Container for an array of values that were retrieved with

{@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)}

or {@link Resources#obtainAttributes}. Be

sure to call {@link #recycle} when done with them.

The indices used to retrieve values from this structure correspond to

the positions of the attributes given to obtainStyledAttributes.

TypedArray是一个容器,这个容器存放的是array的值(看名字也知道了),存放在容器中的这些值使用Resources.Theme下的obtainStyledAttributes和Resources下的obtainAttributes来检索。当完成检索的时候,请调用recycle方法。indices(index复数)用来从对应的容器中检查对应属性的值。所有位置的值可以通过obtainStyledAttributes方法获取到。(obtainStyledAttributes返回TypedArray)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值