Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnItemClickListener Not Triggered on Android GridView

I have a Gridview filled by an Adapter which returns LinearLayouts each contains an ImageButton and TextView.

In the adapter I am binding an onClick and onLongClick event to the ImageButton.

I am trying to bind OnItemClickListener to the gridview but I don't know why that the onItemclicked never fired up.

It's my 6th hour without anything.

By the way; OnItemSelectListener working perfectly on the Grid.

I am checking if some piece of code accidentally handles the onItemClicked but couldn't catch yet.

I need help guys.

gridView = (GridView) layoutInflater.inflate(R.layout.gridview, null); gridView.setOnItemClickListener(new ItemClickListener()); .  . .  //inner handler class class ItemClickListener implements AdapterView.OnItemClickListener {     @Override     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {         Toast.makeText(mainActivity.getApplicationContext(),view + " clicked at pos " +                     i,Toast.LENGTH_SHORT).show();     } } 
like image 538
Olgun Kaya Avatar asked Nov 18 '11 14:11

Olgun Kaya


2 Answers

Do not use clickable objects in the grid. In that case Android cannot handle the click event of GridView.

Instead, use something to show a similar user interface view. Then handle that object's click actions.

Don't: put Button in the GridView to perform some click actions.

Do: put an ImageView instead of ImageButton and handle ImageView's click events.

like image 85
Olgun Kaya Avatar answered Sep 20 '22 05:09

Olgun Kaya


If you wants to use Button or ImageButton then you need to write these attributes in your xml code of the widgets.

android:focusable="false" android:focusableInTouchMode="false" 

Its works for me.

But in GridView, Try to avoid use of these widgets. You can use any other widgets in place of these (Like ImageView or any other).

like image 40
MPG Avatar answered Sep 22 '22 05:09

MPG