Skip to content

Instantly share code, notes, and snippets.

@blipinsk
Last active September 15, 2020 11:32
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save blipinsk/3f8fb37209de6d3eea99 to your computer and use it in GitHub Desktop.
Save blipinsk/3f8fb37209de6d3eea99 to your computer and use it in GitHub Desktop.
An extension of Android AppBarLayout, that allows to programatically change the CollapsibleToolbarLayout state

DEPRECATED

ControllableAppBarLayout has been made to allow to programatically change the CollapsibleToolbarLayout state.

From v23 of the design support library this function is available in the AppBarLayout itself.

You should probably use that (setExpanded) instead of ControllableAppBarLayout (although setTitle seems to be a bit bugged with setting expanded or collapsed).

/**
* Copyright 2015 Bartosz Lipinski
*
* 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.
*/
import android.content.Context;
import android.graphics.Canvas;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import java.lang.ref.WeakReference;
public class ControllableAppBarLayout extends AppBarLayout {
private AppBarLayout.Behavior mBehavior;
private WeakReference<CoordinatorLayout> mParent;
private ToolbarChange mQueuedChange = ToolbarChange.NONE;
private boolean mAfterFirstDraw = false;
public ControllableAppBarLayout(Context context) {
super(context);
}
public ControllableAppBarLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!(getLayoutParams() instanceof CoordinatorLayout.LayoutParams) || !(getParent() instanceof CoordinatorLayout)) {
throw new IllegalStateException("ControllableAppBarLayout must be a direct child of CoordinatorLayout.");
} else {
mParent = new WeakReference<CoordinatorLayout>((CoordinatorLayout) getParent());
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mBehavior == null) {
mBehavior = (Behavior) ((CoordinatorLayout.LayoutParams) getLayoutParams()).getBehavior();
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (r - l > 0 && b - t > 0 && mAfterFirstDraw && mQueuedChange != ToolbarChange.NONE) {
analyzeQueuedChange();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!mAfterFirstDraw) {
mAfterFirstDraw = true;
if (mQueuedChange != ToolbarChange.NONE) {
analyzeQueuedChange();
}
}
}
private synchronized void analyzeQueuedChange() {
switch (mQueuedChange) {
case COLLAPSE:
performCollapsingWithoutAnimation();
break;
case COLLAPSE_WITH_ANIMATION:
performCollapsingWithAnimation();
break;
case EXPAND:
performExpandingWithoutAnimation();
break;
case EXPAND_WITH_ANIMATION:
performExpandingWithAnimation();
break;
}
mQueuedChange = ToolbarChange.NONE;
}
public void collapseToolbar() {
collapseToolbar(false);
}
public void collapseToolbar(boolean withAnimation) {
mQueuedChange = withAnimation ? ToolbarChange.COLLAPSE_WITH_ANIMATION : ToolbarChange.COLLAPSE;
requestLayout();
}
public void expandToolbar() {
expandToolbar(false);
}
public void expandToolbar(boolean withAnimation) {
mQueuedChange = withAnimation ? ToolbarChange.EXPAND_WITH_ANIMATION : ToolbarChange.EXPAND;
requestLayout();
}
private void performCollapsingWithoutAnimation() {
if (mParent.get() != null) {
mBehavior.onNestedPreScroll(mParent.get(), this, null, 0, getHeight(), new int[]{0, 0});
}
}
private void performCollapsingWithAnimation() {
if (mParent.get() != null) {
mBehavior.onNestedFling(mParent.get(), this, null, 0, getHeight(), true);
}
}
private void performExpandingWithoutAnimation() {
if (mParent.get() != null) {
mBehavior.setTopAndBottomOffset(0);
}
}
private void performExpandingWithAnimation() {
if (mParent.get() != null) {
mBehavior.onNestedFling(mParent.get(), this, null, 0, -getHeight() * 5, false);
}
}
private enum ToolbarChange {
COLLAPSE,
COLLAPSE_WITH_ANIMATION,
EXPAND,
EXPAND_WITH_ANIMATION,
NONE
}
}
@marcelpinto
Copy link

Hi nice made, I was doing this outside and a bit different but this implementation works really good. I fork it and add a listener to know when is expanded, collapsed or in between.

https://gist.github.com/skimarxall/863585dcd7abde8f4153

@blipinsk
Copy link
Author

Awesome! I'm glad it works for you 👍 . Thanks for the heads-up on your fork with the listener. Cheers!

@iftekhar-ahmed
Copy link

Great Work! This led me to an understanding of the actual layout behavior of appbar. I followed @skimarxall's updated gist. I tried to take that a little farther with a list of weakreferences to multiple listeners and support add/remove. Hope that helps someone. Thanks a lot @blipinsk. Here's my gist,

https://gist.github.com/iftekhar-ahmed/0b43d49ce03843e18b63

@GuyMichael
Copy link

Very nicely done! Thank you;)

@blipinsk
Copy link
Author

blipinsk commented Sep 8, 2015

You're welcome 😄

@IbrahimDisouki
Copy link

From v23 of the design support library this function is available in the AppBarLayout itself.
Please can you tell what is the function ?

@blipinsk
Copy link
Author

@shivanic1986
Copy link

Hello @blipinsk

I am using your class for collapse AppBar at Half of height but it is broken in 23.0.1.
Is there any other way to collapse AppBar to Half of Height ?
or
Can you tell me which version you have used when you create this class ?

Thanks,
Shivani

@vineet-devin
Copy link

Anyone knows how to set the AppBarLayout to fixed. like I should be able to set it to fixed and later change it to expandable. any idea?

@cargo8
Copy link

cargo8 commented Sep 16, 2016

@vineet-devin change the scroll flags on the layout params of the views within the AppBarLayout

for (int i = 0; i < appBarLayout.getChildCount(); i++) {
    View view = appBarLayout.getChildAt(i);
    AppBarLayout.LayoutParams lp = view.getLayoutParams();
    lp.setScrollFlags(SCROLL_FLAG_ENTER_ALWAYS); // locks the app bar into position
//  lp.setScrollFlags(SCROLL_FLAG_ENTER_ALWAYS|SCROLL_FLAG_SCROLL); // unlocks the app bar to scroll and reveal/hide
}

@pavelsust
Copy link

How to use it i can't understand. anyone bro??

@blipinsk
Copy link
Author

How to use it i can't understand. anyone bro??

I don't think you need it anymore @pavelsust. Just use setExpanded method.

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