package com.sw.inbound.view import android.content.Context import android.util.AttributeSet import android.view.MotionEvent import android.view.ViewConfiguration import androidx.core.view.ViewCompat import androidx.recyclerview.widget.RecyclerView import kotlin.math.abs class NestedRecyclerView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : RecyclerView(context, attrs, defStyleAttr) { private var lastY = 0f private var isDragging = false private var touchSlop = ViewConfiguration.get(context).scaledTouchSlop override fun onInterceptTouchEvent(e: MotionEvent): Boolean { when (e.action) { MotionEvent.ACTION_DOWN -> { lastY = e.y parent.requestDisallowInterceptTouchEvent(true) } MotionEvent.ACTION_MOVE -> { val dy = e.y - lastY if (!canScrollVertically(-1) && dy > 0) { parent.requestDisallowInterceptTouchEvent(false) return false } else if (!canScrollVertically(1) && dy < 0) { parent.requestDisallowInterceptTouchEvent(false) return false } } } return super.onInterceptTouchEvent(e) } override fun onTouchEvent(e: MotionEvent): Boolean { when (e.action) { MotionEvent.ACTION_MOVE -> { if (!isDragging && abs(e.y - lastY) > touchSlop) { isDragging = true } } MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { isDragging = false } } return super.onTouchEvent(e) } override fun dispatchTouchEvent(ev: MotionEvent): Boolean { when (ev.action) { MotionEvent.ACTION_DOWN -> { stopNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL) } } return super.dispatchTouchEvent(ev) } }