TabLayoutUsage.java revision 3a98e54c124caefaa211aa3e37bdb1cbc427416a
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;
21
22import android.os.Bundle;
23import android.support.design.widget.TabLayout;
24import android.support.v4.view.PagerAdapter;
25import android.support.v4.view.ViewPager;
26import android.support.v7.app.AppCompatActivity;
27import android.support.v7.widget.Toolbar;
28import android.view.Gravity;
29import android.view.View;
30import android.view.ViewGroup;
31import android.widget.RadioButton;
32import android.widget.RadioGroup;
33import android.widget.TextView;
34
35import java.util.ArrayList;
36import java.util.Random;
37
38/**
39 * This demonstrates idiomatic usage of TabLayout with a ViewPager
40 */
41public class TabLayoutUsage extends AppCompatActivity {
42
43    private TabLayout mTabLayout;
44    private ViewPager mViewPager;
45    private CheesePagerAdapter mPagerAdapter;
46
47    @Override
48    protected void onCreate(Bundle savedInstanceState) {
49        super.onCreate(savedInstanceState);
50        setContentView(R.layout.design_tabs_viewpager);
51
52        // Retrieve the Toolbar from our content view, and set it as the action bar
53        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
54        setSupportActionBar(toolbar);
55        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
56
57        mTabLayout = (TabLayout) findViewById(R.id.tabs);
58
59        mViewPager = (ViewPager) findViewById(R.id.tabs_viewpager);
60        mPagerAdapter = new CheesePagerAdapter();
61        mViewPager.setAdapter(mPagerAdapter);
62        mViewPager.setOnPageChangeListener(mTabLayout.createOnPageChangeListener());
63        mTabLayout.setOnTabSelectedListener(mTabListener);
64
65        setupButtons();
66        setupRadioGroup();
67    }
68
69    private void setupButtons() {
70        findViewById(R.id.btn_add_tab).setOnClickListener(new View.OnClickListener() {
71            @Override
72            public void onClick(View view) {
73                addRandomTab();
74            }
75        });
76
77        findViewById(R.id.btn_remove_tab).setOnClickListener(new View.OnClickListener() {
78            @Override
79            public void onClick(View view) {
80                if (mTabLayout.getTabCount() >= 1) {
81                    mTabLayout.removeTabAt(mTabLayout.getTabCount() - 1);
82                    mPagerAdapter.removeTab();
83                }
84            }
85        });
86    }
87
88    private void addRandomTab() {
89        Random r = new Random();
90        String cheese = Cheeses.sCheeseStrings[r.nextInt(Cheeses.sCheeseStrings.length)];
91        mTabLayout.addTab(mTabLayout.newTab().setText(cheese));
92        mPagerAdapter.addTab(cheese);
93    }
94
95    private void setupRadioGroup() {
96        // Setup the initially checked item
97        switch (mTabLayout.getTabMode()) {
98            case TabLayout.MODE_SCROLLABLE:
99                ((RadioButton) findViewById(R.id.rb_tab_scrollable)).setChecked(true);
100                break;
101            case TabLayout.MODE_FIXED:
102                ((RadioButton) findViewById(R.id.rb_tab_fixed)).setChecked(true);
103                break;
104        }
105
106        RadioGroup rg = (RadioGroup) findViewById(R.id.radiogroup_tab_mode);
107        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
108            @Override
109            public void onCheckedChanged(RadioGroup radioGroup, int id) {
110                switch (id) {
111                    case R.id.rb_tab_fixed:
112                        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
113                        break;
114                    case R.id.rb_tab_scrollable:
115                        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
116                        break;
117                }
118            }
119        });
120
121        // Setup the initially checked item
122        switch (mTabLayout.getTabGravity()) {
123            case TabLayout.GRAVITY_CENTER:
124                ((RadioButton) findViewById(R.id.rb_tab_g_center)).setChecked(true);
125                break;
126            case TabLayout.GRAVITY_FILL:
127                ((RadioButton) findViewById(R.id.rb_tab_g_fill)).setChecked(true);
128                break;
129        }
130
131        rg = (RadioGroup) findViewById(R.id.radiogroup_tab_gravity);
132        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
133            @Override
134            public void onCheckedChanged(RadioGroup radioGroup, int id) {
135                switch (id) {
136                    case R.id.rb_tab_g_center:
137                        mTabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
138                        break;
139                    case R.id.rb_tab_g_fill:
140                        mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
141                        break;
142                }
143            }
144        });
145    }
146
147    private final TabLayout.OnTabSelectedListener
148            mTabListener = new TabLayout.OnTabSelectedListener() {
149        @Override
150        public void onTabSelected(TabLayout.Tab tab) {
151            mViewPager.setCurrentItem(tab.getPosition());
152        }
153
154        @Override
155        public void onTabUnselected(TabLayout.Tab tab) {
156            // no-op
157        }
158
159        @Override
160        public void onTabReselected(TabLayout.Tab tab) {
161            // no-op
162        }
163    };
164
165    private static class CheesePagerAdapter extends PagerAdapter {
166
167        private final ArrayList<CharSequence> mCheeses = new ArrayList<>();
168
169        public void addTab(String title) {
170            mCheeses.add(title);
171            notifyDataSetChanged();
172        }
173
174        public void removeTab() {
175            if (!mCheeses.isEmpty()) {
176                mCheeses.remove(mCheeses.size() - 1);
177                notifyDataSetChanged();
178            }
179        }
180
181        @Override
182        public int getCount() {
183            return mCheeses.size();
184        }
185
186        @Override
187        public Object instantiateItem(ViewGroup container, int position) {
188            TextView tv = new TextView(container.getContext());
189            tv.setText(getPageTitle(position));
190            tv.setGravity(Gravity.CENTER);
191            tv.setTextAppearance(tv.getContext(), R.style.TextAppearance_AppCompat_Title);
192
193            container.addView(tv, ViewGroup.LayoutParams.MATCH_PARENT,
194                    ViewGroup.LayoutParams.MATCH_PARENT);
195
196            return tv;
197        }
198
199        @Override
200        public boolean isViewFromObject(View view, Object object) {
201            return view == object;
202        }
203
204        @Override
205         public CharSequence getPageTitle(int position) {
206            return mCheeses.get(position);
207        }
208
209        @Override
210        public void destroyItem(ViewGroup container, int position, Object object) {
211            container.removeView((View) object);
212        }
213    }
214
215}
216