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