Run onClickListener in ImageView in GridView

I have a GridView where the elements are representations with an image (ImageView) and a title (TextView). I would like to perform an onClick operation in this ImageView, but not in the entire GridView element. If I use this code:

gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ImageView imageView = (ImageView) view.findViewById(R.id.image); imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // some operation } }); } }); 

just a second click on ImageView will give me an operation. How can I perform an operation on an ImageView the first time I click on a GridView?

+4
source share
3 answers

Check : OnClickListener does not work for the first item in the GridView

try:

Instead of setting the onClickListener in the GridView, set it in the ImageView itself inside your GridAdapter inside the getView() method.

+2
source

When the user clicks on the gridview, you add onclicklistener to the image, so you only get a second click. In adaper, when you create an image, you need to record the onlicklistener image

+3
source

You must set onClickListener for each ImageView inside the Adapter getView() method. Also, set setItemsCanFocus(boolean itemsCanFocus) true in ListView.

0
source

Source: https://habr.com/ru/post/1490903/


All Articles