Skip to content

Instantly share code, notes, and snippets.

@trivalent
Created June 13, 2017 08:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trivalent/593a9d5552c9fd6cb35b7085b2005928 to your computer and use it in GitHub Desktop.
Save trivalent/593a9d5552c9fd6cb35b7085b2005928 to your computer and use it in GitHub Desktop.
package in.co.shuklarahul.sotest;
import android.animation.ValueAnimator;
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;
/**
* Created by zhuleiyue on 2017/3/7.
* Modified : rahul shukla : 13/06/17
*/
public class MyAppBarLayoutBehavior extends AppBarLayout.Behavior {
private static final String TAG = "overScroll";
private static final float TARGET_HEIGHT = 500;
private View mTargetView;
private int mParentHeight;
private int mTargetViewHeight;
private float mTotalDy;
private float mLastScale;
private int mLastBottom;
private boolean isAnimate;
public MyAppBarLayoutBehavior() {
}
public MyAppBarLayoutBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onLayoutChild(CoordinatorLayout parent, AppBarLayout abl, int layoutDirection) {
boolean handled = super.onLayoutChild(parent, abl, layoutDirection);
// 需要在调用过super.onLayoutChild()方法之后获取
if (mTargetView == null) {
mTargetView = parent.findViewWithTag(TAG);
if (mTargetView != null) {
initial(abl);
}
}
return handled;
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) {
isAnimate = true;
return super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes);
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) {
if (mTargetView != null && ((dy < 0 && child.getBottom() >= mParentHeight) || (dy > 0 && child.getBottom() > mParentHeight))) {
scale(child, target, dy);
} else {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
}
}
@Override
public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY) {
if (velocityY > 100) {
isAnimate = false;
}
return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
}
@Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout abl, View target) {
recovery(abl);
super.onStopNestedScroll(coordinatorLayout, abl, target);
}
private void initial(AppBarLayout abl) {
abl.setClipChildren(false);
mParentHeight = abl.getBottom();
mTargetViewHeight = mTargetView.getHeight();
}
private void scale(AppBarLayout abl, View target, int dy) {
mTotalDy += -dy;
mTotalDy = Math.min(mTotalDy, TARGET_HEIGHT);
mLastScale = Math.max(1f, 1f + mTotalDy / TARGET_HEIGHT);
ViewCompat.setScaleX(mTargetView, mLastScale);
ViewCompat.setScaleY(mTargetView, mLastScale);
mLastBottom = mParentHeight + (int) (mTargetViewHeight / 2 * (mLastScale - 1));
abl.setBottom(mLastBottom);
}
private void recovery(final AppBarLayout abl) {
if (mTotalDy > 0) {
mTotalDy = 0;
if (isAnimate) {
ValueAnimator anim = ValueAnimator.ofFloat(mLastScale, 1f).setDuration(200);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (float) animation.getAnimatedValue();
ViewCompat.setScaleX(mTargetView, value);
ViewCompat.setScaleY(mTargetView, value);
abl.setBottom((int) (mLastBottom - (mLastBottom - mParentHeight) * animation.getAnimatedFraction()));
}
});
anim.start();
} else {
ViewCompat.setScaleX(mTargetView, 1f);
ViewCompat.setScaleY(mTargetView, 1f);
abl.setBottom(mParentHeight);
}
}
}
}
@hemantskycap
Copy link

How I set it to AppBarLayout?

@arshad115
Copy link

In your Nestedscrollbar add this propery app:layout_behavior and link to this class.

@arshad115
Copy link

Sorry, it is to be set for the AppbarLayout, not NestedScrollView. I found the original gist here

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