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.app.ActionBar;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.FrameLayout;
26 import android.widget.Toolbar;
27 
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 import androidx.coordinatorlayout.widget.CoordinatorLayout;
31 import androidx.fragment.app.Fragment;
32 
33 import com.google.android.material.appbar.AppBarLayout;
34 import com.google.android.material.appbar.CollapsingToolbarLayout;
35 
36 /**
37  * A base fragment that has a collapsing toolbar layout for enabling the collapsing toolbar design.
38  */
39 public abstract class CollapsingToolbarBaseFragment extends Fragment {
40 
41     private class DelegateCallback implements CollapsingToolbarDelegate.HostCallback {
42         @Nullable
43         @Override
setActionBar(Toolbar toolbar)44         public ActionBar setActionBar(Toolbar toolbar) {
45             requireActivity().setActionBar(toolbar);
46             return null;
47         }
48 
49         @Override
setOuterTitle(CharSequence title)50         public void setOuterTitle(CharSequence title) {
51             // ignore
52         }
53     }
54 
55     private CollapsingToolbarDelegate mToolbardelegate;
56 
57     @Override
onAttach(Context context)58     public void onAttach(Context context) {
59         super.onAttach(context);
60         mToolbardelegate = new CollapsingToolbarDelegate(new DelegateCallback());
61     }
62 
63     @Nullable
64     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)65     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
66             @Nullable Bundle savedInstanceState) {
67         return mToolbardelegate.onCreateView(inflater, container);
68     }
69 
70     /**
71      * Return an instance of CoordinatorLayout.
72      */
73     @Nullable
getCoordinatorLayout()74     public CoordinatorLayout getCoordinatorLayout() {
75         return mToolbardelegate.getCoordinatorLayout();
76     }
77 
78     /**
79      * Return an instance of app bar.
80      */
81     @Nullable
getAppBarLayout()82     public AppBarLayout getAppBarLayout() {
83         return mToolbardelegate.getAppBarLayout();
84     }
85 
86     /**
87      * Return the collapsing toolbar layout.
88      */
89     @Nullable
getCollapsingToolbarLayout()90     public CollapsingToolbarLayout getCollapsingToolbarLayout() {
91         return mToolbardelegate.getCollapsingToolbarLayout();
92     }
93 
94     /**
95      * Return the content frame layout.
96      */
97     @NonNull
getContentFrameLayout()98     public FrameLayout getContentFrameLayout() {
99         return mToolbardelegate.getContentFrameLayout();
100     }
101 }
102