Android:ImageView的findViewById(自定义适配器) [英] Android: findViewById of an ImageView (custom adapter)

查看:366
本文介绍了Android:ImageView的findViewById(自定义适配器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试重新设置ImageView的大小. 我正在使用自定义适配器.

I've been trying to re-set the size of an ImageView. I'm using a custom adapter.

View row = convertView;
if (row == null) {
    LayoutInflater inflater =  (LayoutInflater)
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.grid_item, null);
}

thumb = (ImageView) row.findViewById(R.id.bookThumb);

当我使用ImageView thumb =(ImageView)row.findViewById(R.id.bookThumb);在自定义适配器内部可以很好地工作,但是由于它遍历每一行,因此滚动速度非常慢.

When I use ImageView thumb = (ImageView) row.findViewById(R.id.bookThumb); inside the custom adapter it works great but because it's looping through every row it makes the scrolling very slow.

所以...我现在正试图让thumb =(ImageView)row.findViewById(R.id.bookThumb);从我的主活动中并设置其大小,但我得到的是Null,因此应用程序强制关闭.

So... I'm now trying to get the thumb = (ImageView) row.findViewById(R.id.bookThumb); from my Main Activity and set its size but I get Null and therefore the app force closes.

尝试:

LinearLayout layout = (LinearLayout) findViewById(R.id.gridLayout);
ImageView thumbRow = (ImageView)layout.findViewById(R.id.bookThumb);

也尝试过:

// gv is the gridview (R.id.gridview)
ImageView thumbRow = (ImageView)gv.findViewById(R.id.bookThumb);

总是为空.

感谢任何帮助!感觉我想念一些愚蠢的东西:)

Appreciate any help! feels like I'm missing something silly :)

推荐答案

我认为问题出在两个地方.

I think the problem stems in two places.

首先解决原始问题.由于findViewById调用,您的适配器速度很慢.您可以使用回收的视图,这很棒,但是仍然可以遍历缩略图的视图.有一种技术,非正式地称为ViewHolder,基本上使它更像是直接调用.

First off the original problem. Your Adapter is slow because of the findViewById call. You make use of the recycled view and that's great, but you still traverse the view for the thumbnail. There is a technique, informally called ViewHolder, which basically makes it more of a direct call.

因此,不仅仅是将回收的视图用作开始填充数据的地方,还应在其上附加一些结构,以便您可以预取要处理的元素

So instead of just using the recycled view as a place to start filling data, also attach some structure to it so you can prefetch the elements you want to manipulate

if (row == null) {
    LayoutInflater inflater =  (LayoutInflater)
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.grid_item, null);
    row.setTag(row.findViewById(R.id.bookThumb));

}

thumb = (ImageView) row.getTag();

在这里,我们直接通过变量调用,并且没有遍历.这应该更快.

Here we call directly by variable and no traversing is done. This should be much faster.

第二个问题是NullPointerExcepton.

Also the second problem, the NullPointerExcepton.

我认为这是ID的含混不清与您在主要活动中调用findViewById的点.您实际上不能在mainActivity中做得很好,尤其是涉及滚动时,因为您必须拦截该调用才能在适配器视图中进行渲染.大多数回调已经在主线程上,因此该线程不能同时发生两件事.

I think it's the ambiguity of ids mixed with the point at which you call findViewById in your main activity. You can't really do it well in your mainActivity especially if there is scrolling involved as you would have to intercept the call to render within the adapter view. Most of the callbacks are already on the main thread so two things cannot happen at the same time on that thread.

因此,在您称为findViewById()的那一刻,适配器可能甚至还没有运行,并将该视图附加到层次结构上.如果这样做的话,它可能还没有完成操作.

So at the point you called findViewById() the adapter may not have even run yet and attached that view to the hierarchy. And if it did, it might not have finished manipulating it yet.

在解决第二个问题之前,我会尝试加快适配器的速度,因为我不确定是否有可能安全或准确地完成它,而不会完全放弃使用适配器视图.

I would try to speed up your adapter before approaching the second issue as I am not sure it is even possible to do it safely or accurately, without abandoning the use of an adapter view entirely.

这篇关于Android:ImageView的findViewById(自定义适配器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆