Android Coordinatorlayout in Coordinatorlayout NestedScroll Behavior공부방/Android2021. 11. 24. 18:09
Table of Contents
/**
* Created by JeonGuKang on 24,11월,2021
*
* 아래 주석은 하위 자식 CoordinatorLayout behavior 가 nestedscroll 시 동시에 작동 필요시 사용
* // consumed[0] = tConsumed[0][0] + tConsumed[1][0]
// consumed[1] = tConsumed[0][1] + tConsumed[1][1]
*/
class NestedCoordinatorLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : CoordinatorLayout(context, attrs, defStyleAttr), NestedScrollingChild {
private var mChildHelper: NestedScrollingChildHelper = NestedScrollingChildHelper(this)
init {
isNestedScrollingEnabled = true
}
override fun onNestedPreScroll(
target: View,
dx: Int,
dy: Int,
consumed: IntArray,
type: Int
) {
val tConsumed = Array(2) { IntArray(2) }
super.onNestedPreScroll(target, dx, dy, consumed, type)
dispatchNestedPreScroll(dx, dy, tConsumed[1], null)
// consumed[0] = tConsumed[0][0] + tConsumed[1][0]
// consumed[1] = tConsumed[0][1] + tConsumed[1][1]
}
override fun onNestedScroll(
target: View,
dxConsumed: Int,
dyConsumed: Int,
dxUnconsumed: Int,
dyUnconsumed: Int,
type: Int
) {
super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type)
dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null)
}
override fun onStopNestedScroll(target: View, type: Int) {
/* Disable the scrolling behavior of our own children */
super.onStopNestedScroll(target, type)
/* Disable the scrolling behavior of the parent's other children */
stopNestedScroll()
}
override fun onStartNestedScroll(child: View, target: View, nestedScrollAxes: Int, type: Int): Boolean {
/* Enable the scrolling behavior of our own children */
val tHandled = super.onStartNestedScroll(child, target, nestedScrollAxes, type)
/* Enable the scrolling behavior of the parent's other children */
return startNestedScroll(nestedScrollAxes) || tHandled
}
override fun onStartNestedScroll(child: View, target: View, nestedScrollAxes: Int): Boolean {
/* Enable the scrolling behavior of our own children */
val tHandled = super.onStartNestedScroll(child, target, nestedScrollAxes)
/* Enable the scrolling behavior of the parent's other children */
return startNestedScroll(nestedScrollAxes) || tHandled
}
override fun onStopNestedScroll(target: View) {
/* Disable the scrolling behavior of our own children */
super.onStopNestedScroll(target)
/* Disable the scrolling behavior of the parent's other children */
stopNestedScroll()
}
override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray) {
val tConsumed = Array(2) { IntArray(2) }
super.onNestedPreScroll(target, dx, dy, tConsumed[0])
dispatchNestedPreScroll(dx, dy, tConsumed[1], null)
// consumed[0] = tConsumed[0][0] + tConsumed[1][0]
// consumed[1] = tConsumed[0][1] + tConsumed[1][1]
}
override fun onNestedScroll(
target: View,
dxConsumed: Int,
dyConsumed: Int,
dxUnconsumed: Int,
dyUnconsumed: Int
) {
super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed)
dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null)
}
override fun onNestedPreFling(
target: View,
velocityX: Float,
velocityY: Float
): Boolean {
val tHandled = super.onNestedPreFling(target, velocityX, velocityY)
return dispatchNestedPreFling(velocityX, velocityY) || tHandled
}
override fun onNestedFling(
target: View,
velocityX: Float,
velocityY: Float,
consumed: Boolean
): Boolean {
val tHandled = super.onNestedFling(target, velocityX, velocityY, consumed)
return dispatchNestedFling(velocityX, velocityY, consumed) || tHandled
}
override fun isNestedScrollingEnabled(): Boolean {
return mChildHelper.isNestedScrollingEnabled
}
override fun setNestedScrollingEnabled(enabled: Boolean) {
mChildHelper.isNestedScrollingEnabled = enabled
}
override fun startNestedScroll(axes: Int): Boolean {
return mChildHelper.startNestedScroll(axes)
}
override fun stopNestedScroll() {
mChildHelper.stopNestedScroll()
}
override fun hasNestedScrollingParent() = mChildHelper.hasNestedScrollingParent()
override fun dispatchNestedScroll(
dxConsumed: Int,
dyConsumed: Int,
dxUnconsumed: Int,
dyUnconsumed: Int,
offsetInWindow: IntArray?
): Boolean {
return mChildHelper.dispatchNestedScroll(
dxConsumed, dyConsumed, dxUnconsumed,
dyUnconsumed, offsetInWindow
)
}
override fun dispatchNestedPreScroll(
dx: Int,
dy: Int,
consumed: IntArray?,
offsetInWindow: IntArray?
): Boolean {
return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)
}
override fun dispatchNestedFling(
velocityX: Float,
velocityY: Float,
consumed: Boolean
): Boolean {
return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed)
}
override fun dispatchNestedPreFling(velocityX: Float, velocityY: Float): Boolean {
return mChildHelper.dispatchNestedPreFling(velocityX, velocityY)
}
}
출처 :
https://stackoverflow.com/a/60613726
https://stackoverflow.com/questions/34181372/coordinatorlayout-inside-another-coordinatorlayout
반응형
'공부방 > Android' 카테고리의 다른 글
@soycrab :: 꿀맛코딩
행복한 코딩을 위하여!
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!