Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

I have recyclerView which contains some text view and 2 image view.

I would like to do a task when user clicks on each row and another task when user clicks on each of image views .. but I can only handle one of them at the same time.

Now if I set recylerview.addOnItemTouchListener I can't handle images views onclicks with setting on clicklistener for them in the adapter because when user clicks on image view the row's click listener will trigger.

Below code is my class for handling recycler 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 inside 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

like image 442
hamid reza gholami Avatar asked Sep 17 '16 12:09

hamid reza gholami


1 Answers

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.

like image 114
Preetika Kaur Avatar answered Oct 27 '22 19:10

Preetika Kaur