OnItemClickListener Not Triggered on Android GridView
Asked Answered
W

6

32

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();
    }
}
Wish answered 18/11, 2011 at 14:0 Comment(7)
Is there any exception in LogCat?Liberty
no. since I didnt catch any exception I would of see the application crashed. wouldn't I ??Wish
I don't see anything wrong with this code, maybe the problem is somewhere else.Deboradeborah
I couldn't figure it out as well. I saw lot's of messages around about the onITemClickHandler. That's why I wanted to write here for help. What is funny is; It works for OnItemSelectHandler :)Wish
Android doesn't like it when you place a clickable item within another "clickable" item (e.g. the grid item). I'd take out the ImageButton and see if that gets the onClickListener firing. If that's the problem, there are ways to get both listeners to work.Stylite
@dmon; thank you for your great help. that really worked it out. The solution is do not put a clickable object in the grid. You can handle the click event of other objects but not a default clickable object. In my case; I have to show user an image and also that image needs to be clicked. So I first choose to use ImageButton But now I am using the ImaveViews and seems working slightly.Wish
@dmon; can you tell more about the ways for get both listeners works ? Is it problemmatic for my way of solution ? that using ImageView instead of ImageButton.Wish
W
84

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.

Wish answered 21/11, 2011 at 7:33 Comment(2)
For example, in a subclassed AdapterView within a subclass of GridView: @Override public View getView(final int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(getContext()); imageView.setOnClickListener(new OnClickListener() { @Override onClick(View v) { if (getOnItemClickListener() != null) { getOnItemClickListener().onItemClick(MyGridView.this, v, position, getItemId(position)); } return imageView; }Gobbledegook
#36475107 Can you please look at this similar queston?Lunkhead
S
18

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

Scorch answered 21/11, 2014 at 8:26 Comment(3)
Oddly enough, this works and still lets you tap and trigger the button if you set an onclicklistener for it, but lets you click the grid item if you tap anywhere else, as desired.Staciastacie
I had to put that both for children and for parent views. Including layouts and "non-clickable" views. Weird, isn't it?Hautrhin
great answer. Thanks a lot MPG!Dicker
I
4

Also make sure, that your ListAdpter returns true for

public boolean isEnabled(int _position)

for the position you want to click.

Iapetus answered 28/12, 2012 at 20:52 Comment(0)
B
2

Hey guyz finally got a solution...

what we were doing is directly accessing the Layout inside the GridView, so the onItemClickListener finds it confusing to access the item.

So the solution is to apply the onClickListener inside the Adapter (i.e. normally ArrayAdapter)

so what i m trying to say is:

public View getView(int position, View convertView, ViewGroup parent) {

            //Here row is a view and we can set OnClickListener on this
    final View row;
    ViewHolder holder = null;

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        //Here we inflate the layout to view (linear in my case)
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.imageTitle = (TextView) row.findViewById(R.id.text);
        holder.image = (ImageView) row.findViewById(R.id.image);
        row.setTag(holder);
    } else {
        row = convertView;
        holder = (ViewHolder) row.getTag();
    }

    ImageItem item = data.get(position);
    holder.imageTitle.setText(item.getTitle());
    holder.image.setImageBitmap(item.getImage());

    //Now get the id or whatever needed
    row.setId(position);
    // Now set the onClickListener
    row.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "Clicked" + row.getId() + "!!",
                    Toast.LENGTH_SHORT).show();
        }
    });
    return row;
}
Burnaby answered 22/1, 2014 at 11:28 Comment(0)
D
1

Try to set

    android:clickable="false"
    android:focusable="false"
Distortion answered 22/4, 2016 at 9:56 Comment(0)
B
1

I meet same problem too, because of several reasons. So, here's my tips:

  1. Extend BaseAdapter for your adapter;
  2. Use OnClickListener inside the getView in adapter instead setting OnItemClickListener for GridView;
  3. Avoid setting LayoutParams multiple times;
  4. Check if position = 0, don't use convertView, inflate new View;
  5. Set OnClickListener not only for parent View, but for any child View, if any;
  6. Make all your Views clickable.

I just tested it on 4 devices, and this solution works as expected. Hope, it will help in your case. Correct me, if I made something wrong.

Layout code XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="#273238"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:padding="1dp">

<ImageView
    android:id="@+id/open_image_item_imageview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/loh"
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:scaleType="centerCrop"/>
<TextView
    android:id="@+id/open_image_item_textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_gravity="bottom"
    android:padding="4dp"
    android:textSize="10sp"
    android:ellipsize="start"
    android:background="#55000000"
    android:textColor="#FFFFFF"
    android:text="image name"/>

</FrameLayout>

Adapter code Java:

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        if(convertView != null && position != 0)
            view = convertView;
        else{
            view = LayoutInflater.from(getContext()).inflate(R.layout.open_image_item_layout, null, false);
            view.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, size));
        }

        TextView textView = (TextView)view.findViewById(R.id.open_image_item_textview);
        ImageView imageView = (ImageView)view.findViewById(R.id.open_image_item_imageview);

        ...

        View.OnClickListener onClickListener = getOnClickListener(files[position]);
        view.setOnClickListener(onClickListener);
        textView.setOnClickListener(onClickListener);
        imageView.setOnClickListener(onClickListener);

        return view;
    }
Benitobenjamen answered 5/11, 2017 at 21:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.