/**
* https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2
*
* 코디네이너레이아웃 앱바레이아웃.behavior
*
* 사용법.
* code
* AppBarLayout abl = findViewById(R.id.app_bar);
* ((CoordinatorLayout.LayoutParams) abl.getLayoutParams()).setBehavior(new FixAppBarLayoutBehavior());
*
* xml에서 사용됨.
* xml
* <android.support.design.widget.AppBarLayout
* android:id="@+id/app_bar"
* android:layout_height="..."
* android:layout_width="..."
* app:layout_behavior="your.package.FixAppBarLayoutBehavior">
*/
class FixAppBarLayoutBehavior(context: Context?, attrs: AttributeSet?) :
AppBarLayout.Behavior(context, attrs) {

override fun onNestedScroll(coordinatorLayout: CoordinatorLayout, child: AppBarLayout,
target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int,
dyUnconsumed: Int, type: Int) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
dyUnconsumed, type)
stopNestedScrollIfNeeded(dyUnconsumed, child, target, type)
}

override fun onNestedPreScroll(coordinatorLayout: CoordinatorLayout, child: AppBarLayout,
target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type)
stopNestedScrollIfNeeded(dy, child, target, type)
}

private fun stopNestedScrollIfNeeded(dy: Int, child: AppBarLayout, target: View, type: Int) {
if (type == ViewCompat.TYPE_NON_TOUCH) {
val currOffset = topAndBottomOffset
if (dy < 0 && currOffset == 0 || dy > 0 && currOffset == -child.totalScrollRange) {
ViewCompat.stopNestedScroll(target, ViewCompat.TYPE_NON_TOUCH)
}
}
}
}

+ Recent posts