滚动时Gridview错误的图像

时间:2014-07-31 10:26:19

标签: android gridview scroll recycle

我正在开发一款适用于平板电脑的Android应用程序,该应用程序将用作许多产品的目录(大约10 000个)。

我在gridview中显示它们,我只显示250个,如果我到达这些250的末尾,我会再加载250个。

我的问题是图片。我的应用程序似乎工作时,我不快速滚动,它是顺利的,没有问题,但它可以使用300或400 MB的RAM,所以我想我做得很糟糕。我听说我应该使用WeakReferences但我不确定如何使用它们......这是第一个“问题”。 (这不是很重要,因为用户可能只使用这个应用程序,如果它运行得很好......但是,如果我可以改进它,我会尝试)

这里的另一个大问题是,当我做类似的事情时:

  • 快速滚动到底部
  • 立即快速滚动到顶部

“底部”的一些图片显示在“顶部”的图片上(而不是临时性的,只有当我滚动并回到它们时它们才会再次改变。)

我真正了解的是:

  • griview重用视图的机制。如果我有10个可见的视图,当第一个看不见时,它们将用于下一个可见的5个视图。我不知道这个“回收商”有多少观点。
    • 这意味着当我滚动到底部时,我会看到5个视图,这些视图在5个下一个视图的位置中不可见,然后加载好视图。为了不产生这种不良影响,我使用setRecyclerListener()方法将图片设置为白色(背景颜色)。
  • 我没有“回收”/使用好方法来加载我的图片并回收它们但不知道如何让它更好
  • 我使用动画,我认为由于他们没有任何关于我的问题的链接
  • 我在AsyncTask中加载图片,这似乎有用,但是图片总是被加载(在快速滚动中)这不是一件好事
  • 当快速向上滚动然后快速向上滚动时,我会显示错误的图片。在其他情况下可能会发生这种情况。

    public class ProduitsAdapter extends ArrayAdapter<Article> {
        private final Activity context;
        private ArrayList<Article> params;
        private Resources res;
    
        static class ViewHolder {
            public TextView numero;
            public TextView design;
            public ImageView image;
            public ImageSwitcher imgSwitcher;
            public ImageView codeBarre;
            public TextView codeBarreText;
            public TextView colisage;
            public TextView col3;
            public TextView col4;
            public TextView col5;
            public String image_string = "";
            public boolean found = true;
            public ImageView nouveaute;
        }
    
        public ProduitsAdapter(Activity context, ArrayList<Article> params) {
            super(context, R.layout.list_single, params);
            res = Catalogue.activity.getResources();
            this.context = context;
            this.params = params;
        }
    
        public int getCount() {
            return params.size();
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView = convertView;
            if (rowView == null) {
                LayoutInflater inflater = context.getLayoutInflater();
                ViewHolder viewHolder = new ViewHolder();
    
                if (Catalogue.type_aff.equals("list")) {
                    rowView = inflater.inflate(R.layout.list_single, null);
                    viewHolder.numero = (TextView) rowView
                            .findViewById(R.id.numero);
                    viewHolder.design = (TextView) rowView
                            .findViewById(R.id.designation);
                    viewHolder.col3 = (TextView) rowView.findViewById(R.id.col3);
                    viewHolder.col4 = (TextView) rowView.findViewById(R.id.col4);
                    viewHolder.col5 = (TextView) rowView.findViewById(R.id.col5);
                } else {
                    rowView = inflater.inflate(R.layout.fragment_grid_item, null);
                    viewHolder.numero = (TextView) rowView
                            .findViewById(R.id.numero);
                    viewHolder.design = (TextView) rowView
                            .findViewById(R.id.designation);
                    viewHolder.colisage = (TextView) rowView
                            .findViewById(R.id.grid_colisage);
                    viewHolder.codeBarre = (ImageView) rowView
                            .findViewById(R.id.code_barre);
                    viewHolder.codeBarreText = (TextView) rowView
                            .findViewById(R.id.code_barre_text);
                    viewHolder.nouveaute = (ImageView) rowView
                            .findViewById(R.id.img_nouveaute);
                }
                viewHolder.image = (ImageView) rowView.findViewById(R.id.img);
                rowView.setTag(viewHolder);
            }
    
            ViewHolder holder = (ViewHolder) rowView.getTag();
            if (Catalogue.type_aff.equals("list")) {
                holder.col3.setText(params.get(position).getColonne_pref(3));
                holder.col4.setText(params.get(position).getColonne_pref(4));
                holder.col5.setText(params.get(position).getColonne_pref(5));
            } else {
                if (params.get(position).isNouveaute()) {
                    holder.nouveaute.setImageResource(R.drawable.nouveaute);
                    holder.nouveaute.setVisibility(ImageView.VISIBLE);
                } else
                    holder.nouveaute.setVisibility(ImageView.GONE);
                holder.colisage.setText(params.get(position).getColisage());
                if (ProduitsFragment.barcodeVisible) {
                    holder.codeBarreText.setText(params.get(position)
                            .getGencodNegoce());
                }
            }
            holder.numero.setText(params.get(position).getCodeArt());
            holder.design.setText(params.get(position).getDesignation_pref());
    
            // This is how I store my pictures, because finding one picture in a folder of 10 000 pictures takes too much time
            String pattern_deb = holder.numero.getText() + "";
            String chemin = Environment.getExternalStorageDirectory().getPath()
                    + res.getString(R.string.chemin_app_data)
                    + MainActivity.listDossiersSocietes
                            .get(MainActivity.societeSelected)
                    + res.getString(R.string.chemin_dossier_photos);
            for (int i = 0; i < pattern_deb.length() - 2; i++) {
                chemin += "/" + pattern_deb.charAt(i);
            }
            chemin += "/";
            File f = new File(chemin + params.get(position).getFichierPhoto());
            if (f.exists()) {
                holder.image_string = f.getAbsolutePath();
                new loadImagesFromFileThread(holder, position).execute();
                if (!Catalogue.type_aff.equals("list")
                        && ProduitsFragment.barcodeVisible)
                    new loadBarCodeThread(holder, position).execute();
            } else {
                holder.image.setImageResource(R.drawable.notfound300);
            }
            return rowView;
        }   
    
        private BitmapDrawable loadImagesFromFile(ViewHolder holder, int position) {
            holder.found = true;
            File bmp = new File(holder.image_string);
            if (bmp.exists() && !bmp.isDirectory()) {
                if (position < ProduitsFragment.gridView.getFirstVisiblePosition()
                        && position > ProduitsFragment.gridView
                                .getLastVisiblePosition()) {
                    holder.found = false;
                    return null;
                } else {
                    if (holder.image.getVisibility() == ImageView.VISIBLE) {
                        holder.found = false;
                        return null;
                    }
                }
    
                Bitmap myBitmap = BitmapFactory.decodeFile(bmp.getAbsolutePath());
                int width = myBitmap.getWidth();
                int height = myBitmap.getHeight();
                int newWidth = 200;
                int newHeight = 200;
                if (Catalogue.type_aff.equals("list")) {
                    newWidth = 40;
                    newHeight = 40;
                } else {
                    if (Catalogue.nbGrid == 5) {
                        newWidth = 200;
                        newHeight = 200;
                    }
                    if (Catalogue.nbGrid == 3) {
                        newWidth = 300;
                        newHeight = 300;
                    }
                }
                float scaleWidth = ((float) newWidth) / width;
                float scaleHeight = ((float) newHeight) / height;
                Matrix matrix = new Matrix();
                matrix.postScale(scaleWidth, scaleHeight);
                Bitmap resizedBitmap = Bitmap.createBitmap(myBitmap, 0, 0, width,
                        height, matrix, true);
                BitmapDrawable bmd = new BitmapDrawable(context.getResources(),
                        resizedBitmap);
                return bmd;
            } else {
                holder.found = false;
                return null;
            }
        }
    
        private class loadImagesFromFileThread extends
                AsyncTask<Void, Void, BitmapDrawable> {
            ViewHolder holder;
            int position;
            public loadImagesFromFileThread(ViewHolder holder, int position) {
                this.holder = holder;
                this.position = position;
                this.holder.image.setVisibility(ImageView.INVISIBLE);
            }
    
            @Override
            protected BitmapDrawable doInBackground(Void... params) {
                return loadImagesFromFile(holder, position);
            }
    
            @Override
            protected void onPostExecute(BitmapDrawable bmd) {
                if (ProduitsFragment.isNotScrolling) {
                    if (bmd != null) {
                        holder.image.setImageDrawable(bmd);
                        if (holder.image.getVisibility() != ImageView.VISIBLE
                                && !(position < ProduitsFragment.gridView
                                        .getFirstVisiblePosition() && position > ProduitsFragment.gridView
                                        .getLastVisiblePosition())) {
                            Animation fadeIn = new AlphaAnimation(0, 1);
                            fadeIn.setInterpolator(new DecelerateInterpolator());
                            fadeIn.setDuration(500);
                            fadeIn.setStartOffset(500);
                            holder.image.startAnimation(fadeIn);
                            holder.image.setVisibility(ImageView.VISIBLE);
                        }
                    } else {
                        if (position < ProduitsFragment.gridView
                                .getFirstVisiblePosition()
                                && position > ProduitsFragment.gridView
                                        .getLastVisiblePosition()) {
                            Log.e("DEBUG", "...");
                        } else {
                            if (holder.image.getVisibility() != ImageView.VISIBLE) {
                                if (!holder.found) {
                                    if (Catalogue.type_aff.equals("list")) {
                                        holder.image
                                                .setImageResource(R.drawable.notfound40);
                                    } else {
                                        if (Catalogue.nbGrid == 5) {
                                            holder.image
                                                    .setImageResource(R.drawable.notfound200);
                                        }
                                        if (Catalogue.nbGrid == 3) {
                                            holder.image
                                                    .setImageResource(R.drawable.notfound300);
                                        }
                                    }
                                    Animation fadeIn = new AlphaAnimation(0, 1);
                                    fadeIn.setInterpolator(new DecelerateInterpolator());
                                    fadeIn.setDuration(500);
                                    holder.image.startAnimation(fadeIn);
                                    holder.image.setVisibility(ImageView.VISIBLE);
                                } else {
                                    Animation fadeIn = new AlphaAnimation(0, 1);
                                    fadeIn.setInterpolator(new DecelerateInterpolator());
                                    fadeIn.setDuration(500);
                                    holder.image.startAnimation(fadeIn);
                                }
                                holder.image.setVisibility(ImageView.VISIBLE);
    
                            }
                        }
                    }
                }
            }
        }
    }
    

我很感激任何帮助来纠正错误图片的问题,如果有人能解释我如何使用一些代码正确加载它们(如果可能的话,不使用300 MB的RAM)那将是非常好的。

0 个答案:

没有答案