views:

76

answers:

2

I have the following layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <GridView
      android:id="@+id/grid"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_alignParentTop="true"
      android:numColumns="auto_fit"
      android:verticalSpacing="5dip"
      android:horizontalSpacing="2.5dip"
      android:columnWidth="100px"
      android:stretchMode="columnWidth"
      android:gravity="center"
      android:listSelector="@drawable/transparent"
      android:padding="5dip"
      android:scrollbarStyle="outsideOverlay"
      android:clickable="true" />

</RelativeLayout>

Now I use a custom Adapter that inflates my own views and sets them as the items in the gridview and set a listener on the clicks in the gridview.

GridView grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(new ProductAdapter(getApplicationContext(), products));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       ...
   }
});

The problem is that the listener is not firing if I press on one of the cells in the gridview. If I use the trackpad to navigate around in the grid and I press the trackpad the listener is firing. Could it be that there is some other view that is capturing the click? My custom view consists of a LinearLayout with an ImageView and a TextView i read that inflating a layout for the gridview caused a problem at some places.

How can I make the items of the grid clickable?

Edit There is no difference between inflating the layout in my adapter or just instantiating a single view and returning this for the cellview.

A: 
  GridView grid = (GridView)findViewById(R.id.grid);
  grid.setOnItemClickListener(mOnClickGridCell);

 OnItemClickListener mOnClickGridCell = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Do stuff
        }
 };
Alex Volovoy
could you tell me the diffidence between the methods?
pengwang
Is there a difference between declaring the adapter as a local variable first? This would be a very strange thing. I changed your code to make it look like your example and as expected it doesn't change the behaviour of the code.
Janusz
+1  A: 

Ok very very stupid error on my part. I reused another adapter that deactivated the views inside it.

For you all to know if you set this inside your Adapter

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return false;
}

The views will not be clickable. Just as it is intended to be ;)

Janusz