妈个蛋,网上找了一圈,没一个靠谱的
public class ToolbarAlphaScrollBehavior extends CoordinatorLayout.Behavior{ public ToolbarAlphaScrollBehavior() { } public ToolbarAlphaScrollBehavior(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean layoutDependsOn(CoordinatorLayout parent, Toolbar child, View dependency) { return dependency instanceof AppBarLayout; } @Override public boolean onInterceptTouchEvent(CoordinatorLayout parent, Toolbar child, MotionEvent ev) { return ev == null || super.onInterceptTouchEvent(parent, child, ev); } @Override public boolean onDependentViewChanged(CoordinatorLayout parent, Toolbar child, View dependency) { if (dependency instanceof AppBarLayout) { float ratio = (float) getCurrentScrollValue(child, dependency) / getTotalScrollRange(child, dependency); float alpha = 1f - Math.min(1f, Math.max(0f, ratio)); int drawableAlpha = (int) (alpha * 255); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { child.getBackground().setAlpha(drawableAlpha); } else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { ViewGroup toolbarParent = (ViewGroup) child.getParent(); if (toolbarParent.getChildCount() == 2) { int count = toolbarParent.getChildCount(); for (int i = count - 1; i >= 0; i--) { toolbarParent.getChildAt(i).getBackground().setAlpha(drawableAlpha); } } child.getBackground().setAlpha(drawableAlpha); } else { child.getBackground().setAlpha(drawableAlpha); } if (child.getChildCount() != 0) child.getChildAt(0).setAlpha(alpha); } return false; } private int getCurrentScrollValue(Toolbar child, View dependency) { return dependency.getBottom() - child.getTop(); } private float getTotalScrollRange(Toolbar child, View dependency) { return Math.max(dependency.getHeight(), ((AppBarLayout) dependency).getTotalScrollRange()) - child.getTop(); }}
初始化设置toolbar透明度 toolbar.getBackground().setAlpha(0);
onDependentViewChanged 主要在这里判断滑动,设置toolbar及其子项透明度变化
效果图:
参考:
http://gold.xitu.io/entry/575d885d2e958a0069e88536