ListView中的较大图像

时间:2014-11-16 04:13:37

标签: android listview android-listview scroll android-volley

在我目前的项目中,我需要在ListView中加载一些带有标题和一些信息的大图像。所以我开始使用 Volley 从服务器加载数据。 ListView UI与Android google plus app几乎相同。通过 Volley 帖子,我能够正确加载图片并显示它们非常好。但问题是当我向下滚动列表视图时,它会影响我的滚动。
我上传了video,让您了解我的问题。
注意 - 小缩略图正常工作

以下是适配器

中的 ViewHolder getView 方法
public class ViewHolder{
    TextView title;
    TextView desc;
    NetworkImageView thumbnail;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = null;
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    if (v == null) {
        v = inflater.inflate(R.layout.photo_list_item, null);
    }
    ViewHolder holder = new ViewHolder();


    holder.title = (TextView) v.findViewById(R.id.txt_photo_title);
    holder.desc = (TextView) v.findViewById(R.id.txt_photo_description);
    holder.thumbnail = (NetworkImageView) v.findViewById(R.id.img_photo_thumbnail);
    v.setTag(holder);

    Photo photo = this.getItem(position);

    holder.title.setText(photo.getPhoto_title());
    holder.desc.setText(photo.getPhoto_desc());

    if (mImageLoader == null) {
        mImageLoader = VolleyController.getInstance().getImageLoader();
    }

    holder.thumbnail.setImageUrl(photo.getPhoto_url(), mImageLoader);       
    return v;
}

我的LruCache

public class MBitmapCache extends LruCache<String, Bitmap> implements ImageCache {

    public MBitmapCache(){
        this(getDefaultCacheSize());
    }

    public MBitmapCache(int maxSize) {
        super(maxSize);
        // TODO Auto-generated constructor stub
    }

    public static int getDefaultCacheSize(){
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        final int cacheSize = maxMemory / 8;

        return cacheSize;
    }

    @Override
    protected int sizeOf(String key, Bitmap value) {
        // TODO Auto-generated method stub
        return value.getRowBytes() * value.getHeight() / 1024;
    }

    @Override
    public Bitmap getBitmap(String key) {
        // TODO Auto-generated method stub
        return get(key);
    }

    @Override
    public void putBitmap(String key, Bitmap value) {
        // TODO Auto-generated method stub
        put(key, value);
    }

}

我的排球控制器

public class VolleyController extends Application{

    private static final String TAG = VolleyController.class.getSimpleName();
    private static VolleyController mClassInstace;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;


    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        mClassInstace = this;
    }

    public static synchronized VolleyController getInstance(){
        return mClassInstace;
    }

    public RequestQueue getRequestQueue(){
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }
        return mRequestQueue;
    }

    public ImageLoader getImageLoader(){
        getRequestQueue();
        if (mImageLoader == null) {
            mImageLoader =  new ImageLoader(this.mRequestQueue, new MBitmapCache());
        }
        return mImageLoader;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // TODO Auto-generated method stub
        req.setTag( TextUtils.isEmpty(tag)? TAG : tag );
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req){
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void CancelPendingRequests(String tag){
        if (!TextUtils.isEmpty(tag)) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先:您对ViewHolder的处理是错误的,您执行了setTag但是当 convertView 不为空时,您永远不会执行getTag

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

    if(convertView == null) {
        convertView = getLayoutInflater().inflate(R.layout.layout_list_item, null);

        holder = new ViewHolder();
        holder.title = (TextView) convertView.findViewById(R.id.txt_photo_title);
        holder.desc = (TextView) convertView.findViewById(R.id.txt_photo_description);
        holder.thumbnail = (NetworkImageView) convertView.findViewById(R.id.img_photo_thumbnail);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    ...

    return convertView;
}

可以尽可能地保存性能,您可以查看google tutorios了解详细信息。

第二:看来你的ImageLoader没有使用内存缓存,请发布初始化Volley RequestQueueImageLoader的代码,确保你的ImageLoader使用BitmapCache。