Forked from chrisbanes/FixAppBarLayoutBehavior.java
Last active
August 6, 2018 17:20
-
-
Save rodrigosimoesrosa/9f06e8773322f1743109a2339819d8c5 to your computer and use it in GitHub Desktop.
Kotlin version - Fixed AppBarLayout.Behavior for https://issuetracker.google.com/66996774
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2018 The Android Open Source Project | |
* | |
* 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 | |
/** | |
* This is the Kotlin version of implementation which was created with the purpose to fix a | |
* bug of app bar layout. | |
* | |
* The original implementation is hosted https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2 | |
*/ | |
class FixAppBarLayoutBehavior : AppBarLayout.Behavior { | |
constructor() : super() | |
constructor(context: Context, attrs: AttributeSet) : super(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) { | |
return | |
} | |
val currOffset = topAndBottomOffset | |
if (dy < 0 && currOffset == 0 || dy > 0 && currOffset == -child.totalScrollRange) { | |
ViewCompat.stopNestedScroll(target, ViewCompat.TYPE_NON_TOUCH) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val appBarLayout = findViewById(R.id.app_bar); | |
(appBarLayout.layoutParams as CoordinatorLayout.LayoutParams).behavior = FixAppBarLayoutBehavior() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<android.support.design.widget.AppBarLayout | |
android:id="@+id/app_bar" | |
android:layout_height="..." | |
android:layout_width="..." | |
app:layout_behavior="your.package.FixAppBarLayoutBehavior"> | |
</android.support.design.widget.AppBarLayout> | |
<!-- Content --> | |
</android.support.design.widget.CoordinatorLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment