Skip to content

Instantly share code, notes, and snippets.

@ktchernov
Created June 13, 2017 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktchernov/8a70cd02ce78d7b8778304352ae4a243 to your computer and use it in GitHub Desktop.
Save ktchernov/8a70cd02ce78d7b8778304352ae4a243 to your computer and use it in GitHub Desktop.
An extension of Android AppBarLayout, that allows to receive state changes (collapsed, expanded and idle). This is a simplified version of ControllableAppBarLayout (https://gist.github.com/skimarxall/863585dcd7abde8f4153) - only implementing a state change listener.
package io.github.ktchernov.play_aroundapp.views;
/**
* Copyright 2017 Konstantin Tchernov
*
* Adapted from: ControllableAppBarLayout by Bartosz Lipinski and
* Marcel Pintó Biescas
*
* 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.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
public class StateAwareAppBarLayout extends AppBarLayout
implements AppBarLayout.OnOffsetChangedListener {
private AppBarLayout.Behavior mBehavior;
private State state;
private OnStateChangeListener onStateChangeListener;
public StateAwareAppBarLayout(Context context) {
super(context);
}
public StateAwareAppBarLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
addOnOffsetChangedListener(this);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mBehavior == null) {
mBehavior =
(Behavior) ((CoordinatorLayout.LayoutParams) getLayoutParams()).getBehavior();
}
}
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (verticalOffset == 0) {
if (onStateChangeListener != null && state != State.EXPANDED) {
onStateChangeListener.onStateChange(State.EXPANDED);
}
state = State.EXPANDED;
} else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
if (onStateChangeListener != null && state != State.COLLAPSED) {
onStateChangeListener.onStateChange(State.COLLAPSED);
}
state = State.COLLAPSED;
} else {
if (onStateChangeListener != null && state != State.IDLE) {
onStateChangeListener.onStateChange(State.IDLE);
}
state = State.IDLE;
}
}
public void setOnStateChangeListener(OnStateChangeListener listener) {
this.onStateChangeListener = listener;
}
public State getState() {
return state;
}
public interface OnStateChangeListener {
void onStateChange(State toolbarChange);
}
public enum State {
COLLAPSED,
EXPANDED,
IDLE
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment