AppBarLayoutUsageBase.java revision f87e4dfc7bcd2583e7908ca3cda65cec6c697080
1/*
2 * Copyright (C) 2015 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
17package com.example.android.support.design.widget;
18
19import android.os.Bundle;
20import android.os.Handler;
21import android.text.TextUtils;
22import android.view.Menu;
23import android.view.MenuItem;
24import android.widget.TextView;
25
26import androidx.appcompat.app.AppCompatActivity;
27import androidx.appcompat.widget.Toolbar;
28import androidx.recyclerview.widget.LinearLayoutManager;
29import androidx.recyclerview.widget.RecyclerView;
30import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
31
32import com.example.android.support.design.Cheeses;
33import com.example.android.support.design.R;
34import com.example.android.support.design.Shakespeare;
35import com.google.android.material.appbar.AppBarLayout;
36import com.google.android.material.appbar.CollapsingToolbarLayout;
37import com.google.android.material.tabs.TabLayout;
38
39import java.util.Random;
40
41abstract class AppBarLayoutUsageBase extends AppCompatActivity {
42
43    @Override
44    protected void onCreate(Bundle savedInstanceState) {
45        super.onCreate(savedInstanceState);
46        setContentView(getLayoutId());
47
48        // Retrieve the Toolbar from our content view, and set it as the action bar
49        Toolbar toolbar = findViewById(R.id.toolbar);
50        setSupportActionBar(toolbar);
51
52        CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)
53                findViewById(R.id.collapsing_app_bar);
54        if (collapsingToolbarLayout != null) {
55            if (displayTitle()) {
56                collapsingToolbarLayout.setTitle(getTitle());
57            }
58            collapsingToolbarLayout.setContentScrimColor(0xFFFF00FF);
59        }
60
61        TextView dialog = findViewById(R.id.textview_dialogue);
62        if (dialog != null) {
63            dialog.setText(TextUtils.concat(Shakespeare.DIALOGUE));
64        }
65
66        RecyclerView recyclerView = findViewById(R.id.appbar_recyclerview);
67        if (recyclerView != null) {
68            setupRecyclerView(recyclerView);
69        }
70
71        TabLayout tabLayout = findViewById(R.id.tabs);
72        if (tabLayout != null) {
73            setupTabs(tabLayout);
74        }
75
76        final SwipeRefreshLayout refreshLayout = findViewById(R.id.swiperefresh);
77        if (refreshLayout != null) {
78            refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
79                private final Handler mHandler = new Handler();
80
81                @Override
82                public void onRefresh() {
83                    // Post a delayed runnable to reset the refreshing state in 2 seconds
84                    mHandler.postDelayed(new Runnable() {
85                        @Override
86                        public void run() {
87                            refreshLayout.setRefreshing(false);
88                        }
89                    }, 2000);
90                }
91            });
92        }
93    }
94
95    @Override
96    public boolean onCreateOptionsMenu(Menu menu) {
97        getMenuInflater().inflate(R.menu.sample_actions, menu);
98        return true;
99    }
100
101    @Override
102    public boolean onOptionsItemSelected(MenuItem item) {
103        switch (item.getItemId()) {
104            case R.id.action_toggle_expand: {
105                AppBarLayout abl = findViewById(R.id.app_bar);
106                abl.setExpanded(true);
107                return true;
108            }
109            case R.id.action_toggle_collapse: {
110                AppBarLayout abl = findViewById(R.id.app_bar);
111                abl.setExpanded(false);
112                return true;
113            }
114        }
115        return super.onOptionsItemSelected(item);
116    }
117
118    private void addRandomTab(TabLayout tabLayout) {
119        Random r = new Random();
120        String cheese = Cheeses.sCheeseStrings[r.nextInt(Cheeses.sCheeseStrings.length)];
121        tabLayout.addTab(tabLayout.newTab().setText(cheese));
122    }
123
124    private void setupTabs(TabLayout tabLayout) {
125        for (int i = 0; i < 10; i++) {
126            addRandomTab(tabLayout);
127        }
128    }
129
130    private void setupRecyclerView(RecyclerView recyclerView) {
131        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
132        recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(this, Cheeses.sCheeseStrings));
133    }
134
135    protected boolean displayTitle() {
136        return true;
137    }
138
139    protected abstract int getLayoutId();
140
141}
142