GridView在更新后随机播放视图

时间:2012-06-25 13:50:49

标签: android gridview android-gridview

是的,我已经看到了其他问题,但那里的答案对我没有帮助。

我的应用从我放置在GridView的网页上加载了图片。 以下是getViewonScrollStateChanged的代码:

@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {

    switch(scrollState) {
        case SCROLL_STATE_FLING :
        case SCROLL_STATE_TOUCH_SCROLL :
            downloader.stopAll();
            adapter.removeExecutor(productCategoriesExecutor);
        break;

        case SCROLL_STATE_IDLE :
            loadProductsChunk();
        break;
    }
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    ProductView productView = (ProductView)view;

    if(productView == null) {
        productView = new ProductView(context, downloader);
        productView.setLayoutParams(new GridView.LayoutParams(columnWidth, rowHeight));
    }

    JSONObject product = getItem(i);

    if(product != null) {
        try {
            Log.i("ololo", String.format("thm: %d ", i) + product.getString("thumbnail"));

            productView.setNewPrice(product.getInt("price"));
            productView.setOldPrice(product.getInt("oldPrice"));
            productView.showThumbnail(product.getString("thumbnail"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    else {
        productView.setNewPrice(0);
        productView.setOldPrice(0);
        productView.showThumbnail(null);
    }
    return productView;
}

还有GridView

<GridView
        android:id="@+id/productsGrid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:verticalSpacing="@dimen/products_list_grid_spacing"
        android:horizontalSpacing="@dimen/products_list_grid_spacing"
        android:stretchMode="columnWidth"
        android:scrollingCache="false"
        android:alwaysDrawnWithCache="false"
    />

如您所见,ProductView接收链接,然后加载(或从缓存中获取)图像。 刷新是在没有触摸的情况下执行,现在滚动都不活动。

public void displayRemoteImage(RemoteImage remoteImage) {
    if(this.remoteImage != null) {
        Log.i("ololo", this.remoteImage.getUrl() + remoteImage.getUrl());
    }

    if(this.remoteImage == null || !this.remoteImage.getUrl().equals(remoteImage.getUrl())) {
        this.remoteImage = remoteImage;
        this.remoteImage.setDelegate(this);
    }
}

Okay

但如果我在哪里滚动并返回(使用OTHER PICS),它们会闪烁! 好的,这不是一个大问题 让我们再加载一行

enter image description here

什么是HELL? 而且 - 是的 - 如果我滚动到哪里然后返回将会有需要的图片(并且没有重新加载)。

好吧,也许我们是输家并搞砸了我们的下载?让我们简单地关闭刷新图片:

enter image description here

哈哈。搞砸你们,我要回家了。

以下是更新日志:

06-25 17:26:09.759: INFO/ololo(6613): viewId: 0, should show the link: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:09.779: INFO/ololo(6613): original link: http://mysite//img/products/41/41230/41230thm.jpg; should show: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:09.779: INFO/ololo(6613): viewId: 0, should show the link: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:09.859: INFO/ololo(6613): original link: http://mysite//img/products/3/3031/3031thm.jpg; should show: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:09.869: INFO/ololo(6613): viewId: 0, should show the link: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:09.889: INFO/ololo(6613): original link: http://mysite//img/products/3/3031/3031thm.jpg; should show: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:09.889: INFO/ololo(6613): viewId: 0, should show the link: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:09.909: INFO/ololo(6613): original link: http://mysite//img/products/40/40963/40963thm.jpg; should show: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:09.919: INFO/ololo(6613): viewId: 1, should show the link: http://mysite//img/products/40/40748/40748thm.jpg
06-25 17:26:09.939: INFO/ololo(6613): original link: http://mysite//img/products/40/40940/40940thm.jpg; should show: http://mysite//img/products/40/40748/40748thm.jpg
06-25 17:26:09.939: INFO/ololo(6613): viewId: 2, should show the link: http://mysite//img/products/40/40771/40771thm.jpg
06-25 17:26:09.999: INFO/ololo(6613): original link: http://mysite//img/products/40/40934/40934thm.jpg; should show: http://mysite//img/products/40/40771/40771thm.jpg
06-25 17:26:09.999: INFO/ololo(6613): viewId: 3, should show the link: http://mysite//img/products/40/40790/40790thm.jpg
06-25 17:26:10.009: INFO/ololo(6613): original link: http://mysite//img/products/40/40914/40914thm.jpg; should show: http://mysite//img/products/40/40790/40790thm.jpg
06-25 17:26:10.009: INFO/ololo(6613): viewId: 4, should show the link: http://mysite//img/products/40/40913/40913thm.jpg
06-25 17:26:10.029: INFO/ololo(6613): original link: http://mysite//img/products/40/40913/40913thm.jpg; should show: http://mysite//img/products/40/40913/40913thm.jpg
06-25 17:26:10.029: INFO/ololo(6613): viewId: 5, should show the link: http://mysite//img/products/40/40914/40914thm.jpg
06-25 17:26:10.049: INFO/ololo(6613): original link: http://mysite//img/products/40/40790/40790thm.jpg; should show: http://mysite//img/products/40/40914/40914thm.jpg
06-25 17:26:10.049: INFO/ololo(6613): viewId: 6, should show the link: http://mysite//img/products/40/40934/40934thm.jpg
06-25 17:26:10.069: INFO/ololo(6613): original link: http://mysite//img/products/40/40771/40771thm.jpg; should show: http://mysite//img/products/40/40934/40934thm.jpg
06-25 17:26:10.069: INFO/ololo(6613): viewId: 7, should show the link: http://mysite//img/products/40/40940/40940thm.jpg
06-25 17:26:10.089: INFO/ololo(6613): original link: http://mysite//img/products/40/40748/40748thm.jpg; should show: http://mysite//img/products/40/40940/40940thm.jpg
06-25 17:26:10.099: INFO/ololo(6613): viewId: 8, should show the link: http://mysite//img/products/40/40963/40963thm.jpg
06-25 17:26:10.159: INFO/ololo(6613): original link: http://mysite//img/products/3/3031/3031thm.jpg; should show: http://mysite//img/products/40/40963/40963thm.jpg
06-25 17:26:10.330: INFO/ololo(6613): viewId: 0, should show the link: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:10.340: INFO/ololo(6613): original link: http://mysite//img/products/3/3031/3031thm.jpg; should show: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:10.340: INFO/ololo(6613): viewId: 0, should show the link: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:10.390: INFO/ololo(6613): original link: http://mysite//img/products/3/3031/3031thm.jpg; should show: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:10.390: INFO/ololo(6613): viewId: 0, should show the link: http://mysite//img/products/3/3031/3031thm.jpg
06-25 17:26:10.410: INFO/ololo(6613): original link: http://mysite//img/products/3/3031/3031thm.jpg; should show: http://mysite//img/products/3/3031/3031thm.jpg

更新

这发生在HTC Explorer(2.3.3)和HTC Nexus One(2.3.6)上,但三星Galaxy Tab 10.1(3.2)上没有发生。

2 个答案:

答案 0 :(得分:0)

它来自BaseAdapter的内在实现。它似乎将视图存储在堆栈中或类似的东西,因此在屏幕更新后它以相反的顺序放置视图。解决方案是尽可能加快数据刷新速度。请记住,您不应该使用异步数据刷新:它会导致闪烁。

答案 1 :(得分:0)

使用ViewHolder模式。 以及下载和缓存的ImageManager。