Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2010 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 package com.example.android.supportv7.app;
     17 
     18 import android.os.Bundle;
     19 import android.support.v4.app.Fragment;
     20 import android.support.v4.app.FragmentTransaction;
     21 import android.support.v7.app.ActionBar;
     22 import android.support.v7.app.ActionBar.Tab;
     23 import android.support.v7.app.ActionBarActivity;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.TextView;
     28 import android.widget.Toast;
     29 import com.example.android.supportv7.R;
     30 
     31 /**
     32  * This demonstrates the use of action bar tabs and how they interact
     33  * with other action bar features.
     34  */
     35 public class ActionBarTabs extends ActionBarActivity {
     36     @Override
     37     protected void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39 
     40         setContentView(R.layout.action_bar_tabs);
     41     }
     42 
     43     public void onAddTab(View v) {
     44         final ActionBar bar = getSupportActionBar();
     45         final int tabCount = bar.getTabCount();
     46         final String text = "Tab " + tabCount;
     47         bar.addTab(bar.newTab()
     48                 .setText(text)
     49                 .setTabListener(new TabListener(new TabContentFragment(text))));
     50     }
     51 
     52     public void onRemoveTab(View v) {
     53         final ActionBar bar = getSupportActionBar();
     54         if (bar.getTabCount() > 0) {
     55             bar.removeTabAt(bar.getTabCount() - 1);
     56         }
     57     }
     58 
     59     public void onToggleTabs(View v) {
     60         final ActionBar bar = getSupportActionBar();
     61 
     62         if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
     63             bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
     64             bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
     65         } else {
     66             bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
     67             bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
     68         }
     69     }
     70 
     71     public void onRemoveAllTabs(View v) {
     72         getSupportActionBar().removeAllTabs();
     73     }
     74 
     75     /**
     76      * A TabListener receives event callbacks from the action bar as tabs
     77      * are deselected, selected, and reselected. A FragmentTransaction
     78      * is provided to each of these callbacks; if any operations are added
     79      * to it, it will be committed at the end of the full tab switch operation.
     80      * This lets tab switches be atomic without the app needing to track
     81      * the interactions between different tabs.
     82      *
     83      * NOTE: This is a very simple implementation that does not retain
     84      * fragment state of the non-visible tabs across activity instances.
     85      * Look at the FragmentTabs example for how to do a more complete
     86      * implementation.
     87      */
     88     private class TabListener implements ActionBar.TabListener {
     89         private TabContentFragment mFragment;
     90 
     91         public TabListener(TabContentFragment fragment) {
     92             mFragment = fragment;
     93         }
     94 
     95         @Override
     96         public void onTabSelected(Tab tab, FragmentTransaction ft) {
     97             ft.add(R.id.fragment_content, mFragment, mFragment.getText());
     98         }
     99 
    100         @Override
    101         public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    102             ft.remove(mFragment);
    103         }
    104 
    105         @Override
    106         public void onTabReselected(Tab tab, FragmentTransaction ft) {
    107             Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show();
    108         }
    109 
    110     }
    111 
    112     private class TabContentFragment extends Fragment {
    113         private String mText;
    114 
    115         public TabContentFragment(String text) {
    116             mText = text;
    117         }
    118 
    119         public String getText() {
    120             return mText;
    121         }
    122 
    123         @Override
    124         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    125                 Bundle savedInstanceState) {
    126             View fragView = inflater.inflate(R.layout.action_bar_tab_content, container, false);
    127 
    128             TextView text = (TextView) fragView.findViewById(R.id.text);
    129             text.setText(mText);
    130 
    131             return fragView;
    132         }
    133     }
    134 }
    135