Skip to content

Instantly share code, notes, and snippets.

@erikhuizinga
Last active February 6, 2020 06:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erikhuizinga/edf408167b46eb5b1568424563ca4e59 to your computer and use it in GitHub Desktop.
Save erikhuizinga/edf408167b46eb5b1568424563ca4e59 to your computer and use it in GitHub Desktop.
Fixed AppBarLayout.Behavior for https://issuetracker.google.com/66996774
/*
* Copyright (C) 2017 The Android Open Source Project & Erik Huizinga
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package your.package
import android.content.Context
import android.support.design.widget.AppBarLayout
import android.support.design.widget.CoordinatorLayout
import android.support.v4.view.ViewCompat
import android.util.AttributeSet
import android.view.View
/**
* Workaround AppBarLayout.Behavior for https://issuetracker.google.com/66996774
*
*
* See https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2 for
* example usage.
*
* Kotlinised by Erik Huizinga (github: @erikhuizinga).
*/
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)
}
}
}
}
@lvaizer
Copy link

lvaizer commented Aug 9, 2018

Thanks you for that!

@jishindev
Copy link

Please add the default empty constructor implementation as well for usages in Kotlin files directly.

@amitkumar0000
Copy link

please change this from
class FixAppBarLayoutBehavior(context: Context?, attrs: AttributeSet?) to

class FixAppBarLayoutBehavior @jvmoverloads constructor(context: Context?, attrs: AttributeSet?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment