OnClickListener not working for first item in GridView

前端 未结 7 1518
一向
一向 2020-12-03 02:54

I have a problem with creating a GridView-based calendar. Here is the Grid:

\"GridView

This

相关标签:
7条回答
  • 2020-12-03 03:43

    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.

    0 讨论(0)
  • 2020-12-03 03:44

    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.

    0 讨论(0)
  • 2020-12-03 03:45

    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

    0 讨论(0)
  • 2020-12-03 03:48

    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();
        }
    });
    
    0 讨论(0)
  • 2020-12-03 03:49

    Set onItemClickListener on the GridView object instead of setting onClickListeners on each button.

    And I found another question with your problem here. Bounty maybe?

    0 讨论(0)
  • 2020-12-03 03:51

    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.

    0 讨论(0)
提交回复
热议问题