Home | History | Annotate | Download | only in tabcompat
      1 /*
      2  * Copyright 2012 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.tabcompat;
     18 
     19 import com.example.android.tabcompat.lib.CompatTab;
     20 import com.example.android.tabcompat.lib.CompatTabListener;
     21 import com.example.android.tabcompat.lib.TabCompatActivity;
     22 import com.example.android.tabcompat.lib.TabHelper;
     23 
     24 import android.os.Bundle;
     25 import android.support.v4.app.Fragment;
     26 import android.support.v4.app.FragmentTransaction;
     27 import android.view.Gravity;
     28 import android.view.LayoutInflater;
     29 import android.view.View;
     30 import android.view.ViewGroup;
     31 import android.widget.TextView;
     32 
     33 public class MainActivity extends TabCompatActivity {
     34 
     35     @Override
     36     public void onCreate(Bundle savedInstanceState) {
     37         super.onCreate(savedInstanceState);
     38         setContentView(R.layout.main);
     39 
     40         TabHelper tabHelper = getTabHelper();
     41 
     42         CompatTab photosTab = tabHelper.newTab("photos")
     43                 .setText(R.string.tab_photos)
     44                 .setIcon(R.drawable.ic_tab_photos)
     45                 .setTabListener(new InstantiatingTabListener(this, PhotosFragment.class));
     46         tabHelper.addTab(photosTab);
     47 
     48         CompatTab videosTab = tabHelper.newTab("videos")
     49                 .setText(R.string.tab_videos)
     50                 .setIcon(R.drawable.ic_tab_videos)
     51                 .setTabListener(new InstantiatingTabListener(this, VideosFragment.class));
     52         tabHelper.addTab(videosTab);
     53     }
     54 
     55     /**
     56      * Implementation of {@link CompatTabListener} to handle tab change events. This implementation
     57      * instantiates the specified fragment class with no arguments when its tab is selected.
     58      */
     59     public static class InstantiatingTabListener implements CompatTabListener {
     60 
     61         private final TabCompatActivity mActivity;
     62         private final Class mClass;
     63 
     64         /**
     65          * Constructor used each time a new tab is created.
     66          *
     67          * @param activity The host Activity, used to instantiate the fragment
     68          * @param cls      The class representing the fragment to instantiate
     69          */
     70         public InstantiatingTabListener(TabCompatActivity activity, Class<? extends Fragment> cls) {
     71             mActivity = activity;
     72             mClass = cls;
     73         }
     74 
     75         /* The following are each of the ActionBar.TabListener callbacks */
     76         @Override
     77         public void onTabSelected(CompatTab tab, FragmentTransaction ft) {
     78             // Check if the fragment is already initialized
     79             Fragment fragment = tab.getFragment();
     80             if (fragment == null) {
     81                 // If not, instantiate and add it to the activity
     82                 fragment = Fragment.instantiate(mActivity, mClass.getName());
     83                 tab.setFragment(fragment);
     84                 ft.add(android.R.id.tabcontent, fragment, tab.getTag());
     85             } else {
     86                 // If it exists, simply attach it in order to show it
     87                 ft.attach(fragment);
     88             }
     89         }
     90 
     91         @Override
     92         public void onTabUnselected(CompatTab tab, FragmentTransaction ft) {
     93             Fragment fragment = tab.getFragment();
     94             if (fragment != null) {
     95                 // Detach the fragment, because another one is being attached
     96                 ft.detach(fragment);
     97             }
     98         }
     99 
    100         @Override
    101         public void onTabReselected(CompatTab tab, FragmentTransaction ft) {
    102             // User selected the already selected tab. Do nothing.
    103         }
    104     }
    105 
    106     public static class PhotosFragment extends Fragment {
    107 
    108         @Override
    109         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    110                 Bundle savedInstanceState) {
    111             TextView textView = new TextView(getActivity());
    112             textView.setGravity(Gravity.CENTER);
    113             textView.setText(R.string.tab_photos);
    114             return textView;
    115         }
    116     }
    117 
    118     public static class VideosFragment extends Fragment {
    119 
    120         @Override
    121         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    122                 Bundle savedInstanceState) {
    123             TextView textView = new TextView(getActivity());
    124             textView.setGravity(Gravity.CENTER);
    125             textView.setText(R.string.tab_videos);
    126             return textView;
    127         }
    128     }
    129 }
    130