diff --git a/res/layout/settings_homepage_container.xml b/res/layout/settings_homepage_container.xml index 8d81e824642..75d449fe889 100644 --- a/res/layout/settings_homepage_container.xml +++ b/res/layout/settings_homepage_container.xml @@ -21,24 +21,17 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - - - - + app:layout_behavior="com.android.settings.widget.FloatingAppBarScrollingViewBehavior"> + + + + diff --git a/res/values/dimens.xml b/res/values/dimens.xml index 83848af419b..a6c6c37df50 100755 --- a/res/values/dimens.xml +++ b/res/values/dimens.xml @@ -105,6 +105,8 @@ 32dp 4dp 16dp + + 80dp 16dp diff --git a/src/com/android/settings/widget/FloatingAppBarScrollingViewBehavior.java b/src/com/android/settings/widget/FloatingAppBarScrollingViewBehavior.java new file mode 100644 index 00000000000..23f6ccd4b92 --- /dev/null +++ b/src/com/android/settings/widget/FloatingAppBarScrollingViewBehavior.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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. + */ + +package com.android.settings.widget; + +import android.content.Context; +import android.graphics.Color; +import android.util.AttributeSet; +import android.view.View; + +import androidx.annotation.VisibleForTesting; +import androidx.coordinatorlayout.widget.CoordinatorLayout; + +import com.google.android.material.appbar.AppBarLayout; + +/** + * This scrolling view behavior will set the background of the {@link AppBarLayout} as + * transparent and without the elevation. Also make header overlapped the scrolling child view. + */ +public class FloatingAppBarScrollingViewBehavior extends AppBarLayout.ScrollingViewBehavior { + private boolean initialized; + + public FloatingAppBarScrollingViewBehavior(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { + boolean changed = super.onDependentViewChanged(parent, child, dependency); + if (!initialized && dependency instanceof AppBarLayout) { + initialized = true; + AppBarLayout appBarLayout = (AppBarLayout) dependency; + setAppBarLayoutTransparent(appBarLayout); + } + return changed; + } + + @VisibleForTesting + void setAppBarLayoutTransparent(AppBarLayout appBarLayout) { + appBarLayout.setBackgroundColor(Color.TRANSPARENT); + appBarLayout.setTargetElevation(0); + } + + @Override + protected boolean shouldHeaderOverlapScrollingChild() { + return true; + } +} diff --git a/tests/robotests/src/com/android/settings/widget/FloatingAppBarScrollingViewBehaviorTest.java b/tests/robotests/src/com/android/settings/widget/FloatingAppBarScrollingViewBehaviorTest.java new file mode 100644 index 00000000000..6acc4d984fe --- /dev/null +++ b/tests/robotests/src/com/android/settings/widget/FloatingAppBarScrollingViewBehaviorTest.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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. + */ + +package com.android.settings.widget; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.Context; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; + +import com.android.settings.R; +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +import com.google.android.material.appbar.AppBarLayout; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RuntimeEnvironment; + +@RunWith(SettingsRobolectricTestRunner.class) +public class FloatingAppBarScrollingViewBehaviorTest { + + private FloatingAppBarScrollingViewBehavior mScrollingViewBehavior; + private Context mContext; + + @Before + public void setUp() { + mContext = RuntimeEnvironment.application; + mScrollingViewBehavior = new FloatingAppBarScrollingViewBehavior(mContext, + Robolectric.buildAttributeSet().build()); + } + + @Test + public void shouldHeaderOverlapScrollingChild_returnTrue() { + assertThat(mScrollingViewBehavior.shouldHeaderOverlapScrollingChild()).isTrue(); + } + + @Test + public void setAppBarLayoutTransparent_backgroundDefaultAsWhite_shouldBeTransparent() { + mContext.setTheme(R.style.Theme_Settings_Home); + final AppBarLayout appBarLayout = new AppBarLayout(mContext); + appBarLayout.setBackgroundColor(Color.WHITE); + mScrollingViewBehavior.setAppBarLayoutTransparent(appBarLayout); + assertThat(((ColorDrawable) appBarLayout.getBackground()).getColor()).isEqualTo( + Color.TRANSPARENT); + } +}