0
votes

I have simple GridView with images. Each item in GridView has ImageView that shows the picture and another ImageView that becomes visible when image is favorited( white star).

When i scroll fast to the bottom of the GridView the Image that was favorited and selected on position 3 for instance is not selected and favorited anymore and instead some other item on the bottom becomes randomly selected and favorited. And then again when i scroll to top some other item becomes favorited and selected randomly.

But i noticed i have this problem mostly only with second and third item.

This is originaly selected and favorited item : This is originaly selected and favorited item

And this item gets randomly selected and favorited at bottom when scrolled to bottom

And this item gets randomly selected and favorited at bottom when scrolled to bottom:

My Adapter :

    public class ImageAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return imageUrls.length;
    }


    @Override
    public long getItemId(int position) {

        return 0;

    }

    public class ViewHolder {
        ImageView imageView;
        ProgressBar progressBar;
        ImageView imgFavorite;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;


        if (convertView == null) {


            convertView = getLayoutInflater().inflate(R.layout.item_grid_image,
                    parent, false);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.image);
            holder.progressBar = (ProgressBar) convertView
                    .findViewById(R.id.Itemprogress);
            holder.imgFavorite = (ImageView) convertView
                    .findViewById(R.id.selectorText);
            convertView.setTag(holder);

            inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);


            RelativeLayout relative = (RelativeLayout) convertView.findViewById(R.id.profileImageContainer);
            relative.getLayoutParams().height = width;

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        imageLoader.displayImage(imageUrls[position], holder.imageView,
                options, new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                        holder.progressBar.setProgress(0);
                        holder.progressBar.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onLoadingComplete(String imageUri,
                            View view, Bitmap loadedImage) {
                        holder.progressBar.setVisibility(View.GONE);

                    }
                }, null);


        if (profilePicList[position].isprofilepic && firstTime
                ) {
            currentPosition = position;
             DefaultImg = convertView;
             holder.imgFavorite.setVisibility(View.VISIBLE);
             firstTime = false;
        }


        return convertView;

    }

    @Override
    public Object getItem(int position) {
        return null;
    }

}

EDIT: added on clicklistener code where i change background of selected image

AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        if(mActionMode == null) {
            mActionMode = startActionMode(mActionModeCallback);
        }




        if (previousView != null) {

            ((ViewGroup) previousView).getChildAt(0).setAlpha(1.0f);
            ((ViewGroup) previousView).setBackgroundResource(0);

        }

        ((ViewGroup) view).setBackgroundResource(R.drawable.back);
        ((ViewGroup) view).getChildAt(0).setAlpha(0.3f);

        previousView = view;


    }

};
2

2 Answers

1
votes

Put else part, that might solve your problem. Gridview reuses each imageview when you scroll.

if (profilePicList[position].isprofilepic && firstTime
                ) {
            currentPosition = position;
             DefaultImg = convertView;
             holder.imgFavorite.setVisibility(View.VISIBLE); // or INVISIBLE
             firstTime = false;
 }else{
   holder.imgFavorite.setVisibility(View.GONE);
 }
1
votes

The adapters are built to reuse Views, when a View is scrolled so that is no longer visible, it can be used for one of the new Views appearing. This reused View is the convertView.

Add this bloc after your if condition (when you passe the imgFavorite to VISIBLE)

else 
{
   holder.imgFavorite.setVisibility(View.GONE);
   firstTime = true;
}