Skip to content

Instantly share code, notes, and snippets.

@vahid-m
Forked from KaNiShi/NestedCoordinatorLayout.kt
Created January 24, 2024 19:16
Show Gist options
  • Save vahid-m/dde7aa7af9c222a7be3e3994e635b010 to your computer and use it in GitHub Desktop.
Save vahid-m/dde7aa7af9c222a7be3e3994e635b010 to your computer and use it in GitHub Desktop.
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.NestedScrollingChild3
import androidx.core.view.NestedScrollingChildHelper
class NestedCoordinatorLayout @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : CoordinatorLayout(context, attrs, defStyleAttr), NestedScrollingChild3 {
private val nestedScrollingChildHelper: NestedScrollingChildHelper = NestedScrollingChildHelper(this)
init {
isNestedScrollingEnabled = true
}
override fun onStartNestedScroll(child: View, target: View, axes: Int, type: Int): Boolean =
super.onStartNestedScroll(child, target, axes, type).or(startNestedScroll(axes, type))
override fun onNestedFling(target: View, velocityX: Float, velocityY: Float, consumed: Boolean): Boolean =
super.onNestedFling(target, velocityX, velocityY, consumed).or(
dispatchNestedFling(
velocityX,
velocityY,
consumed
)
)
override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) {
super.onNestedPreScroll(target, dx, dy, consumed, type)
if (consumed[1] == 0) {
dispatchNestedPreScroll(dx, dy, consumed, null, type)
}
}
override fun onNestedScroll(
target: View,
dxConsumed: Int,
dyConsumed: Int,
dxUnconsumed: Int,
dyUnconsumed: Int,
type: Int,
consumed: IntArray
) {
super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type, consumed)
if (consumed[1] == 0) {
dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null, type, consumed)
}
}
override fun onNestedPreFling(target: View, velocityX: Float, velocityY: Float): Boolean =
super.onNestedPreFling(target, velocityX, velocityY).or(dispatchNestedPreFling(velocityX, velocityY))
override fun onStopNestedScroll(target: View, type: Int) {
super.onStopNestedScroll(target, type)
stopNestedScroll(type)
}
override fun dispatchNestedScroll(
dxConsumed: Int,
dyConsumed: Int,
dxUnconsumed: Int,
dyUnconsumed: Int,
offsetInWindow: IntArray?,
type: Int
): Boolean = nestedScrollingChildHelper.dispatchNestedScroll(
dxConsumed,
dyConsumed,
dxUnconsumed,
dyUnconsumed,
offsetInWindow,
type
)
override fun dispatchNestedScroll(
dxConsumed: Int,
dyConsumed: Int,
dxUnconsumed: Int,
dyUnconsumed: Int,
offsetInWindow: IntArray?
): Boolean = nestedScrollingChildHelper.dispatchNestedScroll(
dxConsumed,
dyConsumed,
dxUnconsumed,
dyUnconsumed,
offsetInWindow
)
override fun dispatchNestedScroll(
dxConsumed: Int,
dyConsumed: Int,
dxUnconsumed: Int,
dyUnconsumed: Int,
offsetInWindow: IntArray?,
type: Int,
consumed: IntArray
) {
nestedScrollingChildHelper.dispatchNestedScroll(
dxConsumed,
dyConsumed,
dxUnconsumed,
dyUnconsumed,
offsetInWindow,
type,
consumed
)
}
override fun isNestedScrollingEnabled(): Boolean = nestedScrollingChildHelper.isNestedScrollingEnabled
override fun dispatchNestedPreScroll(
dx: Int,
dy: Int,
consumed: IntArray?,
offsetInWindow: IntArray?,
type: Int
): Boolean = nestedScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow, type)
override fun dispatchNestedPreScroll(dx: Int, dy: Int, consumed: IntArray?, offsetInWindow: IntArray?): Boolean =
nestedScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)
override fun stopNestedScroll(type: Int) {
nestedScrollingChildHelper.stopNestedScroll(type)
}
override fun stopNestedScroll() {
nestedScrollingChildHelper.stopNestedScroll()
}
override fun hasNestedScrollingParent(type: Int): Boolean =
nestedScrollingChildHelper.hasNestedScrollingParent(type)
override fun hasNestedScrollingParent(): Boolean = nestedScrollingChildHelper.hasNestedScrollingParent()
override fun dispatchNestedPreFling(velocityX: Float, velocityY: Float): Boolean =
nestedScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY)
override fun setNestedScrollingEnabled(enabled: Boolean) {
nestedScrollingChildHelper.isNestedScrollingEnabled = enabled
}
override fun dispatchNestedFling(velocityX: Float, velocityY: Float, consumed: Boolean): Boolean =
nestedScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed)
override fun startNestedScroll(axes: Int, type: Int): Boolean =
nestedScrollingChildHelper.startNestedScroll(axes, type)
override fun startNestedScroll(axes: Int): Boolean = nestedScrollingChildHelper.startNestedScroll(axes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment