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
*
* 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.
*
* Change the package name as you wish.
*/
public class FixAppBarLayoutBehavior extends AppBarLayout.Behavior {
public FixAppBarLayoutBehavior() {
super();
}
public FixAppBarLayoutBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
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);
}
@Override
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(R.id.app_bar);
((CoordinatorLayout.LayoutParams) abl.getLayoutParams()).setBehavior(new FixAppBarLayoutBehavior());
<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>
@erikhuizinga

This comment has been minimized.

Show comment
Hide comment
@erikhuizinga

erikhuizinga commented Nov 16, 2017

@emretekin

This comment has been minimized.

Show comment
Hide comment
@emretekin

emretekin Dec 5, 2017

This is exactly the best solution for this click bug. It works very well for me.
Thank you man.

emretekin commented Dec 5, 2017

This is exactly the best solution for this click bug. It works very well for me.
Thank you man.

@mohamadk

This comment has been minimized.

Show comment
Hide comment
@mohamadk

mohamadk Dec 7, 2017

well done man

mohamadk commented Dec 7, 2017

well done man

@NullPoint-A

This comment has been minimized.

Show comment
Hide comment
@NullPoint-A

NullPoint-A Dec 14, 2017

very gooD!! Thanks~

NullPoint-A commented Dec 14, 2017

very gooD!! Thanks~

@maysamrasoli

This comment has been minimized.

Show comment
Hide comment
@maysamrasoli

maysamrasoli Dec 26, 2017

very nice. thank u

maysamrasoli commented Dec 26, 2017

very nice. thank u

@kaiyan910

This comment has been minimized.

Show comment
Hide comment
@kaiyan910

kaiyan910 Jan 3, 2018

thanks, finally solved the problem after searching for the whole morning

kaiyan910 commented Jan 3, 2018

thanks, finally solved the problem after searching for the whole morning

@DennyWeinberg

This comment has been minimized.

Show comment
Hide comment
@DennyWeinberg

DennyWeinberg Jan 7, 2018

Awesome! Thank you!!!!!!!

DennyWeinberg commented Jan 7, 2018

Awesome! Thank you!!!!!!!

@amiejais

This comment has been minimized.

Show comment
Hide comment
@amiejais

amiejais Feb 5, 2018

Not working for me ...I have tried all ..but nothing working

amiejais commented Feb 5, 2018

Not working for me ...I have tried all ..but nothing working

@Asad4429

This comment has been minimized.

Show comment
Hide comment
@Asad4429

Asad4429 commented Feb 6, 2018

Thanks

@reyn4lu

This comment has been minimized.

Show comment
Hide comment
@reyn4lu

reyn4lu Feb 7, 2018

@chrisbanes I use listview setNestedScrollingEnabled true instead of recyclerview, can't work.

reyn4lu commented Feb 7, 2018

@chrisbanes I use listview setNestedScrollingEnabled true instead of recyclerview, can't work.

@fedorenkoalex

This comment has been minimized.

Show comment
Hide comment
@fedorenkoalex

fedorenkoalex Mar 2, 2018

it works! thank you too much

fedorenkoalex commented Mar 2, 2018

it works! thank you too much

@pandalion98

This comment has been minimized.

Show comment
Hide comment
@pandalion98

pandalion98 Apr 9, 2018

Can confirm that this works.

pandalion98 commented Apr 9, 2018

Can confirm that this works.

@tsasotv

This comment has been minimized.

Show comment
Hide comment
@tsasotv

tsasotv Apr 16, 2018

excellent!!
It's a great job

tsasotv commented Apr 16, 2018

excellent!!
It's a great job

@Tanamo-Inc

This comment has been minimized.

Show comment
Hide comment
@Tanamo-Inc

Tanamo-Inc May 8, 2018

perfecto!!!
Thanks

Tanamo-Inc commented May 8, 2018

perfecto!!!
Thanks

@surekha29

This comment has been minimized.

Show comment
Hide comment
@surekha29

surekha29 May 16, 2018

Worked like a charm!!
When is this going to be fixed in the support library, so we don't have to use the workaround?

surekha29 commented May 16, 2018

Worked like a charm!!
When is this going to be fixed in the support library, so we don't have to use the workaround?

@sinanceylan

This comment has been minimized.

Show comment
Hide comment
@sinanceylan

sinanceylan 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.

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.

@sjd753

This comment has been minimized.

Show comment
Hide comment
@sjd753

sjd753 May 24, 2018

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

sjd753 commented May 24, 2018

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

@chongbo2013

This comment has been minimized.

Show comment
Hide comment
@chongbo2013

chongbo2013 commented Jun 13, 2018

thank u

@booncol

This comment has been minimized.

Show comment
Hide comment
@booncol

booncol Jun 17, 2018

You are genius! Thank you.

booncol commented Jun 17, 2018

You are genius! Thank you.

@rawewhat

This comment has been minimized.

Show comment
Hide comment
@rawewhat

rawewhat 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.

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.

@ljaniszewski

This comment has been minimized.

Show comment
Hide comment
@ljaniszewski

ljaniszewski Jun 21, 2018

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

ljaniszewski commented Jun 21, 2018

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

@luangs7

This comment has been minimized.

Show comment
Hide comment
@luangs7

luangs7 Jun 29, 2018

Works perfect to me! Thanks, you saved me

luangs7 commented Jun 29, 2018

Works perfect to me! Thanks, you saved me

@dariush-fathie

This comment has been minimized.

Show comment
Hide comment
@dariush-fathie

dariush-fathie Jul 6, 2018

thank you . it works

dariush-fathie commented Jul 6, 2018

thank you . it works

@nobeginning

This comment has been minimized.

Show comment
Hide comment
@nobeginning

nobeginning Jul 11, 2018

Nice, thank you man

nobeginning commented Jul 11, 2018

Nice, thank you man

@Ahmedfir

This comment has been minimized.

Show comment
Hide comment
@Ahmedfir

Ahmedfir Jul 16, 2018

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

Ahmedfir commented Jul 16, 2018

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

@rodrigosimoesrosa

This comment has been minimized.

Show comment
Hide comment
@rodrigosimoesrosa

rodrigosimoesrosa Aug 6, 2018

Works perfectly to me, thank you very much!

rodrigosimoesrosa commented Aug 6, 2018

Works perfectly to me, thank you very much!

@lvaizer

This comment has been minimized.

Show comment
Hide comment
@lvaizer

lvaizer Aug 9, 2018

Thanks!!!

lvaizer commented Aug 9, 2018

Thanks!!!

@jhonsonkilly

This comment has been minimized.

Show comment
Hide comment
@jhonsonkilly

jhonsonkilly commented Aug 10, 2018

Thanks

@ibrahimsn98

This comment has been minimized.

Show comment
Hide comment
@ibrahimsn98

ibrahimsn98 Aug 24, 2018

You saved me :D Thanks!

ibrahimsn98 commented Aug 24, 2018

You saved me :D Thanks!

@longjiang-dragon

This comment has been minimized.

Show comment
Hide comment
@longjiang-dragon

longjiang-dragon commented Aug 26, 2018

Thanks!!!

@hellaandrew

This comment has been minimized.

Show comment
Hide comment
@hellaandrew

hellaandrew Sep 11, 2018

Excellent work! This saved me a lot of headache.

hellaandrew commented Sep 11, 2018

Excellent work! This saved me a lot of headache.

@dwist14

This comment has been minimized.

Show comment
Hide comment
@dwist14

dwist14 Sep 13, 2018

Mantap jiwa..

dwist14 commented Sep 13, 2018

Mantap jiwa..

@wangkunlin

This comment has been minimized.

Show comment
Hide comment
@wangkunlin

wangkunlin Sep 19, 2018

Works perfect! Thanks

wangkunlin commented Sep 19, 2018

Works perfect! Thanks

@ShahoodulHassan

This comment has been minimized.

Show comment
Hide comment
@ShahoodulHassan

ShahoodulHassan 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 ?

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 ?

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