使用静态函数有哪些陷阱?就像这段 Android 代码一样

发布于 2024-09-25 17:57:02 字数 1281 浏览 8 评论 0原文

我在 这个示例 用于下载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若沐 2024-10-02 17:57:02

我在本示例的 getView() 方法中使用静态函数来下载 ImageView 的源代码。

我在那篇博文中没有看到静态方法。

因为我经历过,在某些情况下(当我滚动得非常快时)图像会混淆。

这与静态方法无关,而与应用图像有关。行被回收。因此,如果下载图像花费的时间太长,则可能不再需要该图像 - ImageView 应该显示其他图像。解决此问题的一种方法是将 setTag()ImageView 所需的图像 URL 粘贴到 ImageView 本身上。下载完成后,在将下载的图像放入 ImageView 之前,调用 getTag() 并比较 URL。如果标记中的 URL 与下载的 URL 不同,请不要更新 ImageView,因为这将导致错误的图像。

I use in the getView()-Method of this example a static function to download the source of an ImageView.

I do not see a static method anywhere in that blog post.

Because I experienced, that in some cases (when I scroll really fast) the Images get mixed up.

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 an ImageView in setTag() on the ImageView itself. When the download is complete, before putting the downloaded image in the ImageView, call getTag() and compare URLs. If the URL in the tag is different than the URL that was downloaded, do not update the ImageView, since that will be for the wrong image.

蓝礼 2024-10-02 17:57:02

我通过不重用渲染器(这听起来确实比实际更痛苦)来解决这个问题,而是使用带有 WeakReference 对象。这使您的列表速度更快,并防止您在渲染器上设置现在用于其他数据的图像,同时在内存不足时让 GC 有机会删除未使用的列表项。

public View getView(int position, View convertView, ViewGroup parent) {
    Renderer result = null;
    WeakReference<Renderer> wr = (WeakReference<Renderer>) _renderers[position];
    if (ref != null)
        result = wr.get();

    if (result == null) {
        result = new Renderer(_context);
        // set the texts here and start loading your images
        _renderers[position] = new WeakReference<Renderer>(result);
    }
    return result;
}

您必须将 _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.

public View getView(int position, View convertView, ViewGroup parent) {
    Renderer result = null;
    WeakReference<Renderer> wr = (WeakReference<Renderer>) _renderers[position];
    if (ref != null)
        result = wr.get();

    if (result == null) {
        result = new Renderer(_context);
        // set the texts here and start loading your images
        _renderers[position] = new WeakReference<Renderer>(result);
    }
    return result;
}

You have to cast _renderers[position] to WeakReference, because java does not support arrays with generics, so _renderers is an array of Objects

樱花坊 2024-10-02 17:57:02

如果静态函数没有副作用,那么使用它应该是完全安全的。根据您对该函数的描述,它似乎确实有副作用,因此您需要确保从不同位置调用该函数不会导致任何冲突。如果没有看到这个函数,我真的无法告诉你更多的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文