简体   繁体   中英

Images in gridview change their position while scrolling

Hi i'm having this kind of problem, when scrolling imageviews change their positions and their background images. I saw other answers on this topic on this site, but non of them helped me.

Like this once: grid view scrolling issue GridView scrolling problem on Android GridView elements changes their place dynamically when scroll screen

and many others...but they don't solve my problem.

Important thing is that i don't use custom layout for gridview or gridview items(imageViews).I create them programmatically. This is very important to me so if someone know the answer pls help me...Thanks.

public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView==null){
        imageView = new ImageView(ctx);
    } else {
        imageView = (ImageView) convertView;
    }
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
        imageView.setBackgroundResource(tmp[position]);
        imageView.setImageResource(blank);

    return imageView;
}

Its a simple method. Adapters recycle each and every view while scrolling. So we just need to create the ones which are being re-used after the first time. Holders help to avoid expensive calls like findViewById and re-use the items by just getting the old ones and changing its properties.

One point you need to keep in mind is that we need to hold the images which are to be displayed in any one of the containers like array[] or List and reset each time before returning the view, or else it will display the previously last recycled view.

public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

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

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_layout, null);
                holder = new ViewHolder();
                holder.cover = (ImageView) convertView
                        .findViewById(R.id.item_cover);
                convertView.setTag(holder);

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

            }

        holder.cover.setBackgroundResource(tmp[position]);

        return convertView ;
    }

    static class ViewHolder {
        ImageView cover;
    }

try this

 class Holder {
     ImageView imageView;
 }

 public View getView(int position, View convertView, ViewGroup parent) {
 Holder holder;
if(holder.imageView==null){
    holder=new Holder();
    holder.imageView = new ImageView(ctx);
    holder.imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
    holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    holder.imageView.setPadding(8, 8, 8, 8);
    imageView.setTag(holder);
 } else {
    holder= (Holder) imageView.getTag();
 }

     holder.imageView.setBackgroundResource(tmp[position]);
     holder.imageView.setImageResource(blank);

 return imageView;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM