Recyclerview addOnItemTouchListener get whatsubview click inside row

I implemented the Recyclerview onclickListener from this solution . This solution is great for clicks on recycler pages. But I canโ€™t get which subtitle (for example: ImageView, Button) is clicked on the line.

mAttachmentRecyclerview.addOnItemTouchListener( new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { if (view.getId()==R.id.attachmnet_remove) { attachmentsList.remove(position); mAttachmentAdapter.notifyDataSetChanged(); attachmentCount--; } } } )); 

onItemClick(view,position) always returns the view id as -1

How do I track a click on a click?

+8
java android android-recyclerview onitemclicklistener
source share
4 answers

Below is a ViewHolder that contains two text views: title and description :

 public class CustomViewHolder extends RecyclerView.ViewHolder { private final OnViewClickListener mListener; public final TextView title; public final TextView description; public interface OnViewClickListener { void onViewClick(View v, int adapterPosition); } public CustomViewHolder(View itemView, OnViewClickListener mListener) { super(itemView); this.mListener = mListener; title = (TextView) itemView.findViewById(R.id.titleTextView); description = (TextView) itemView.findViewById(R.id.descriptionTextView); title.setOnClickListener(onClickListener); description.setOnClickListener(onClickListener); } private final View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View view) { mListener.onViewClick(view, getAdapterPosition()); } }; } 

Both of these subprojects have an OnClickListener attached to them, which calls the custom OnViewClickListener implementation that passes the View that was clicked, as well as the position RecyclerView on the adapter that received the click event.

Finally, use View.getId() to get the id with a click in the OnViewClickListener .

+7
source share

This answer is deprecated as it uses RecyclerView.getChildPosition(View) , which is deprecated. Try replacing it with RecyclerView.getChildAdapterPosition(View) .

If I were you, I would simply create listeners for each ViewHolder and its child views that you want to listen to. Honestly, I don't seem to see the benefits of using this more complex method that you referenced. In addition, this method will only inform you that the item has been clicked, nothing about the child views of the item.

Also, according to docs , the RecyclerView.OnItemTouchListener intended to be used when you want to detect a touch on an element while the list scrolls, not for regular or direct touches.

+5
source share

Set tags

You can set the tag in your views by initializing them through:

 view1.setTag(1); 

This will set the view tag to 1.

then in the onItemClick method:

 @Override public void onItemClick(View view, int position) { if(view.getTag()==1) { //do something } else { //do something else } 
+1
source share

Try adding onClickListeners to subviews.

0
source share

All Articles