Search code examples
androidgridviewandroid-cursoradapter

How do I click a specific part of a single item in a GridView?


Every item in my GridView is a custom layout consisting of four ImageViews, each with their own ids, each of which has its image set by a custom CursorAdapter.

I'd like to have clicking each of the ImageViews in an item lead to a different function. I'd also like to have the id of the GridView item to be used in the function.

If I use gridview.setOnItemClickListener, it looks for clicks to the item as a whole. If I use android:onClick in the XML, it doesn't give me the id of the item.


Solution

  • Inside the getView method of custom adapter, set OnClickListener for each ImageView.

    imageView1 = rootView.findViewById(R.id.image1);
        imageView2 = rootView.findViewById(R.id.image2);
    
    imageView1.setOnClickListener(...
    imageView2.setOnClickListener(...