Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform onClickListener on ImageView in GridView

I have GridView where items are views with image (ImageView) and title (TextView). I'd like to perform onClick operation on this ImageView, but not on the whole GridView item. 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
      }
    });
  }
});

only the second click on ImageView will provide me to do operation. How can I do operation on ImageView by the first click on GridView?

like image 518
user2017548 Avatar asked Jul 11 '13 13:07

user2017548


2 Answers

When the user clicks on the gridview, then you add an onclicklistener to the imageview, that's why you receive only the second click. In the adaper, when you create the imageview, you have to write the image's onlicklistener

like image 83
jaimebl Avatar answered Sep 21 '22 21:09

jaimebl


Check : OnClickListener not working for first item in GridView

try :

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

like image 26
Tarsem Singh Avatar answered Sep 22 '22 21:09

Tarsem Singh