Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Aug 9, 2018

Thanks you for that!

@jishindev

This comment has been minimized.

Copy link

commented Feb 22, 2019

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

@amitkumar0000

This comment has been minimized.

Copy link

commented May 21, 2019

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
You can’t perform that action at this time.