图像GridView不会失效

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图像GridView不会失效相关的知识,希望对你有一定的参考价值。

我一直在尝试更新我的图像GridView。 仅当我再次设置适配器时,图像才会更新。再次设置适配器使我的活动滚动到顶部,我试图避免这种情况。

这是一些代码:

private class LoadImagesTask extends AsyncTask<Object,Void, Object> {
    private ImageAdapter imageAdapter;
    private ArrayList<Integer> idsArray;
    @Override
    protected void onPreExecute() {   
        imageAdapter = new ImageAdapter(getApplicationContext());
        Bitmap bmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.locked_image);
        idsArray = dbHelper.getPicturesIdsByAlbumId(1);
        for(int i = 0;i<idsArray.size();i++){
            imageAdapter.bitmaps.add(bmap);
        }

        gridView.setAdapter(imageAdapter);
    }
    @Override
    protected Void doInBackground(Object... arg) {

        for(int i = 0;i<idsArray.size();i++){
            byte[] data = dbHelper.getPictureById(idsArray.get(i)).littlePicBytes;
            imageAdapter.bitmaps.set(i, BitmapFactory.decodeByteArray(data, 0, data.length));
            publishProgress(); //********** This line calls onProgressUpdate()  *********
        }
        return null;

    }
}

现在,此代码适用于更新视图:

@Override
protected void onProgressUpdate(Void... params) {
    gridView.setAdapter(imageAdapter);
}

但这不是:

@Override
protected void onProgressUpdate(Void... params) {
    imageAdapter.notifyDataSetChanged();
    gridView.invalidateViews();
}

可能是因为没有更新我的观点?谢谢!

编辑:ImageAdapter代码:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    public ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
    public ImageAdapter(Context c) {
        mContext = c;

    }

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

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View gridView;

            if (convertView == null) {

                gridView = new View(mContext);

                // get layout from mobile.xml
                gridView = inflater.inflate(R.layout.image_adapter, null);

                // set image based on selected text
                ImageView imageView = (ImageView) gridView
                        .findViewById(R.id.imageView);
                imageView.setImageBitmap(bitmaps.get(position));
            }
                else{ 
                    gridView = (View) convertView;
                }
            return gridView;
    }
}
答案

您的代码中有两个主要问题:

  1. 在doInBackground中,您更改适配器的数据,这不是线程安全操作,因为适配器可能会在您更改数据时从数据中读取。我在谈论这一行: imageAdapter.bitmaps.set(i, BitmapFactory.decodeByteArray(data, 0, data.length)); 相反,您应该仅在UI线程上更新其数据,例如在onProgressUpdate内部。
  2. 在getView中,您已经错误地实现了它。当convertView不为null时,您永远不会更新imageView的内容。应该是什么样的东西:
public View getView(int position, View convertView, ViewGroup parent) 
  {
  View rootView=convertView;
  ViewHolder holder;
  if (rootView != null) 
    holder=(ViewHolder) rootView.getTag();
  else
    {
    rootView = inflater.inflate(R.layout.image_adapter, null,false);
    holder = new ViewHolder();
    holder.imageView = (ImageView) rootView.findViewById(R.id.imageView);
    rootView.setTag(holder);
    }
  holder.imageView.setImageBitmap(getItem(position));
  return rootView;
  }


private Bitmap getItem(final int position)
  {
  return bitmaps.get(position); 
  }    

private static class ViewHolder
  {
  ImageView imageView;
  }

以上是关于图像GridView不会失效的主要内容,如果未能解决你的问题,请参考以下文章

片段中gridview的Android文本和图像适配器

如何在Android中加载带有动画的cardview GridView?

将所选图像从库中上传到gridview

GridView 中 order by desc 排序失效??

Android drawSelectorOnTop 与 GridView

在片段中在运行时更改方向时更改布局而不重新创建视图