ListView在滚动时“重新加载”ImageView,如何阻止它执行此操作?

时间:2013-06-20 03:28:06

标签: android listview android-listview universal-image-loader

我有一个listview,它将预览图像加载到行中,但当我向下滚动一下并重新显示该行时,图像似乎重新加载,就像它必须下载并重新解码自身一样再次。我现在正在使用AQuery来加载图像,但我尝试过Novoda的Image Loader和Nostra的Universal Image Loader,以及ImageViews自己“重新加载”。

这是我的简化getView方法(图像插在底部):

@Override
public View getView(int position, View convertView, ViewGroup parent) {

FrameLayout postItem = (FrameLayout) convertView;
//final PostHolder holder;
if(null == postItem){
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    postItem = (FrameLayout) inflater.inflate(R.layout.column_post, parent, false);   
}

final AQuery aq = new AQuery(postItem);

    JSONObject thePost = null;
    try { 
        thePost = mPosts.getJSONObject(position).getJSONObject("data");
    } catch (Exception e) {
        System.out.println("errreoroer");
    }

    String postThumbnailUrl = null;
    String author = null;
    String domain = null;
    String subreddit = null;
    int votes = 0;
    int numComments = 0;
    long createdUtc = 0;

    final HashMap<String, Object> postData = new HashMap<String, Object>();

    try {

        postData.put("title", thePost.getString("title"));

        //parse thumbnail
        postThumbnailUrl = thePost.getString("thumbnail");  

        postData.put("url", thePost.getString("url"));

        postData.put("permalink", thePost.getString("permalink"));

        postData.put("is_self", thePost.getBoolean("is_self"));

        //whent it was made
        createdUtc = thePost.getLong("created_utc");

        //author
        author = thePost.getString("author");

        subreddit = thePost.getString("subreddit");

        domain = thePost.getString("domain");

        votes = thePost.getInt("ups") - thePost.getInt("downs");

        //num_comments
        numComments = thePost.getInt("num_comments");

    } catch (Exception e) {}    


    //gets the direct url to the image
    mSmartContent.getDirectMedia((String)postData.get("url"), new Callbacks() {

        @Override
        public void callback(Object obj) {

            Bundle cData = (Bundle)obj;

            if (cData.getString("type") == "pic") {

                aq.id(R.id.imagePreview).visibility(View.VISIBLE).image((String)cData.getString("url"));


            } else {

                aq.id(R.id.imagePreview).visibility(View.GONE);

            }


        }



    });


    return postItem;

}

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果不将图像保留在内存中,则必须重新加载(无论是从某些缓存还是从网络)。

您可以通过在视口出现之前预先加载(绑定)视图来避免出现这种情况,类似于ViewPager加载屏幕外的页面,但它与图像加载器无关。