This interface is deprecated.
Action bar navigation modes are deprecated and not supported by inline toolbar action bars. Consider using other common navigation patterns instead.

Adding Navigation Tabs


Figure 7. Action bar tabs on a wide screen.

  Tabs in the action bar make it easy for users to explore and switch between different views in your app. The tabs provided by the ActionBar are ideal because they adapt to different screen sizes. For example, when the screen is wide enough the tabs appear in the action bar alongside the action buttons (such as when on a tablet, shown in figure 7), while when on a narrow screen they appear in a separate bar (known as the "stacked action bar", shown in figure 8). In some cases, the Android system will instead show your tab items as a drop-down list to ensure the best fit in the action bar.

Figure 8. Tabs on a narrow screen.

  Tab模式并不一定显示为图8那样,要看屏幕大小,够大时显示如图7,有些情况还可能显示为下拉列表

  To get started, your layout must include a ViewGroup in which you place each Fragment associated with a tab. Be sure the ViewGroup has a resource ID so you can reference it from your code and swap the tabs within it. Alternatively, if the tab content will fill the activity layout, then your activity doesn't need a layout at all (you don't even need to call setContentView()). Instead, you can place each fragment in the default root view, which you can refer to with the android.R.id.content ID.

  如果每个tab对应的fragment都是填满宿主activity的,那么宿主activity可以不用有layout文件,不用setContentView(),可以让fragment寄生在default root view上,它的id是android.R.id.content 

Once you determine where the fragments appear in the layout, the basic procedure to add tabs is:

  开启顶部tab模式的步骤如下:
  1. Implement the ActionBar.TabListener interface. This interface provides callbacks for tab events, such as when the user presses one so you can swap the tabs.

    实现tab接口
  2. For each tab you want to add, instantiate an ActionBar.Tab and set the ActionBar.TabListener by calling setTabListener(). Also set the tab's title and with setText() (and optionally, an icon with setIcon()).
    构造一个ActionBar.Tab类,设置tab接口,标题,图标.
  3. Then add each tab to the action bar by calling addTab()
    调用actionBar.addTab();

  Notice that the ActionBar.TabListener callback methods don't specify which fragment is associated with the tab, but merely which ActionBar.Tab was selected. You must define your own association between each ActionBar.Tab and the appropriate Fragment that it represents. There are several ways you can define the association, depending on your design.

  For example, here's how you might implement the ActionBar.TabListener such that each tab uses its own instance of the listener:

 public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
     private Fragment mFragment;
     private final Activity mActivity;
     private final String mTag;
     private final Class<T> mClass;

     /** Constructor used each time a new tab is created.
       * @param activity  The host Activity, used to instantiate the fragment
       * @param tag  The identifier tag for the fragment
       * @param clz  The fragment's Class, used to instantiate the fragment
       */
     public TabListener(Activity activity, String tag, Class<T> clz) {
         mActivity = activity;
         mTag = tag;
         mClass = clz;
     }

     /* The following are each of the ActionBar.TabListener callbacks */

     public void onTabSelected(Tab tab, FragmentTransaction ft) {
         // Check if the fragment is already initialized
         if (mFragment == null) {
             // If not, instantiate and add it to the activity
             mFragment = Fragment.instantiate(mActivity, mClass.getName());
             ft.add(android.R.id.content, mFragment, mTag);
         } else {
             // If it exists, simply attach it in order to show it
             ft.attach(mFragment);
         }
     }

     public void onTabUnselected(Tab tab, FragmentTransaction ft) {
         if (mFragment != null) {
             // Detach the fragment, because another one is being attached
             ft.detach(mFragment);
         }
     }

     public void onTabReselected(Tab tab, FragmentTransaction ft) {
         // User selected the already selected tab. Usually do nothing.
     }
 }

Caution: You must not call commit() for the fragment transaction in each of these callbacks—the system calls it for you and it may throw an exception if you call it yourself. You also cannot add these fragment transactions to the back stack.

注意在ActionBar.TabListener的回调函数中有个FragmentTransaction ft ,不能调用它的commit函数,否则出异常,系统会帮你调用.

  In this example, the listener simply attaches (attach()) a fragment to the activity layout—or if not instantiated, creates the fragment and adds (add()) it to the layout (as a child of the android.R.id.content view group)—when the respective tab is selected, and detaches (detach()) it when the tab is unselected.

  All that remains is to create each ActionBar.Tab and add it to the ActionBar. Additionally, you must call setNavigationMode(NAVIGATION_MODE_TABS) to make the tabs visible.

  For example, the following code adds two tabs using the listener defined above:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     // Notice that setContentView() is not used, because we use the root
     // android.R.id.content as the container for each fragment

     // setup action bar for tabs
     ActionBar actionBar = getSupportActionBar();
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
     actionBar.setDisplayShowTitleEnabled(false);

     Tab tab = actionBar.newTab()
                        .setText(R.string.artist)
                        .setTabListener(new TabListener<ArtistFragment>(
                                this, "artist", ArtistFragment.class));
     actionBar.addTab(tab);

     tab = actionBar.newTab()
                    .setText(R.string.album)
                    .setTabListener(new TabListener<AlbumFragment>(
                            this, "album", AlbumFragment.class));
     actionBar.addTab(tab);
 }

  If your activity stops, you should retain the currently selected tab with the saved instance state so you can open the appropriate tab when the user returns. When it's time to save the state, you can query the currently selected tab with getSelectedNavigationIndex(). This returns the index position of the selected tab.

注意要保存tab对应的fragment的状态.getSelectedNavigationIndex()可以得到被选中的tab的index

Caution: It's important that you save the state of each fragment so when users switch fragments with the tabs and then return to a previous fragment, it looks the way it did when they left. Some of the state is saved by default, but you may need to manually save state for customized views. For information about saving the state of your fragment, see the Fragments API guide.

Note: The above implementation for ActionBar.TabListener is one of several possible techniques. Another popular option is to use ViewPager to manage the fragments so users can also use a swipe gesture to switch tabs. In this case, you simply tell the ViewPager the current tab position in the onTabSelected() callback. For more information, read Creating Swipe Views with Tabs.

ActionBar官方教程(9)ActionBar的顶部tab模式(注意,已经被弃用)的更多相关文章

  1. ActionBar官方教程(10)ActionBar的下拉列表模式

    Adding Drop-down Navigation As another mode of navigation (or filtering) for your activity, the acti ...

  2. ActionBar官方教程(5)ActionBar的分裂模式(底部tab样式),隐藏标题,隐藏图标

    Using split action bar Split action bar provides a separate bar at the bottom of the screen to displ ...

  3. ActionBar官方教程(11)自定义ActionBar的样式(含重要的样式属性表及练习示例)

    Styling the Action Bar If you want to implement a visual design that represents your app's brand, th ...

  4. ActionBar官方教程(2)选主题让应用支或不支持ActionBar及支持ActionBar的应用如何隐藏和显示

    Adding the Action Bar As mentioned above, this guide focuses on how to use the ActionBar APIs in the ...

  5. 【转】 Pro Android学习笔记(五一):ActionBar(4):标准和Tab模式

    之前,我们学习的Action Bar是标准模式,Tab模式的如下图所示. 对于Tab,我们在Android学习笔记(二二): 多页显示-Tag的使用中学习过,但Action Bar的tab更适合fra ...

  6. ActionBar官方教程(1)简介及各区域介绍

    Action Bar The action bar is a window feature that identifies the user location, and provides user a ...

  7. ActionBar官方教程(6)把图标变成一个返回到上级的按钮,同一个app间,不同app间,不同fragment间

    Navigating Up with the App Icon Enabling the app icon as an Up button allows the user to navigate yo ...

  8. ActionBar官方教程(3)更改标题处的图片

    Using a logo instead of an icon By default, the system uses your application icon in the action bar, ...

  9. ActionBar官方教程(8)ShareActionProvider与自定义操作项提供器

    Adding an Action Provider Similar to an action view, an action provider replaces an action button wi ...

随机推荐

  1. 安装.NET Framework组件时,电脑意外重启后再次安装失败

    因为软件运行环境需要安装.Net Framework,我安装的是2.0sp版本,可以安装过程中计算机意外关闭,重新打开后再次安装却出现安装失败的提示,具体内容是: 产品: Microsoft .NET ...

  2. 几个动画demo

    一.点击扩散效果 这个效果没什么难度,主要是加深对核心动画的理解和使用,但是还是有几个想说明的地方.先看一下效果,具体内容代码里有注释. // // CircleButton.m // UITest ...

  3. CSS3滤镜!!!

    <!DOCTYPE html> <html> <head> <style> img { width: 33%; height: auto; float: ...

  4. [翻译]AOP编程

    翻译文章链接http://www.codeproject.com/Articles/1080517/Aspect-Oriented-Programming-using-Interceptors-wit ...

  5. Java获取线程的对象和名称

    /*获取线程对象以及名称(很有意义的) 原来线程都有自己默认的名称Thread-编号  该编号从0开始 Thread 父类的方法static  Thread currentThread() :获取当前 ...

  6. 转载:一句代码改变Swing难看的字体

    Swing 皮肤的一个键值:swing.boldMetal 默认为 true因此造成了默认字体极度难看: 其实一句代码就能解决问题:UIManager.put("swing.boldMeta ...

  7. CENTOS7 使用网络管理器配置静态IP地址

    CENTOS7 的网络配置和CENTOS6有些不同. 如果你想要使用网络管理器来管理该接口,你可以使用nmtui(网络管理器文本用户界面),它提供了在终端环境中配置配置网络管理器的方式. 在使用nmt ...

  8. C/C++代码检视实例

    相关文章链接如下: 微软过桥问题与测试人员素养 等价类分法 新解 测试用例设计中的NP难题 90%程序员写不出无BUG的二分查找程序? C/C++代码检视要点 4.1             代码检视 ...

  9. PHP上传文件大小限制问题 post_max_size对大小的影响及解决方法

    今天在操作php上传的时候发现了一个问题,就是当php脚步上传的文件大小超过php.ini中post_max_size的限制的时候页面不会给出提醒,文件也上传失败,这个问题感觉应该算是一个另类,今天跟 ...

  10. linux磁盘设备知识

    linux分区数字编号: 1.分区数字编号1至4留给主分区或扩展分区使用,逻辑分区编号从5开始. 2.IDE硬盘设备名均以/dev/hd开头,不同硬盘编号依次是/dev/hda/./dev/hdb./ ...