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