如何使用GridView适配器回收Bitmap?

时间:2015-03-10 09:00:06

标签: android gridview android-fragments bitmap android-bitmap

我的Android应用程序中有一个GridView。当我点击BACK按钮时,我想重新录制所有图像。我不知道怎么弄这个?

gridview.setAdapter(adapter);

这是我在适配器中的getView():

public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_addimage_grid,
                        parent, false);
                holder = new ViewHolder();
                holder.image = (ImageView) convertView
                        .findViewById(R.id.item_grida_image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if (position == Bimp.SelectBitmap.size()) {
                holder.image.setImageBitmap(BitmapFactory.decodeResource(
                        getResources(), R.drawable.pic_add));
                if (position == 3) {
                    holder.image.setVisibility(View.GONE);
                }
            } else {
                holder.image.setImageBitmap(Bimp.SelectBitmap.get(position)
                        .getBitmap());
            }

            return convertView;
        }

        public class ViewHolder {
            public ImageView image;
        }

当我点击此按钮时,我会转到另一个片段:

case R.id.title_btn_left:           
            Bimp.SelectBitmap.clear();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            SearchFragment searchFragment = new SearchFragment();
            ft.replace(R.id.fl_content, searchFragment);
            ft.commit();
            break;

现在,当我点击后退按钮时,我设置了 Bimp.SelectBitmap.clear(); 。但是,当我进入选择图像的片段时,它太慢并且无法响应 gridview.setOnItemClickListener()。我认为,因为当我去另一个片段时图像不是回忆。

1 个答案:

答案 0 :(得分:1)

setRecyclerListener(...)设置为GridView

gridview.setRecyclerListener(new RecyclerListener() {
    @Override
    public void onMovedToScrapHeap(View view) {
        //get Bitmap by 'view' and release it here         
    }
});

希望这有帮助!