Previously, I used this scheme to click on an element entirely:

@Override public void onBindViewHolder(LanguageViewHolder personViewHolder, final int position) personViewHolder.languageName.setText(languages.get(position).name); ((View) personViewHolder.languageName.getParent()).setOnClickListener(view -> listener.onRVItemClick(position)); } 

Now I have slightly changed the structure and in the onClick method I only know how to click on the id:

 @Override public void onClick(View v) { switch (v.getId()) { case languages: if (listener != null) { listener.onItemClick(getAdapterPosition()); } break; } } 

I tried the switch to contact v.Parent () but I can’t get it to mind. The main idea is not only by id, but by the whole view where this id is located.

  • one
    It's not very clear what you need .. Perhaps something like this: holder.itemView.setOnClickListener(this) ? - JuriySPb ♦
  • The listener needs to be hung on each view for which you want to handle a click. - eugeneek
  • @eugeneek; in general, it seems like there’s no, you can contact getParent not to prescribe the same click on all the views that are in the same xml. And the author does it in the upper part of the code, so I understand the difficulty is the transfer of the same functionality to the onClick() method? - Morozov
  • @Morozov In the code, the listener just hangs on the parent, and not on the twist, inside that parent. If you do in the way that you propose, the code will be executed and for the view you clicked on for the parent. And to avoid this, we need additional checks. Do you think it's better to crutch through getParent() than to use the normal way? - eugeneek
  • I do not speak for each twist to do a separate listener. I only say that you need to hang a listener on each view that requires click processing. And then, in principle, there will be no problems with the definition of a view by id. - eugeneek

0