使用静态函数有哪些陷阱?就像这段 Android 代码一样
我在 这个示例 用于下载 ImageView 源的静态函数。稍后将包括线程。但是,我想知道在这种情况下如何节省静态函数的使用。
因为我经历过,在某些情况下(当我滚动得非常快时)图像会混淆。
/**
* Is called, when the ListAdapter requests a new ListItem, when scrolling. Returns a listItem (row)
*/
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Order o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
if (tt != null) {
tt.setText("Name: "+o.getOrderName()); }
//At this point I use a static function to download the bitmap to set it as source of an ImageView
}
return v;
}
I use in the getView()-Method of this example a static function to download the source of an ImageView. Later there will be threading included. However, I like to know in general how save the use of static function is in this case.
Because I experienced, that in some cases (when I scroll really fast) the Images get mixed up.
/**
* Is called, when the ListAdapter requests a new ListItem, when scrolling. Returns a listItem (row)
*/
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Order o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
if (tt != null) {
tt.setText("Name: "+o.getOrderName()); }
//At this point I use a static function to download the bitmap to set it as source of an ImageView
}
return v;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在那篇博文中没有看到静态方法。
这与静态方法无关,而与应用图像有关。行被回收。因此,如果下载图像花费的时间太长,则可能不再需要该图像 - ImageView 应该显示其他图像。解决此问题的一种方法是将
setTag()
中ImageView
所需的图像 URL 粘贴到ImageView
本身上。下载完成后,在将下载的图像放入ImageView
之前,调用getTag()
并比较 URL。如果标记中的 URL 与下载的 URL 不同,请不要更新ImageView
,因为这将导致错误的图像。I do not see a static method anywhere in that blog post.
That has nothing to do with the method being static, and everything to do with applying the images. Rows get recycled. Hence, if a download of an image takes too long, it may be that the image is no longer needed -- the
ImageView
should be showing some other image instead. One way to address this is to stick the URL of the image needed by anImageView
insetTag()
on theImageView
itself. When the download is complete, before putting the downloaded image in theImageView
, callgetTag()
and compare URLs. If the URL in the tag is different than the URL that was downloaded, do not update theImageView
, since that will be for the wrong image.我通过不重用渲染器(这听起来确实比实际更痛苦)来解决这个问题,而是使用带有 WeakReference 对象。这使您的列表速度更快,并防止您在渲染器上设置现在用于其他数据的图像,同时在内存不足时让 GC 有机会删除未使用的列表项。
您必须将 _renderers[position] 转换为 WeakReference,因为 java 不支持具有泛型的数组,因此 _renderers 是一个对象数组
I fixed this by not reusing the renderers (it really sounds more painfull than it is) but instead "caching" them using an array with WeakReference objects. This makes your list fast and prevents you from setting images on renderers that are now used for other data, while at the same time giving the GC a chance to remove the unused list items if you run low on memory.
You have to cast _renderers[position] to WeakReference, because java does not support arrays with generics, so _renderers is an array of Objects
如果静态函数没有副作用,那么使用它应该是完全安全的。根据您对该函数的描述,它似乎确实有副作用,因此您需要确保从不同位置调用该函数不会导致任何冲突。如果没有看到这个函数,我真的无法告诉你更多的事情。
If a static function has no side-effects, then it should be perfectly safe to use. Based on your description of the function, it does seem to have side effects, so you need to make sure that calling the function from different places doesn't cause any conflicts. I can't really tell you anything more without seeing the function.