Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Fixed AppBarLayout.Behavior for
* Copyright (C) 2017 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
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.
package your.package;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
* Workaround AppBarLayout.Behavior for
* See for
* example usage.
* Change the package name as you wish.
public class FixAppBarLayoutBehavior extends AppBarLayout.Behavior {
public FixAppBarLayoutBehavior() {
public FixAppBarLayoutBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target,
int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
dxUnconsumed, dyUnconsumed, type);
stopNestedScrollIfNeeded(dyUnconsumed, child, target, type);
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
View target, int dx, int dy, int[] consumed, int type) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
stopNestedScrollIfNeeded(dy, child, target, type);
private void stopNestedScrollIfNeeded(int dy, AppBarLayout child, View target, int type) {
if (type == ViewCompat.TYPE_NON_TOUCH) {
final int currOffset = getTopAndBottomOffset();
if ((dy < 0 && currOffset == 0)
|| (dy > 0 && currOffset == -child.getTotalScrollRange())) {
ViewCompat.stopNestedScroll(target, ViewCompat.TYPE_NON_TOUCH);
AppBarLayout abl = findViewById(;
((CoordinatorLayout.LayoutParams) abl.getLayoutParams()).setBehavior(new FixAppBarLayoutBehavior());
< xmlns:android=""
<!-- Content -->
Copy link

sinanceylan commented May 21, 2018

It also works on CollapsingToolbarLayout! Thank you very much. You saved my hours.
By the way, current version support library 27.1.1 has this bug and this solution works.

Copy link

sjd753 commented May 24, 2018

It solves the fling problem but normal ACTION_MOVE does not work on RecyclerView (Horizontal) inside NestedScrollView

Copy link

chongbo2013 commented Jun 13, 2018

thank u

Copy link

booncol commented Jun 17, 2018

You are genius! Thank you.

Copy link

rawewhat commented Jun 18, 2018

It works perfectly, but when I test on older Android like 4.1.1 the problem happens again, the button can't be click and only can scroll half of the screen.

Copy link

ljaniszewski commented Jun 21, 2018

yeah, you can't expect this to be working on such old Android.
For me works good, thanks!

Copy link

luangs7 commented Jun 29, 2018

Works perfect to me! Thanks, you saved me

Copy link

dariush-fathie commented Jul 6, 2018

thank you . it works

Copy link

nobeginning commented Jul 11, 2018

Nice, thank you man

Copy link

Ahmedfir commented Jul 16, 2018

Amazing ! I lost a lot of time to recognize that it was the appbar... thanks a lot !

Copy link

rodrigosimoesrosa commented Aug 6, 2018

Works perfectly to me, thank you very much!

Copy link

lvaizer commented Aug 9, 2018


Copy link

jhonsonkilly commented Aug 10, 2018


Copy link

ibrahimsn98 commented Aug 24, 2018

You saved me :D Thanks!

Copy link

longjiang-dragon commented Aug 26, 2018


Copy link

hellaandrew commented Sep 11, 2018

Excellent work! This saved me a lot of headache.

Copy link

dwist14 commented Sep 13, 2018

Mantap jiwa..

Copy link

wangkunlin commented Sep 19, 2018

Works perfect! Thanks

Copy link

ShahoodulHassan commented Sep 23, 2018

Not working for me on V27.1.1
I've tried both java and xml ways but in vain.
Checked on emulator running on 5.0 and physical device running on 7.1.1
Any ideas ?

Copy link

fazalurrehmansharif commented Nov 12, 2018

Thanks so much i was wondering if there is any solution or not but now i am feeling very lucky thankyou so much

Copy link

shubhamvashisht commented Apr 8, 2019

Am having same issue with BottomSheetBehavior, any fix for that?

Copy link

harsha-main commented Jun 11, 2019

Why isn't this incorporated in the official release so far? I have a Horizontal recyclerview inside a vertical recyclerview, which is inside a nestedscrollview. I'm now having issues with the usability of the horizontal scroll. It works very different from the Google Play app, which has good UX, I need to know if there's a standard solution for this provided by Android.

Copy link

geoff-powell commented Jun 21, 2019

@harsha-main I have the same layout with horizontal recycler inside a vertical recycler inside a coordinator layout. One think that fixed my issues with the app bar layout was setting android:nestedScrollEnabled="false" on the horizontal recycler. It fixed our issues with our collapsing header so it might fix your issues.

Copy link

allenjy commented Oct 23, 2019


Copy link

Ekt0s commented Nov 19, 2019

Thanks Chris ! 🙌

Copy link

fjz97 commented Jan 10, 2020


Copy link

jesselima commented Mar 22, 2020

Oh God!!!

Copy link

sajjadyousefnia commented May 26, 2020

doesn't work for me, crashes!.

Copy link

boylenssen commented Nov 6, 2020

Seems to be solved in the latest version of AndroidX dependencies

Copy link

yummyChina commented Dec 25, 2020

doesn't work for me

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