1 /*
2  * Copyright (C) 2021 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.settingslib.collapsingtoolbar;
18 
19 import android.os.Build;
20 import android.os.Bundle;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.FrameLayout;
25 import android.widget.Toolbar;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.coordinatorlayout.widget.CoordinatorLayout;
30 import androidx.fragment.app.Fragment;
31 
32 import com.google.android.material.appbar.AppBarLayout;
33 import com.google.android.material.appbar.CollapsingToolbarLayout;
34 
35 /**
36  * A base fragment that has a collapsing toolbar layout for enabling the collapsing toolbar design.
37  */
38 public abstract class CollapsingToolbarBaseFragment extends Fragment {
39 
40     private static final float TOOLBAR_LINE_SPACING_MULTIPLIER = 1.1f;
41 
42     @Nullable
43     private CoordinatorLayout mCoordinatorLayout;
44     @Nullable
45     private CollapsingToolbarLayout mCollapsingToolbarLayout;
46     @Nullable
47     private AppBarLayout mAppBarLayout;
48     @NonNull
49     private Toolbar mToolbar;
50     @NonNull
51     private FrameLayout mContentFrameLayout;
52 
53     @Nullable
54     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)55     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
56             @Nullable Bundle savedInstanceState) {
57         final View view = inflater.inflate(R.layout.collapsing_toolbar_base_layout, container,
58                 false);
59         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
60             mCoordinatorLayout = view.findViewById(R.id.content_parent);
61         }
62         mCollapsingToolbarLayout = view.findViewById(R.id.collapsing_toolbar);
63         mAppBarLayout = view.findViewById(R.id.app_bar);
64         if (mCollapsingToolbarLayout != null) {
65             mCollapsingToolbarLayout.setLineSpacingMultiplier(TOOLBAR_LINE_SPACING_MULTIPLIER);
66         }
67         disableCollapsingToolbarLayoutScrollingBehavior();
68         mToolbar = view.findViewById(R.id.action_bar);
69         mContentFrameLayout = view.findViewById(R.id.content_frame);
70         return view;
71     }
72 
73     @Override
onActivityCreated(@ullable Bundle savedInstanceState)74     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
75         super.onActivityCreated(savedInstanceState);
76 
77         requireActivity().setActionBar(mToolbar);
78     }
79 
80     /**
81      * Return an instance of CoordinatorLayout.
82      */
83     @Nullable
getCoordinatorLayout()84     public CoordinatorLayout getCoordinatorLayout() {
85         return mCoordinatorLayout;
86     }
87 
88     /**
89      * Return an instance of app bar.
90      */
91     @Nullable
getAppBarLayout()92     public AppBarLayout getAppBarLayout() {
93         return mAppBarLayout;
94     }
95 
96     /**
97      * Return the collapsing toolbar layout.
98      */
99     @Nullable
getCollapsingToolbarLayout()100     public CollapsingToolbarLayout getCollapsingToolbarLayout() {
101         return mCollapsingToolbarLayout;
102     }
103 
104     /**
105      * Return the content frame layout.
106      */
107     @NonNull
getContentFrameLayout()108     public FrameLayout getContentFrameLayout() {
109         return mContentFrameLayout;
110     }
111 
disableCollapsingToolbarLayoutScrollingBehavior()112     private void disableCollapsingToolbarLayoutScrollingBehavior() {
113         if (mAppBarLayout == null) {
114             return;
115         }
116         final CoordinatorLayout.LayoutParams params =
117                 (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
118         final AppBarLayout.Behavior behavior = new AppBarLayout.Behavior();
119         behavior.setDragCallback(
120                 new AppBarLayout.Behavior.DragCallback() {
121                     @Override
122                     public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
123                         return false;
124                     }
125                 });
126         params.setBehavior(behavior);
127     }
128 }
129