ActionBar官方教程(8)ShareActionProvider与自定义操作项提供器
Adding an Action Provider
Similar to an action view, an action provider replaces an action button with a customized layout. However, unlike an action view, an action provider takes control of all the action's behaviors and an action provider can display a submenu when pressed.

Figure 6. An action bar withShareActionProvider expanded to show share targets.
To declare an action provider, supply the actionViewClass attribute in the menu <item> tag with a fully-qualified class name for an ActionProvider.
You can build your own action provider by extending the ActionProvider class, but Android provides some pre-built action providers such as ShareActionProvider, which facilitates a "share" action by showing a list of possible apps for sharing directly in the action bar (as shown in figure 6).
Because each ActionProvider class defines its own action behaviors, you don't need to listen for the action in the onOptionsItemSelected() method. If necessary though, you can still listen for the click event in the onOptionsItemSelected() method in case you need to simultaneously perform another action. But be sure to return false so that the the action provider still receives the onPerformDefaultAction() callback to perform its intended action.
However, if the action provider provides a submenu of actions, then your activity does not receive a call to onOptionsItemSelected() when the user opens the list or selects one of the submenu items.
Using the ShareActionProvider
To add a "share" action with ShareActionProvider, define the actionProviderClass for an <item> tag with the ShareActionProvider class. For example:
使用ShareActionProvider第1步,在menu.xml中声明某个item的属性为 android:actionProviderClass="xxx.ShareActionProvider"
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_share"
android:title="@string/share"
yourapp:showAsAction="ifRoom"
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"
/>
...
</menu>
Now the action provider takes control of the action item and handles both its appearance and behavior. But you must still provide a title for the item to be used when it appears in the action overflow.
The only thing left to do is define the Intent you want to use for sharing. To do so, edit your onCreateOptionsMenu() method to call MenuItemCompat.getActionProvider() and pass it the MenuItem holding the action provider. Then call setShareIntent() on the returned ShareActionProvider and pass it an ACTION_SEND intent with the appropriate content attached.
You should call setShareIntent() once during onCreateOptionsMenu() to initialize the share action, but because the user context might change, you must update the intent any time the shareable content changes by again calling setShareIntent().
使用ShareActionProvider第2步,在代码中找到在menu中声明支持ActionPorivder的操作项,在代码中设置共享类型.
For example:
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
// Set up ShareActionProvider's default share intent
MenuItem shareItem = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider)
MenuItemCompat.getActionProvider(shareItem);
mShareActionProvider.setShareIntent(getDefaultIntent());
return super.onCreateOptionsMenu(menu);
}
/** Defines a default (dummy) share intent to initialize the action provider.
* However, as soon as the actual content to be used in the intent
* is known or changes, you must update the share intent by again calling
* mShareActionProvider.setShareIntent()
*/
private Intent getDefaultIntent() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
return intent;
}
The ShareActionProvider now handles all user interaction with the item and you do not need to handle click events from the onOptionsItemSelected() callback method.
By default, the ShareActionProvider retains a ranking for each share target based on how often the user selects each one. The share targets used more frequently appear at the top of the drop-down list and the target used most often appears directly in the action bar as the default share target. By default, the ranking information is saved in a private file with a name specified by DEFAULT_SHARE_HISTORY_FILE_NAME. If you use the ShareActionProvider or an extension of it for only one type of action, then you should continue to use this default history file and there's nothing you need to do. However, if you use ShareActionProvider or an extension of it for multiple actions with semantically different meanings, then each ShareActionProvider should specify its own history file in order to maintain its own history. To specify a different history file for the ShareActionProvider, call setShareHistoryFileName() and provide an XML file name (for example,"custom_share_history.xml").
Note: Although the ShareActionProvider ranks share targets based on frequency of use, the behavior is extensible and extensions of ShareActionProvider can perform different behaviors and ranking based on the history file (if appropriate).
Creating a custom action provider
Creating your own action provider allows you to re-use and manage dynamic action item behaviors in a self-contained module, rather than handle action item transformations and behaviors in your fragment or activity code. As shown in the previous section, Android already provides an implementation of ActionProvider for share actions: the ShareActionProvider.
To create your own action provider for a different action, simply extend the ActionProvider class and implement its callback methods as appropriate. Most importantly, you should implement the following:
自定义ActionProvider要继承ActionProvider类,相关接口如下,定义一个接口后,再像前面那样在menu,xml中指定某个item为这个新定义的ActionProvider子类就可.
ActionProvider()This constructor passes you the applicationContext, which you should save in a member field to use in the other callback methods.onCreateActionView(MenuItem)This is where you define the action view for the item. Use theContextacquired from the constructor to instantiate aLayoutInflaterand inflate your action view layout from an XML resource, then hook up event listeners. For example:-
public View onCreateActionView(MenuItem forItem) { // Inflate the action view to be shown on the action bar. LayoutInflater layoutInflater = LayoutInflater.from(mContext); View view = layoutInflater.inflate(R.layout.action_provider, null); ImageButton button = (ImageButton) view.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Do something... } }); return view; } onPerformDefaultAction()The system calls this when the menu item is selected from the action overflow and the action provider should perform a default action for the menu item.- However, if your action provider provides a submenu, through the
onPrepareSubMenu()callback, then the submenu appears even when the action provider is placed in the action overflow. Thus,onPerformDefaultAction()is never called when there is a submenu.
Note: An activity or a fragment that implements onOptionsItemSelected() can override the action provider's default behavior (unless it uses a submenu) by handling the item-selected event (and returningtrue), in which case, the system does not call onPerformDefaultAction().
For an example extension of ActionProvider, see ActionBarSettingsActionProviderActivity.
ActionBar官方教程(8)ShareActionProvider与自定义操作项提供器的更多相关文章
- ActionBar官方教程(7)自定义操作项的view,如何得到它及处理它的事件
Adding an Action View An action view is a widget that appears in the action bar as a substitute for ...
- ActionBar官方教程(11)自定义ActionBar的样式(含重要的样式属性表及练习示例)
Styling the Action Bar If you want to implement a visual design that represents your app's brand, th ...
- ActionBar官方教程(4)给ActionBar添加操作项及它们的事件处理
Adding Action Items The action bar provides users access to the most important action items relating ...
- ActionBar官方教程(10)ActionBar的下拉列表模式
Adding Drop-down Navigation As another mode of navigation (or filtering) for your activity, the acti ...
- ActionBar官方教程(9)ActionBar的顶部tab模式(注意,已经被弃用)
This interface is deprecated.Action bar navigation modes are deprecated and not supported by inline ...
- 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官方教程(5)ActionBar的分裂模式(底部tab样式),隐藏标题,隐藏图标
Using split action bar Split action bar provides a separate bar at the bottom of the screen to displ ...
- ActionBar官方教程(3)更改标题处的图片
Using a logo instead of an icon By default, the system uses your application icon in the action bar, ...
- ActionBar官方教程(2)选主题让应用支或不支持ActionBar及支持ActionBar的应用如何隐藏和显示
Adding the Action Bar As mentioned above, this guide focuses on how to use the ActionBar APIs in the ...
随机推荐
- ios数据库FMDB
一.下载fmdb类库 二.添加libsqulite3.0.dylib 三.添加头文件#import "FMDB.h" 四.打开数据库 a.设置路径NSString *path = ...
- ajax和jsonp的封装
一直在用jQuery的ajax,跨域也是一直用的jQuery的jsonp,jQuery确实很方便,$.ajax({...})就可以搞定. 为了更好的理解ajax和jsonp,又重新看了下书,看了一些博 ...
- 一篇关于学C++的感想(拿来与大家分享)
学计算机的如果你有耐心看下去,我敢保证这绝对是一种收获 期待爱 大师提醒: 计算机专业不是学编程,而是懂得计算机的工作原理,以及和计算机相关的学科技术.一个高手不必懂得编程,coder是最底层的人物, ...
- python 自动化之路 day 01.1 数据类型
一.变量二.数据类型2.1 什么是数据类型及数据类型分类2.2 标准数据类型:2.2.1 数字2.2.1.1 整型:2.2.1.2 长整型long:2.2.1.3 布尔bool:2.2.1.4 浮点数 ...
- Linux之最最最最基础(包括在虚拟机中安装linux系统)
这里是以CentOS 6.5 64bit为例(学习用这个,Kali神马的有兴趣自己研究(这个系统是玩渗透用的)) 一 ---->配置Vmware Workstation 自定义--选择 ...
- head 头标签(转发)
HTML head 头标签 paddingme | 04 Oct 2014 HTML head 头部分的标签.元素有很多,涉及到浏览器对网页的渲染,SEO 等等,而各个浏览器内核以及各个国内浏览器厂商 ...
- SQL Server2008数据库导入导出兼容性处理
使用场景:SQL Server 的高版本数据库恢复到低版本则可能会有兼容性问题,为了解决此类问题进行数据库脚本模式处理,数据库结构,及数据存储更换版本等. 1. 选择要导出的数据库,右键任务,生成脚 ...
- socket 基础学习
这个示例程序是同步套接字程序,功能很简单,只是客户端发给服务器一条信息,服务器向客户端返回一条信息:这里只是一个简单的示例,是一个最基本的socket编程流程,在接下来的文章中,会依次记录套接字的同步 ...
- php 时间函数参考
time()在PHP中是得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒,很奇怪吧 不过这样方便计算, 要找出前一天的时间就是 time()-60*60*24; 要找出前一年的时间就 ...
- PHP初学留神(三)
星期一进行面试结束后,意味着我的考研日子也结束了,以及我的2013.在好好总结之后还不能停止学习,心想着要把算法继续学下去,还有Linux.不过呢,始终都要记住尼采老师的这句当头棒喝:“不加选择的知识 ...