recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { View child = rv.findChildViewUnder(e.getX(), e.getY()); if (child != null) { // Highlight the clicked item child.setBackgroundColor(getResources().getColor(R.color.colorAccent)); } return false; } });
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { View child = rv.findChildViewUnder(e.getX(), e.getY()); if (child != null) { // Retrieve data from the clicked item int position = recyclerView.getChildAdapterPosition(child); MyData data = myDataAdapter.getData(position); // Do something with the data ... } return false; } });In this example, we are retrieving the data from the clicked item in a RecyclerView. We use the getChildAdapterPosition() method to get the position of the clicked item and then retrieve the data using a custom adapter. We can then perform any actions with the data. Package library: android.support.v7.widget