Java tutorial
/** * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.gosuncn.core.ui.listener; import android.support.v4.view.GestureDetectorCompat; import android.support.v7.widget.RecyclerView; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; /** * RecyclerView?ViewOnItemClickListener? * ?ViewHolderheader?footerRecyclerView? * RecyclerView?Item */ public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener { private GestureDetectorCompat mGestureDetector; public RecyclerItemClickListener(final RecyclerView recyclerView) { mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(), new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); if (child != null) { int position = recyclerView.getChildAdapterPosition(child); if (position >= 0) { //swipeRefreshLayout?position-1 onItemClick(child, position); } } return true; } @Override public void onLongPress(MotionEvent e) { View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); if (child != null) { int position = recyclerView.getChildAdapterPosition(child); if (position >= 0) { onLongItemClick(child, position); } } } }); } @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent motionEvent) { mGestureDetector.onTouchEvent(motionEvent); return false; } @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { } @Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { } public void onItemClick(View view, int position) { } public void onLongItemClick(View view, int position) { } }