OnClickListener not working for first item in GridView

Use your OnClickListener() in your activity after .setAdapter() method, not in your adapter class.

gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
    Toast.makeText(GridViewActivity.this, "" + position,
             Toast.LENGTH_SHORT).show();
    }
});

Ok, I found the solution. The problem was these lines:

ViewHolder holder;
if (convertView == null)
{
    holder = new ViewHolder();
    LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.calendar_day_gridcell, parent, false);
    holder.gridCell = (Button) convertView.findViewById(R.id.calendar_day_gridcell_button);
    holder.multiDayEvent = (EventLengthView) convertView.findViewById(R.id.eventLengthView);
    convertView.setTag(holder);
}
else {
    holder = (ViewHolder) convertView.getTag();
}

When putting instatiating the gridCell button there, somehow, it mixes up the click listener of the first position in the adapter. I ended up fixing it by just instatiating the holder in every pass, instead of getting it by tag (which is better for performance, but oh well). Thanks everyone for the help.


For what it's worth, I had also this issue (first cell in the gridview having delayed onclick).

In my case, I was using a (lightly customized) gridviews with dynamic sets of imageviews that I would make visible/gone and redraw the gridview. All imageviews where prepared beforehand and stored in the adapter.

Initially, I used notifyDataSetChanged() on the adapter but this resulted in the delayed click problem for the first cell whenever the set was updated. I changed to just invalidating the gridview and now things are fine. Bear in mind that all my views are already created and stored in the adapter, its just the getview method that checks which ones are really visible.


I had the same problem.Keeping the setLayoutParams inside the if(view == null) clause worked out. You dont have to sacrifice view recyling in that way.

like :

 if(row == null){
   // inflate row
   row.setLayoutParams(params);
   //remaining code
 }else{
    holder = (ViewHolder)row.getTag();
 }
 // everything else

I don't know why it works but it worked for me. I also noticed that all codes regarding the same issue in stackoverflow had also used setLayoutParams outside the if clause. This is my first time so i dont know if i can post this everywhere. Hope it helped .

Source : Lot of trail and error .