Listview上的通用图像加载器是滞后的

时间:2013-04-22 15:14:10

标签: android listview lag universal-image-loader

我已经确定我只有一个ImageLoader实例,所以我知道这不是问题,出于某种原因,它只会在显示从网络上加载的全新图像时滞后。因此我认为它与UI因为解码图像而断断续续的事实有关,但我认为Universal Image Loader是异步处理所有内容的。这是我对BaseAdapter的getView方法所拥有的。

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

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

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

    LinearLayout postItem = (LinearLayout) inflater.inflate(R.layout.column_post, parent, false);   

    String postThumbnailUrl = null;


    try {

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

    } catch (Exception e) {}        

    //grab the post view objects

    ImageView postThumbnailView = (ImageView)postItem.findViewById(R.id.thumbnail);

    if (!(postThumbnailUrl.equals("self") || postThumbnailUrl.equals("default") || postThumbnailUrl.equals("nsfw")))
        mImageLoader.displayImage(postThumbnailUrl, postThumbnailView);         


    return postItem;

}

1 个答案:

答案 0 :(得分:1)

我认为你的问题不是ImageLoader。事实上,您没有使用系统传回给您的convertView,因此您根本不回收视图,只是为列表的每一行充气。

尝试更改getView()方法以使用convertView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout postItem = convertView
    if(null == postItem){
        LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        postItem = (LinearLayout) inflater.inflate(R.layout.column_post, parent, false);   
    }

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

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

    } catch (Exception e) {}        

    //grab the post view objects
    ImageView postThumbnailView = (ImageView)postItem.findViewById(R.id.thumbnail);
    if (!(postThumbnailUrl.equals("self") || postThumbnailUrl.equals("default") || postThumbnailUrl.equals("nsfw")))
        mImageLoader.displayImage(postThumbnailUrl, postThumbnailView);         

    return postItem;

}