ActionBar官方教程(10)ActionBar的下拉列表模式
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开启下拉列表模式的基本步骤
- Create a
SpinnerAdapterthat provides the list of selectable items for the drop-down and the layout to use when drawing each item in the list.准备下拉list的adapter
- Implement
ActionBar.OnNavigationListenerto define the behavior that occurs when the user selects an item from the list.实现ActionBar.OnNavigationListener,处理下拉列表的item事件
- During your activity's
onCreate()method, enable the action bar's drop-down list by callingsetNavigationMode(NAVIGATION_MODE_LIST).设备ActionBar.setNavigationMode(NAVIGATION_MODE_LIST);设置ActionBar为下拉模式
- Set the callback for the drop-down list with
setListNavigationCallbacks(). For example:注册SpinnerAdapter 和 ActionBar.OnNavigationListener
actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);
This method takes your
SpinnerAdapterandActionBar.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);
ThecreateFromResource()method takes three parameters: the applicationContext, the resource ID for the string array, and the layout to use for each list item.
A 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的下拉列表模式的更多相关文章
- ActionBar官方教程(9)ActionBar的顶部tab模式(注意,已经被弃用)
This interface is deprecated.Action bar navigation modes are deprecated and not supported by inline ...
- ActionBar官方教程(5)ActionBar的分裂模式(底部tab样式),隐藏标题,隐藏图标
Using split action bar Split action bar provides a separate bar at the bottom of the screen to displ ...
- ActionBar官方教程(2)选主题让应用支或不支持ActionBar及支持ActionBar的应用如何隐藏和显示
Adding the Action Bar As mentioned above, this guide focuses on how to use the ActionBar APIs in the ...
- ActionBar官方教程(11)自定义ActionBar的样式(含重要的样式属性表及练习示例)
Styling the Action Bar If you want to implement a visual design that represents your app's brand, th ...
- 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 ...
- ActionBar官方教程(3)更改标题处的图片
Using a logo instead of an icon By default, the system uses your application icon in the action bar, ...
- ActionBar官方教程(1)简介及各区域介绍
Action Bar The action bar is a window feature that identifies the user location, and provides user a ...
- ActionBar官方教程(8)ShareActionProvider与自定义操作项提供器
Adding an Action Provider Similar to an action view, an action provider replaces an action button wi ...
- ActionBar官方教程(7)自定义操作项的view,如何得到它及处理它的事件
Adding an Action View An action view is a widget that appears in the action bar as a substitute for ...
随机推荐
- 使用 vmstat 监测系统性能
在linux/unix下,vmstat是常用的系统性能监测工具.常用用法如下 vmstat 1 10 表示以1秒为间隔,做相关参数的采样,一共10次.输出范例如下: procs ----------- ...
- JDBC向oracle插入数据
public static void main(String[] args) throws SQLException { 2 3 4 String driver="oracle.jdbc.d ...
- 通过sql做数据透视表,数据库表行列转换(pivot和Unpivot用法)(一)
在mssql中大家都知道可以使用pivot来统计数据,实现像excel的透视表功能 一.MSsqlserver中我们通常的用法 1.Sqlserver数据库测试 ---创建测试表 Create tab ...
- OC2_数组操作
// // main.m // OC2_数组操作 // // Created by zhangxueming on 15/6/11. // Copyright (c) 2015年 zhangxuemi ...
- C++异常处理(Exception Handling)
在C++中引入了三种操作符来处理程序的出错情况,分别是:try , throw , catch 1.基本的用法如下: try{ //code to be tried throw exceptio ...
- Entity Framework 学习笔记(1)
开始从头系统地学习Entity Framework,当前的稳定版本为6.1.3,Nuget主页 http://www.nuget.org/packages/EntityFramework/ 微软喜欢把 ...
- Java标准输入输出流的重定向及恢复
在Java中输入输出数据一般(图形化界面例外)要用到标准输入输出流System.in和System.out,System.in,System.out默认指向控制台,但有时程序从文件中输入数据并将结果输 ...
- c++实现类似Common Lisp的多参数加法和比较
在CL里我们可以这样: $ sbcl * (+ 1 2 3) 6 * (< 1 2 3) T * (< 2 3 1) NIL * 从简单的方面看, CL的+和<就是一个接收多参数的函 ...
- 使用AutoMapper
一.AutoMapper初探: [参考Using AutoMapper: Getting Started] 1.新建空的ASP.NET MVC项目. 2.在Models文件夹添加类: public c ...
- SharePoint Client Add Folder,file to Library
public void UploadDocument(string siteURL, string documentListName, string documentListURL, string d ...