Элементы GridView изменяются после прокрутки

У меня есть girdview, который отображает изображение и текст, при прокрутке gridview порядок изображений меняется, текст остается правильным, но изображения меняются. Я использовал шаблон Viewholder m, но все еще имею ту же проблему. Ниже приведен мой код:

@Override
public View getView(int position, View v, ViewGroup parent) {
    // Keeps reference to avoid future findViewById()
    final ViewHolder viewHolder;
    if (v == null) {
        LayoutInflater li = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.row_recipe, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.tvRecipeName = (TextView) v.findViewById(R.id.txtUserName);
        viewHolder.ivRecipe = (ImageView) v.findViewById(R.id.ivRecipe);
        viewHolder.tvLikes = (TextView) v.findViewById(R.id.tvLikes);
        viewHolder.loader = (ProgressBar) v.findViewById(R.id.loader);

        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }

    final Recipe recipe = values.get(position);
    if (recipe != null) {
        viewHolder.tvRecipeName.setText(recipe.name);
        viewHolder.tvLikes.setText(String.valueOf(recipe.likesCount));


        // Thread to load image of the recipe
        Photo mainPhoto = recipe.getMainPhoto();
        if (mainPhoto != null && mainPhoto.name != null
                && !mainPhoto.name.equals("null")) {
            mainPhoto.format = CompressFormat.JPEG;
            mainPhoto.quality = 80;
            mainPhoto.sample = true;

            // Calculate inSampleSize
            Display display = mActivity.getWindowManager()
                    .getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            int height = size.y;
            int reqWidth = width / 2;
            double widthHeightRatio = (int) Math.ceil(height / width);
            int reqHeight = (int) Math.ceil(reqWidth * widthHeightRatio);

            mainPhoto.width = reqWidth;
            mainPhoto.height = reqHeight;

            // setup a listener to just change the one image view instead of
            // calling notifyDataSetChanged which redraws the entire list
            // view screen.

            mainPhoto.listener = new ImageCacheListener() {
                @Override
                public void onSuccess(Bitmap bitmap, Photo photo) {
                    super.onSuccess(bitmap, photo);
                    viewHolder.ivRecipe.setImageBitmap(bitmap);
                    viewHolder.loader.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onError(Throwable throwable, Photo photo) {
                    super.onError(throwable, photo);
                }
            };

            // get the friend image or the default
            Bitmap recipePhoto = remoteImageCache.getImage(mainPhoto);

            if (recipePhoto != null)
                viewHolder.ivRecipe.setImageBitmap(recipePhoto);
        } else {
            viewHolder.loader.setVisibility(View.INVISIBLE);
        }
    }
    return v;
}

static class ViewHolder {
    TextView tvLikes;
    ProgressBar loader;
    ImageView ivRecipe;
    TextView tvRecipeName;
}

person Adham    schedule 03.11.2014    source источник


Ответы (2)


ваше представление перерабатывается, пока ваше растровое изображение обрабатывается в отдельном потоке, вам нужно убедиться, что представление изображения, которое изначально началось с этого процесса, остается тем же, когда оно завершено.

http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

person tyczj    schedule 03.11.2014

Лучшее решение, которое я могу предложить, это использовать библиотеку Volley (Easy and Best). Я уже использовал это в 2 проектах, работает как шарм.

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

person Rohit    schedule 03.11.2014