RecyclerView item click helper class
Project: http://git-wip-us.apache.org/repos/asf/incubator-taverna-mobile/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-taverna-mobile/commit/704137b7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-taverna-mobile/tree/704137b7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-taverna-mobile/diff/704137b7 Branch: refs/heads/master Commit: 704137b751a73ca6b65f35f0b134567ee0a91f5a Parents: 40085c5 Author: Sagar <[email protected]> Authored: Fri May 6 16:55:15 2016 +0530 Committer: Sagar <[email protected]> Committed: Fri May 6 16:55:15 2016 +0530 ---------------------------------------------------------------------- .../ui/adapter/RecyclerItemClickListner.java | 121 +++++++++++++++++++ 1 file changed, 121 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-taverna-mobile/blob/704137b7/app/src/main/java/org/apache/taverna/mobile/ui/adapter/RecyclerItemClickListner.java ---------------------------------------------------------------------- diff --git a/app/src/main/java/org/apache/taverna/mobile/ui/adapter/RecyclerItemClickListner.java b/app/src/main/java/org/apache/taverna/mobile/ui/adapter/RecyclerItemClickListner.java new file mode 100644 index 0000000..d6fd5d1 --- /dev/null +++ b/app/src/main/java/org/apache/taverna/mobile/ui/adapter/RecyclerItemClickListner.java @@ -0,0 +1,121 @@ +package org.apache.taverna.mobile.ui.adapter; + +import android.content.Context; +import android.support.annotation.Nullable; +import android.support.v7.widget.RecyclerView; +import android.view.GestureDetector; +import android.view.MotionEvent; +import android.view.View; + +public class RecyclerItemClickListner implements RecyclerView.OnItemTouchListener { + + protected OnItemClickListener listener; + + private GestureDetector gestureDetector; + + @Nullable + private View childView; + + private int childViewPosition; + + public RecyclerItemClickListner(Context context, OnItemClickListener listener) { + this.gestureDetector = new GestureDetector(context, new GestureListener()); + this.listener = listener; + } + + @Override + public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent event) { + childView = view.findChildViewUnder(event.getX(), event.getY()); + childViewPosition = view.getChildAdapterPosition(childView); + + return childView != null && gestureDetector.onTouchEvent(event); + } + + @Override + public void onTouchEvent(RecyclerView view, MotionEvent event) { + // Not needed. + } + + @Override + public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { + + } + + /** + * A click listener for items. + */ + public interface OnItemClickListener { + + /** + * Called when an item is clicked. + * + * @param childView View of the item that was clicked. + * @param position Position of the item that was clicked. + */ + public void onItemClick(View childView, int position); + + /** + * Called when an item is long pressed. + * + * @param childView View of the item that was long pressed. + * @param position Position of the item that was long pressed. + */ + public void onItemLongPress(View childView, int position); + + } + + /** + * A simple click listener whose methods can be overridden one by one. + */ + public static abstract class SimpleOnItemClickListener implements OnItemClickListener { + + /** + * Called when an item is clicked. The default implementation is a no-op. + * + * @param childView View of the item that was clicked. + * @param position Position of the item that was clicked. + */ + public void onItemClick(View childView, int position) { + // Do nothing. + } + + /** + * Called when an item is long pressed. The default implementation is a no-op. + * + * @param childView View of the item that was long pressed. + * @param position Position of the item that was long pressed. + */ + public void onItemLongPress(View childView, int position) { + // Do nothing. + } + + } + + protected class GestureListener extends GestureDetector.SimpleOnGestureListener { + + @Override + public boolean onSingleTapUp(MotionEvent event) { + if (childView != null) { + listener.onItemClick(childView, childViewPosition); + } + + return true; + } + + @Override + public void onLongPress(MotionEvent event) { + if (childView != null) { + listener.onItemLongPress(childView, childViewPosition); + } + } + + @Override + public boolean onDown(MotionEvent event) { + // Best practice to always return true here. + // http://developer.android.com/training/gestures/detector.html#detect + return true; + } + + } + +} \ No newline at end of file
