Adding Drop-down Navigation


  As another mode of navigation (or filtering) for your activity, the action bar offers a built in drop-down list (also known as a "spinner"). For example, the drop-down list can offer different modes by which content in the activity is sorted.

Figure 9. A drop-down navigation list in the action bar.

  Using the drop-down list is useful when changing the content is important but not necessarily a frequent occurrence. In cases where switching the content is more frequent, you should use navigation tabsinstead.

  The basic procedure to enable drop-down navigation is:

  ActionBar开启下拉列表模式的基本步骤
  1. Create a SpinnerAdapter that provides the list of selectable items for the drop-down and the layout to use when drawing each item in the list.

    准备下拉list的adapter
  2. Implement ActionBar.OnNavigationListener to define the behavior that occurs when the user selects an item from the list.
    实现ActionBar.OnNavigationListener,处理下拉列表的item事件
  3. During your activity's onCreate() method, enable the action bar's drop-down list by calling setNavigationMode(NAVIGATION_MODE_LIST).
    设备ActionBar.setNavigationMode(NAVIGATION_MODE_LIST);设置ActionBar为下拉模式
  4. Set the callback for the drop-down list with setListNavigationCallbacks(). For example:
    注册SpinnerAdapter 和 ActionBar.OnNavigationListener
    actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);

    This method takes your SpinnerAdapter and ActionBar.OnNavigationListener.

  This procedure is relatively short, but implementing the SpinnerAdapter and ActionBar.OnNavigationListener is where most of the work is done. There are many ways you can implement these to define the functionality for your drop-down navigation and implementing various types of SpinnerAdapter is beyond the scope of this document (you should refer to the SpinnerAdapter class reference for more information). However, below is an example for a SpinnerAdapter and ActionBar.OnNavigationListener to get you started (click the title to reveal the sample).

Example SpinnerAdapter and OnNavigationListener

  SpinnerAdapter is an adapter that provides data for a spinner widget, such as the drop-down list in the action bar. SpinnerAdapter is an interface that you can implement, but Android includes some useful implementations that you can extend, such as ArrayAdapter and SimpleCursorAdapter. For example, here's an easy way to create a SpinnerAdapter by using ArrayAdapter implementation, which uses a string array as the data source:

  SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this,
        R.array.action_list, android.R.layout.simple_spinner_dropdown_item);
  The createFromResource() method takes three parameters: the application Context, the resource ID for the string array, and the layout to use for each list item.

string array defined in a resource looks like this:

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
     <string-array name="action_list">
         <item>Mercury</item>
         <item>Venus</item>
         <item>Earth</item>
     </string-array>
 </resources>

  The ArrayAdapter returned by createFromResource() is complete and ready for you to pass it to setListNavigationCallbacks() (in step 4 from above). Before you do, though, you need to create the OnNavigationListener.

  Your implementation of ActionBar.OnNavigationListener is where you handle fragment changes or other modifications to your activity when the user selects an item from the drop-down list. There's only one callback method to implement in the listener: onNavigationItemSelected().

  The onNavigationItemSelected() method receives the position of the item in the list and a unique item ID provided by the SpinnerAdapter.

Here's an example that instantiates an anonymous implementation of OnNavigationListener, which inserts a Fragment into the layout container identified by R.id.fragment_container:

 mOnNavigationListener = new OnNavigationListener() {
   // Get the same strings provided for the drop-down's ArrayAdapter
   String[] strings = getResources().getStringArray(R.array.action_list);

   @Override
   public boolean onNavigationItemSelected(int position, long itemId) {
     // Create new fragment from our own Fragment class
     ListContentFragment newFragment = new ListContentFragment();
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

     // Replace whatever is in the fragment container with this fragment
     // and give the fragment a tag name equal to the string at the position
     // selected
     ft.replace(R.id.fragment_container, newFragment, strings[position]);

     // Apply changes
     ft.commit();
     return true;
   }
 };

  This instance of OnNavigationListener is complete and you can now callsetListNavigationCallbacks() (in step 4), passing the ArrayAdapter and this OnNavigationListener.

  In this example, when the user selects an item from the drop-down list, a fragment is added to the layout (replacing the current fragment in the R.id.fragment_container view). The fragment added is given a tag that uniquely identifies it, which is the same string used to identify the fragment in the drop-down list.

Here's a look at the ListContentFragment class that defines each fragment in this example:

 public class ListContentFragment extends Fragment {
     private String mText;

     @Override
     public void onAttach(Activity activity) {
       // This is the first callback received; here we can set the text for
       // the fragment as defined by the tag specified during the fragment
       // transaction
       super.onAttach(activity);
       mText = getTag();
     }

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
         // This is called to define the layout for the fragment;
         // we just create a TextView and set its text to be the fragment tag
         TextView text = new TextView(getActivity());
         text.setText(mText);
         return text;
     }
 }
 

ActionBar官方教程(10)ActionBar的下拉列表模式的更多相关文章

  1. ActionBar官方教程(9)ActionBar的顶部tab模式(注意,已经被弃用)

    This interface is deprecated.Action bar navigation modes are deprecated and not supported by inline ...

  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官方教程(2)选主题让应用支或不支持ActionBar及支持ActionBar的应用如何隐藏和显示

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

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

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

  5. 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 ...

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

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

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

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

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

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

  9. ActionBar官方教程(7)自定义操作项的view,如何得到它及处理它的事件

    Adding an Action View An action view is a widget that appears in the action bar as a substitute for ...

随机推荐

  1. WCF编程系列(三)地址与绑定

    WCF编程系列(三)地址与绑定   地址     地址指定了接收消息的位置,WCF中地址以统一资源标识符(URI)的形式指定.URI由通讯协议和位置路径两部分组成,如示例一中的: http://loc ...

  2. ASP.NET 发送email

    首先添加命名空间 using System.Net.Mail; /// <summary> /// 发送邮件 /// </summary> /// <param name ...

  3. C# 高精度减法 支持小数(待优化)

    是现实思路 1,先小数点补位,8913758923475893274958738945793845-4893127498372459823745324532453245.284929384729837 ...

  4. unity访问php

    长连接,弱联网.不好意思,这俩不是一个意思. 反过来说,短连接,强联网,是不是有点别扭呢. 你可以不会php,甚至你可以不知道php是干什么的. 百度php安装环境,自行搭建好环境,顺便测试一下.(下 ...

  5. js个人笔记

    一.删除元素 <!DOCTYPE html> <html> <head> <title>删除元素</title> </head> ...

  6. [译]CSS content

    原文地址:http://css-tricks.com/css-content/ CSS中有一个属性content,只能和伪元素:before和:after一起使用,他们的写法像伪类选择器(前面有冒号) ...

  7. 回溯算法之n皇后问题

    今天在看深度优先算法的时候,联想到DFS本质不就是一个递归回溯算法问题,只不过它是应用在图论上的.OK,写下这篇博文也是为了回顾一下回溯算法设计吧. 学习回溯算法问题,最为经典的问题我想应该就是八皇后 ...

  8. hdu 4778 Gems Fight! 博弈+状态dp+搜索

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4102743.html 题目链接:hdu 4778 Gems Fight! 博弈+状态dp+搜 ...

  9. C#与.NET

    1 .NET Framework的核心是其运行库执行环境,即公共语言运行库(CLR)或.NET运行库,一般将CLR控制下运行的代码称为托管代码(managed code). 在CLR在执行编写好的代码 ...

  10. ASP.NET MVC Web API使用示例

    上篇博客讲解rest服务开发时,曾经提到过asp.net mvc中的rest api,由于篇幅原因,没有在上篇博客中进行讲解,这里专门拿出来进行讨论.还是一样引用上次的案例,用asp.net mvc提 ...