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