Prechádzať zdrojové kódy

Add OnRecyclerItemClickListener

cooperku_kdanmobile 5 rokov pred
rodič
commit
f42b19c316

+ 55 - 0
reader/src/main/java/com/kdanmobile/reader/widget/drag/OnRecyclerItemClickListener.kt

@@ -0,0 +1,55 @@
+package com.kdanmobile.reader.widget.drag
+
+import android.support.v4.view.GestureDetectorCompat
+import android.support.v7.widget.RecyclerView
+import android.view.GestureDetector
+import android.view.MotionEvent
+import android.view.View
+
+/**
+ * @classname:OnRecyclerItemClickListener
+ * @author:luozhipeng
+ * @date:1/11/17 21:57
+ * @description:监控RecyclerView的点击事件,单击,长按
+ */
+abstract class OnRecyclerItemClickListener(private val recyclerView: RecyclerView) : RecyclerView.OnItemTouchListener {
+    private val mGestureDetector: GestureDetectorCompat
+
+    init {
+        mGestureDetector = GestureDetectorCompat(recyclerView.context, ItemTouchHelperGestureListener())
+    }
+
+    override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
+        mGestureDetector.onTouchEvent(e)
+        return false
+    }
+
+    override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {
+        mGestureDetector.onTouchEvent(e)
+    }
+
+    override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
+
+    private inner class ItemTouchHelperGestureListener : GestureDetector.SimpleOnGestureListener() {
+        override fun onSingleTapUp(e: MotionEvent): Boolean {
+            val child = recyclerView.findChildViewUnder(e.x, e.y)
+            if (child != null) {
+                val vh = recyclerView.getChildViewHolder(child)
+                onItemClick(vh)
+            }
+            return true
+        }
+
+        override fun onLongPress(e: MotionEvent) {
+            val child = recyclerView.findChildViewUnder(e.x, e.y)
+            if (child != null) {
+                val vh = recyclerView.getChildViewHolder(child)
+                onItemLongClick(vh)
+            }
+        }
+    }
+
+    abstract fun onItemClick(vh: RecyclerView.ViewHolder)
+
+    abstract fun onItemLongClick(vh: RecyclerView.ViewHolder)
+}