从重新加载图像停止排球

时间:2014-10-25 11:05:57

标签: android listview parse-platform android-volley

我正在创建一个类似于instagram的应用程序,因此从Internet上加载图像。加载的图像来自Parse.com的后端服务。我希望能够做的是在列表视图中加载大量项目,并防止列表视图在进入视图时重新加载图像。基本上我不希望图像刷新,我希望它们在滚动时保留在列表视图中。

OnCreate MainActivity

imageCache = new BitmapLruCache();
imageLoader = new ImageLoader(Volley.newRequestQueue(getApplicationContext()), imageCache);

PostsAdapter

public class PostsAdapter extends BaseAdapter implements StickyListHeadersAdapter {

    private Context context;
    public List<PostList> postList;

    public void add(PostList object,int position) {
        postList.add(position,object);
    }

    public PostsAdapter(Context context) {
        this.context=context;
        postList = new ArrayList<PostList>();
    }

    public int getCount() {
        return postList.size();
    }

    public PostList getItem(int position) {
        return postList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.posts_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
            }else{
            holder = (ViewHolder)convertView.getTag();
            holder.imageView.setImageUrl(null, imageLoader);
            }
        holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub

                    postList.get(position).liked = isChecked;

            }
        });
        holder.imageView.setOnClickListener(new DoubleClickListener(){

            @Override
            public void onSingleClick(View v) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onDoubleClick(View v) {
                // TODO Auto-generated method stub
                holder.checkbox.setChecked(true);
                postList.get(position).liked = true;
            }
        });

        holder.checkbox.setChecked(postList.get(position).liked);
        holder.imageView.setSoundEffectsEnabled(false);
        holder.imageView.setImageUrl(postList.get(position).postPicture, imageLoader);
       return convertView;
    }

    @Override
    public View getHeaderView(int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub
        HeaderViewHolder HVH;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.post_header_list, null);
            HVH = new HeaderViewHolder(convertView);
            convertView.setTag(HVH);
        }else{
            HVH = (HeaderViewHolder)convertView.getTag();
        } 
        CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
                Long.parseLong(postList.get(position).user),
                System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
        HVH.username.setText(timeAgo);
        return convertView;
    }

    @Override
    public long getHeaderId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    }
public abstract class DoubleClickListener implements OnClickListener {

    private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds

    long lastClickTime = 0;

    @Override
    public void onClick(View v) {
        long clickTime = System.currentTimeMillis();
        if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
            onDoubleClick(v);
        } else {
            onSingleClick(v);
        }
        lastClickTime = clickTime;
    }

    public abstract void onSingleClick(View v);
    public abstract void onDoubleClick(View v);
}
 static class ViewHolder{
 NetworkImageView imageView;
 CheckBox checkbox;
    ViewHolder(View v){
            imageView = (NetworkImageView )v.findViewById(R.id.Posts_Image);
            checkbox = (CheckBox)v.findViewById(R.id.liked);

1 个答案:

答案 0 :(得分:0)

在NetworkImageView的以下功能

@Override
protected void onDetachedFromWindow() {
    if (mImageContainer != null) {
        // If the view was bound to an image request, cancel it and clear
        // out the image from the view.
        mImageContainer.cancelRequest();
        setImageBitmap(null);
        // also clear out the container so we can reload the image if necessary.
        mImageContainer = null;
    }
    super.onDetachedFromWindow();
}

您会注意到,图像位图被设置为null,您可以在库中编辑它并按住您想要的长度保存图像... 但是,如果你这样做,HEAP尺寸会变大...... / 因此,在不需要时保留一些清理位图的逻辑

编辑: 用于增加文件存储中的缓存大小 您可以在DiskBasedCache文件中更改值

/** Default maximum disk usage in bytes. */
private static final int DEFAULT_DISK_USAGE_BYTES = 10 * 1024 * 1024;

您可以查看以下帖子了解允许的堆大小 Android heap size on different phones/devices and OS versions 你可以玩

 Runtime.getRuntime().maxMemory(); and 
 Runtime.getRuntime().freeMemory();

决定何时开始释放位图。而不是明白地说

 setImageBitmap(null);    // You can say if only 30% is free let's start freeing memory

 if (Runtime.getRuntime().freeMemory() < Runtime.getRuntime().freeMemory()* 0.3)
      setImageBitmap(null);