I have a problem with creating a GridView-based calendar. Here is the Grid:
This
I had this same issue. The click event on the first item was significantly delayed. I was already using notifyDataSetInvalidated(). Using onItemClickListener() solved the issue, although I still don't understand why the onClick listener on the individual items has this behavior.
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.
After spending so many days, set layout params as follows..
if (view == null) {
// call the user's implementation
view = mInflater.inflate(mViewId, null);
holder = createHolder(view);
// we set the holder as tag
view.setTag(holder);
rlMainCell = (RelativeLayout)view.findViewById(R.id.rlMainCell);
rlMainCell.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) view.getResources().getDimension(R.dimen._128sdp)));
} else {
// get holder back...much faster than inflate
holder = (ViewHolder) view.getTag();
}
it works absolutely fine to me
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();
}
});
Set onItemClickListener on the GridView object instead of setting onClickListeners on each button.
And I found another question with your problem here. Bounty maybe?
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.