Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to handle the click event in android RecyclerView

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 923
    Comment on it

    Listview has the callbacks for single click and long click, but the new concept RecyclerView haven't such things like setOnItemClickListener and setOnItemLongClickListener.


    RecyclerView.OnItemTouchListener is the key of working with SingleClick and LongClick and Gesture detector will respond to single click when motion event with SingleTab or long click when motion event with long press by user. Below the few lines of code helps you to work with single click and long click in Recycler View.

    public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
        public interface OnItemClickListener {
            void onItemClick(View view, int position);
    
            void onItemLongClick(View view, int position);
        }
    
        private OnItemClickListener mListener;
    
        private 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 childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
    
                    if (childView != null && mListener != null) {
                        mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView));
                    }
                }
            });
        }
    
        @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 false;
        }
    
        @Override
        public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
        }
    
        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        }
    }

     

    With using addOnItemTouchListener will provide position and view as parameters for single click and long click in your activity.

    mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(YourActivity.this, mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
    
                @Override
                public void onItemClick(View view, int position) {
                    
            // Single item click
                    Toast.makeText(YourActivity.this, "Single click event",Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onItemLongClick(View view, int position) {
    
                    // Long item click
                    Toast.makeText(YourActivity.this, "Long click event",Toast.LENGTH_SHORT).show();
                }
    }));

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: