hamid reza gholami hamid reza gholami - 2 months ago 26
Android Question

how to handle both RecyclerView on item click listener for its rows and the views inside it

hello evry one i have recyclerView wich contains some text view and 2 image view i would like to do some task when user click on each row and another task when user click on each of image views .. but i only can handle one of them at the same time now if i set recylerview.addOnItemTouchListener i cant handle images views onclicks with setting on clicklistener for them in the adapter becase when user click on image view the rows click listener will trigger. below code is my
class for handeling recyler view items

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;

public interface OnItemClickListener {
public void onItemClick(View view, int position);

public void onLongItemClick(View view, int position);
}

GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}

@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && mListener != null) {
mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
}
}
});
}

@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}

@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }

@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}


and this is my holder adapter

public MyViewHolder(View view) {
super(view);
tv_word = (TextView) view.findViewById(R.id.tv_word_show);
tv_english = (TextView) view.findViewById(R.id.tv_english_show);
tv_translation = (TextView) view.findViewById(R.id.tv_tranlation_show);
tv_numbershow = (TextView) view.findViewById(R.id.tv_numershow);
fav = (ImageView) view.findViewById(R.id.iv_fav);
more = (ImageView) view.findViewById(R.id.iv_more);
fav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext,"show some text for test",Toast.LENGTH_LONG).show();
}
});


i appreciate any help for my problem

Answer

Create Interface in you adapter class.

 public void setClickListener(ClickListener clickListener) {
            this.clickListener = clickListener;
        }

        public interface ClickListener {
            void ItemClicked(View v, int position);
        }

Declare the object on top like this:

ClickListener clickListener;

holder.view.setOnclickListener(new OnclickListener( 
 @Override
        public void onClick(View v) {
            if (clickListener != null) {
                clickListener.ItemClicked(v, getAdapterPosition());
            }
        }
);

Implement this interface in class and intialize the clicklistener where you are setting adapter to recyclerview.

 adapter.setClickListener(this);

When you will implement this in class you will get following method:

 @Override
    public void ItemClicked(View v, int position) {

    }

Apply switch case from v.getId() you can handle as many click listeners you need to implement.