OnClickListener not working for first item in GridView

浪尽此生 提交于 2019-11-27 13:11:55
Rory Kelly

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.

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 .

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();
    }
});

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 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.

cosmincalistru

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

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

Archa

Please refer this link, it has set gridview background color to TRANSPARENT. It may help you.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!