Anton Makov
Anton Makov

Reputation: 123

grid view scrolling issue

I had a list contains images, each time the user clicked on the image and then on the grid view the image will be displayed on the grid view, this part i already done and it`s working. The problem is when the user starting to scroll up/down, all the previous images on the grid starting to show in different places. My guess I have a problem with the getView function inside my adapter

public class GridAdapter extends BaseAdapter{

Context mContext;
public GridAdapter(Context con){
    mContext = con;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return Globals.obsImageMatrix.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup arg2) {

     if(convertView==null){
        LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.grid_image, null);
        Holder.image =(ImageView) convertView.findViewById(R.id.obsImage);
      //  Holder.image.setImageResource(R.drawable.blank);
     }
      Holder.image =(ImageView) convertView.findViewById(R.id.obsImage);
      Holder.image.setImageResource(Globals.obsImageMatrix.get(position));
    return convertView;
}



public static class Holder{
    public static ImageView image;
}

}

Globals.obsImageMatrix is a matrix that it`s size as the grid itself, that contain the images of the grid.

Upvotes: 2

Views: 5287

Answers (2)

Stark
Stark

Reputation: 2611

  1. findViewById should be done only when you are creating a new viewholder.
  2. use this as your LayoutInflater instead:

    Context mContext;
    
    Private LayoutInflater mInflater;
    
        public GridAdapter(Context con){
            mContext = con;
            mInflater = LayoutInflater.from(con);
    }
    

In your getView():

@Override
public View getView(final int position, View convertView, ViewGroup arg2) {

     ViewHolder holder;
     if(convertView==null){
        convertView = mInflater.inflate(R.layout.grid_image, null);                 
        holder = new ViewHolder();
        holder.image =(ImageView) convertView.findViewById(R.id.obsImage);
        convertView.setTag(holder);
     }else{
        holder = (ViewHolder)convertView.getTag(); 
     }

      holder.image.setImageResource(Globals.obsImageMatrix.get(position));
    return convertView;
}


static class Holder{
    ImageView image;
}

Upvotes: 0

AMerle
AMerle

Reputation: 4354

You do not use your ViewHolder correctly. Try this:

@Override
public View getView(final int position, View convertView, ViewGroup arg2) {

     ViewHolder holder = null;
     if(convertView==null){
        LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.grid_image, null);
        holder = new ViewHolder();
        holder.image =(ImageView) convertView.findViewById(R.id.obsImage);
        convertView.setTag(holder);
     }else{
        holder = (ViewHolder)convertView.getTag(); 
     }

      holder.image.setImageResource(Globals.obsImageMatrix.get(position));
    return convertView;
}

public static class Holder{
    public ImageView image;
}

You have to read more about viewHolders to understand this pattern. There is a lot of posts on SO and google.

Hope this will help you

Upvotes: 3

Related Questions