Skip to content

Instantly share code, notes, and snippets.

@alex-townsend
Created March 15, 2016 18:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-townsend/e4cda67be7a42540834b to your computer and use it in GitHub Desktop.
Save alex-townsend/e4cda67be7a42540834b to your computer and use it in GitHub Desktop.
A CoordinatorLayout.Behavior for showing/hiding a CircleImageView anchored to an AppBarLayout
package android.support.design.widget;
import android.graphics.Rect;
import android.view.View;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* Layout Behavior to show/hide a CircleImageView anchored to a AppBarLayout
*/
public class CircleImageViewBehavior extends CoordinatorLayout.Behavior<CircleImageView> {
private Rect mTmpRect;
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, CircleImageView child, View dependency) {
// check that our dependency is the AppBarLayout
return dependency instanceof AppBarLayout;
}
@Override public boolean onDependentViewChanged(CoordinatorLayout parent, CircleImageView child,
View dependency) {
if (dependency instanceof AppBarLayout) {
return updateCircleImageViewVisibility(parent, (AppBarLayout) dependency, child);
}
return false;
}
private boolean updateCircleImageViewVisibility(CoordinatorLayout parent,
AppBarLayout appBarLayout, CircleImageView child) {
final CoordinatorLayout.LayoutParams lp =
(CoordinatorLayout.LayoutParams) child.getLayoutParams();
if (lp.getAnchorId() != appBarLayout.getId()) {
// The anchor ID doesn't match the dependency, so we won't automatically
// show/hide the FAB
return false;
}
if (mTmpRect == null) {
mTmpRect = new Rect();
}
// First, let's get the visible rect of the dependency
final Rect rect = mTmpRect;
ViewGroupUtils.getDescendantRect(parent, appBarLayout, rect);
if (rect.bottom <= appBarLayout.getMinimumHeightForVisibleOverlappingContent()) {
// If the anchor's bottom is below the seam, hide it
child.setVisibility(View.INVISIBLE);
} else {
// Else, show it
child.setVisibility(View.VISIBLE);
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment