Created
September 21, 2017 02:38
-
-
Save PauloCeami/bb86416828432e7d4ba6dee3a77a5147 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.app.centralmetadevendasoficial.appvendas.Util; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.GestureDetector; | |
import android.view.MotionEvent; | |
import android.view.View; | |
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener { | |
private GestureDetector gestureDetector; | |
private ClickListener clickListener; | |
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) { | |
this.clickListener = clickListener; | |
gestureDetector = 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 && clickListener != null) { | |
clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child)); | |
} | |
} | |
}); | |
} | |
@Override | |
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { | |
View child = rv.findChildViewUnder(e.getX(), e.getY()); | |
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) { | |
clickListener.onClick(child, rv.getChildAdapterPosition(child)); | |
} | |
return false; | |
} | |
@Override | |
public void onTouchEvent(RecyclerView rv, MotionEvent e) { | |
} | |
@Override | |
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { | |
} | |
public interface ClickListener { | |
void onClick(View view, int position); | |
void onLongClick(View view, int position); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment