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