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. 再议Unity 3D

    一年前,偶发冲动,翻译了<[译] Unity3D游戏和facebook绑定(1:简介)>系列文章. 现在看有2个明显的好处, 一:给这个不温不火的博客带了top 3的人气: 二:我个人由此 ...

  2. [Guava官方文档翻译] 4. 使用Guava Ordering排序 (Ordering Explained)

    本文地址:http://www.cnblogs.com/hamhog/p/3537233.html 示例 assertTrue(byLengthOrdering.reverse().isOrdered ...

  3. CICS定时

    F+14个0-----给CICS传参数FM CICSServer svr;        svr.GetRetCommData(&pMsgStr, &chMsgType); CICSS ...

  4. 关于php的认识和介绍

    php的介绍: 什么是php? <1>:php是一个编程语言 <2>:php是处理php编程语言的一个软件.php语言必须运行在php软件上. 为什么要学习php? php可以 ...

  5. 【Linux工具】svn命令行使用实例

    引言 网上有这么多介绍 svn 使用的文章,为什么还要写?因为它们深入不浅出,平铺不分类,理论不实际,看完也记不住. 本文先介绍基本用法,后进行实例演练.不求大而全,只求熟练常用,自行用 svn he ...

  6. C++中const修饰基本数据类型、指针、引用、对象

    const修饰基本数据类型 #include <iostream> using namespace std; void main(){ const int a = 1; const cha ...

  7. 修改Sublime Text 3 的侧边栏字体大小

    此文转载哈,忘了作者地址,还请见谅!! 首先需要确保安装了Package ControlPackage Control作为ST必备插件,这里就不多介绍了,没装的话,google一下,各种介绍以及安装教 ...

  8. switch...case 和 if...else

    switch...case与if...else的根本区别在于: switch...case会生成一个跳转表来指示实际的case分支的地址,而这个跳转表的索引号与switch变量的值是相等的,switc ...

  9. 高性能IO设计的Reactor和Proactor模式(转)

    在高性能的I/O设计中,有两个比较著名的模式Reactor和Proactor模式,其中Reactor模式用于同步I/O,而Proactor运用于异步I/O操作. 在比较这两个模式之前,我们首先的搞明白 ...

  10. 根据版本的不同整理所有的绿色SQL Server

    在这篇论坛文章中,读者可以了解到如何根据不同的SQL Server版本,整理出所有版本的绿色SQL Server的具体方法,详细内容请参考下文: 1. Sqlservr.exe 运行参数 Sql Se ...