I began to study the Fragments and faced with the fact that the listener had to hang on a button (in this case, this is a picture). I usually wrote through onClick (View V) and indicated a method in the markup, but I know that this does not work in Fragments. If I hang the listener anonymously, then everything works, but I want the Fragment of the implements View.OnClickListener and there was one method where by ID find out what the button is and then already process. In short, I’m not catching up on what to specify in image imageRight.setOnClickListener imageViewRight.setOnClickListener(!!!!!!);

The code below works but is not convenient.

 imageViewRight.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { } }); 

    1 answer 1

     public class MyFragment extends Fragment implements View.OnClickListener{} 

    and override the onClick () method; what is the problem?

    • I can not hang the listener on the button! imageViewLeft.setOnClickListener (this); - does not work, this is underlined in brackets - Eugene Zaychenko
    • Oh, it worked. Removed onClick () and re-registered implements View.OnClickListener {} and earned. It was visible the glitch or "programming is magic". I apologize, I thought there was not this, but something else should be indicated. - Eugene Zaychenko
    • one
      In the snippet, you call the activity method, so instead of this you need to write getActivity() - Tommy
    • This is yes, but the question was about binding the handler to the button, getActivity () is not an option. The question is closed. - Eugene Zaychenko