summaryrefslogtreecommitdiffstats
path: root/samples/SupportDesignDemos/src/com/example/android/support/design/widget/TabLayoutUsage.java
blob: 26b61083fea1a94571667fee124d565c8957b963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.support.design.widget;

import com.example.android.support.design.Cheeses;
import com.example.android.support.design.R;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Random;

/**
 * This demonstrates idiomatic usage of TabLayout with a ViewPager
 */
public class TabLayoutUsage extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private CheesePagerAdapter mPagerAdapter;

    private final Random mRandom = new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.design_tabs_viewpager);

        // Retrieve the Toolbar from our content view, and set it as the action bar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mTabLayout = (TabLayout) findViewById(R.id.tabs);
        mViewPager = (ViewPager) findViewById(R.id.tabs_viewpager);

        mPagerAdapter = new CheesePagerAdapter();
        mViewPager.setAdapter(mPagerAdapter);

        mTabLayout.setupWithViewPager(mViewPager);

        setupRadioGroup();
    }

    public void addTab(View view) {
        String cheese = Cheeses.sCheeseStrings[mRandom.nextInt(Cheeses.sCheeseStrings.length)];
        mPagerAdapter.addTab(cheese);
    }

    public void selectFirstTab(View view) {
        if (mTabLayout.getTabCount() > 0) {
            mViewPager.setCurrentItem(0);
        }
    }

    public void removeTab(View view) {
        mPagerAdapter.removeTab();
    }

    private void setupRadioGroup() {
        // Setup the initially checked item
        switch (mTabLayout.getTabMode()) {
            case TabLayout.MODE_SCROLLABLE:
                ((RadioButton) findViewById(R.id.rb_tab_scrollable)).setChecked(true);
                break;
            case TabLayout.MODE_FIXED:
                ((RadioButton) findViewById(R.id.rb_tab_fixed)).setChecked(true);
                break;
        }

        RadioGroup rg = (RadioGroup) findViewById(R.id.radiogroup_tab_mode);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id) {
                switch (id) {
                    case R.id.rb_tab_fixed:
                        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
                        break;
                    case R.id.rb_tab_scrollable:
                        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
                        break;
                }
            }
        });

        // Setup the initially checked item
        switch (mTabLayout.getTabGravity()) {
            case TabLayout.GRAVITY_CENTER:
                ((RadioButton) findViewById(R.id.rb_tab_g_center)).setChecked(true);
                break;
            case TabLayout.GRAVITY_FILL:
                ((RadioButton) findViewById(R.id.rb_tab_g_fill)).setChecked(true);
                break;
        }

        rg = (RadioGroup) findViewById(R.id.radiogroup_tab_gravity);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id) {
                switch (id) {
                    case R.id.rb_tab_g_center:
                        mTabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
                        break;
                    case R.id.rb_tab_g_fill:
                        mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
                        break;
                }
            }
        });
    }

    private static class CheesePagerAdapter extends PagerAdapter {
        private final ArrayList<CharSequence> mCheeses = new ArrayList<>();

        public void addTab(String title) {
            mCheeses.add(title);
            notifyDataSetChanged();
        }

        public void removeTab() {
            if (!mCheeses.isEmpty()) {
                mCheeses.remove(mCheeses.size() - 1);
                notifyDataSetChanged();
            }
        }

        @Override
        public int getCount() {
            return mCheeses.size();
        }

        @Override
        public int getItemPosition(Object object) {
            final Item item = (Item) object;
            final int index = mCheeses.indexOf(item.cheese);
            return index >= 0 ? index : POSITION_NONE;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            final TextView tv = new TextView(container.getContext());
            tv.setText(getPageTitle(position));
            tv.setGravity(Gravity.CENTER);
            tv.setTextAppearance(tv.getContext(), R.style.TextAppearance_AppCompat_Title);
            container.addView(tv, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);

            Item item = new Item();
            item.cheese = mCheeses.get(position);
            item.view = tv;
            return item;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            final Item item = (Item) object;
            return item.view == view;
        }

        @Override
         public CharSequence getPageTitle(int position) {
            return mCheeses.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            final Item item = (Item) object;
            container.removeView(item.view);
        }

        private static class Item {
            TextView view;
            CharSequence cheese;
        }
    }

}