1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.widget; 18 19 import android.content.Context; 20 import android.graphics.Color; 21 import android.util.AttributeSet; 22 import android.view.View; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.coordinatorlayout.widget.CoordinatorLayout; 26 27 import com.google.android.material.appbar.AppBarLayout; 28 29 /** 30 * This scrolling view behavior will set the background of the {@link AppBarLayout} as 31 * transparent and without the elevation. Also make header overlapped the scrolling child view. 32 */ 33 public class FloatingAppBarScrollingViewBehavior extends AppBarLayout.ScrollingViewBehavior { 34 private boolean initialized; 35 FloatingAppBarScrollingViewBehavior(Context context, AttributeSet attrs)36 public FloatingAppBarScrollingViewBehavior(Context context, AttributeSet attrs) { 37 super(context, attrs); 38 } 39 40 @Override onDependentViewChanged(CoordinatorLayout parent, View child, View dependency)41 public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { 42 boolean changed = super.onDependentViewChanged(parent, child, dependency); 43 if (!initialized && dependency instanceof AppBarLayout) { 44 initialized = true; 45 AppBarLayout appBarLayout = (AppBarLayout) dependency; 46 setAppBarLayoutTransparent(appBarLayout); 47 } 48 return changed; 49 } 50 51 @VisibleForTesting setAppBarLayoutTransparent(AppBarLayout appBarLayout)52 void setAppBarLayoutTransparent(AppBarLayout appBarLayout) { 53 appBarLayout.setBackgroundColor(Color.TRANSPARENT); 54 appBarLayout.setTargetElevation(0); 55 } 56 57 @Override shouldHeaderOverlapScrollingChild()58 protected boolean shouldHeaderOverlapScrollingChild() { 59 return true; 60 } 61 } 62