Created
June 25, 2017 08:41
-
-
Save CarolusX74/22afb5686978e03d4ff01ac1132726b8 to your computer and use it in GitHub Desktop.
Permite implementar onClickListener y LongClickListener en un Recycler View - ES
This file contains hidden or 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 com.cjtp.android.utils; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.GestureDetector; | |
import android.view.MotionEvent; | |
import android.view.View; | |
/** | |
* Created by Carlos Torres Pensa on 25/6/2017. | |
*/ | |
/** Permite implementar onClickListener y LongClickListener en un Recycler View **/ | |
public class MyRecyclerMultiListener implements RecyclerView.OnItemTouchListener { | |
private RecyclerListeners recyclerListeners; | |
private GestureDetector gestureDetector; | |
public MyRecyclerMultiListener(Context context, final RecyclerView recycleView, final RecyclerListeners recyclerListeners) { | |
this.recyclerListeners = recyclerListeners; | |
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { | |
@Override | |
public boolean onSingleTapUp(MotionEvent e) { | |
return true; | |
} | |
@Override | |
public void onLongPress(MotionEvent e) { | |
View child = recycleView.findChildViewUnder(e.getX(), e.getY()); | |
if (child != null && recyclerListeners != null) { | |
recyclerListeners.onRecyclerItemLongClick(child, recycleView.getChildAdapterPosition(child)); | |
} | |
} | |
}); | |
} | |
//---------------------------------------------------------------------------------------------- | |
@Override | |
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { | |
View child=rv.findChildViewUnder(e.getX(),e.getY()); | |
if(child!=null && recyclerListeners !=null && gestureDetector.onTouchEvent(e)){ | |
recyclerListeners.onRecyclerItemClick(child,rv.getChildAdapterPosition(child)); | |
} | |
return false; | |
} | |
@Override | |
public void onTouchEvent(RecyclerView rv, MotionEvent e) { | |
} | |
@Override | |
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { | |
} | |
//---------------------------------------------------------------------------------------------- | |
public interface RecyclerListeners { | |
public void onRecyclerItemClick(View view,int position); | |
public void onRecyclerItemLongClick(View view, int position); | |
} | |
/********************USO ********************/ | |
/** En el fragment implementar MyRecyclerMultiListener.RecyclerListeners | |
* | |
* Settear el listener de esta forma: | |
* recyclerView.addOnItemTouchListener(new MyRecyclerMultiListener(getContext(), recyclerView,this)); | |
* | |
* Implementar los métodos onRecyclerItemClick y onRecyclerItemLongClick | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment