Kaizige takes you to consolidate the application layer. This is a pitfall encountered when you use ActionProvider to implement Sub-menus.
Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992
I recently reviewed the Android infrastructure and encountered a pitfall when I saw ActionProvider. I shared it with you to avoid it.
First, we will briefly introduce ActionProvider.
ShareActionProvider, have you used it? The sharing function provided by the system is used. In fact, the notify ActionProvider is a subclass of the ActionProvider. It can be displayed as a MenuItem on the ActionBar, but we can customize event behavior, therefore, we can inherit the ActionProvider to implement the sub-menu effect, as shown below:
On the basic usage, I do not explain, you can refer to the article http://blog.csdn.net/guolin_blog/article/details/25466665 Guo Shen
Let's focus on the pitfalls I encountered.
Problem description: With appcompat-support-v7: 22.0.0, support-v4: 22.0.0 compatible package, the problem that ActionProvider cannot be displayed.
Cause: the import and xml Namespaces are inconsistent due to compatibility packages.
Correct syntax:
Note that the ActionProvider must use the v4 compatibility package.
import android.content.Context;import android.support.v4.view.ActionProvider;import android.view.MenuItem;import android.view.SubMenu;import android.view.View;/** * Created by zhaokaiqiang on 15/3/18. */public class MyActionProvider extends ActionProvider {private Context context;public MyActionProvider(Context context) {super(context);this.context = context;}@Overridepublic View onCreateActionView() {return null;}@Overridepublic void onPrepareSubMenu(SubMenu subMenu) {subMenu.clear();subMenu.add("sub item 1").setIcon(R.mipmap.ic_launcher).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {@Overridepublic boolean onMenuItemClick(MenuItem item) {return true;}});subMenu.add("sub item 2").setIcon(R.mipmap.ic_launcher).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {@Overridepublic boolean onMenuItemClick(MenuItem item) {return false;}});}@Overridepublic boolean hasSubMenu() {return true;}}
Note that the app namespace should be used in the menu xml, as shown in the following figure. If android: namespace is used, the program will not report an error, but the sub-menu will not be displayed, so it will be depressed.
<Menu xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto"> <item android: id = "@ + id/action_search" android: icon = "@ android: drawable/ic_menu_search "android: actionViewClass =" android. widget. searchView "app: showAsAction =" ifRoom | collapseActionView "android: title =" Search "/> <item android: id =" @ + id/action_add "android: icon = "@ android: drawable/ic_menu_add" android: title = "add" app: showAsAction = "ifRoom" app: actionProviderClass = "com. socks. uidemo. myActionProvider "/> </menu>
If we use the app: namespace, but it is not in the v4 package, an error will be reported, as shown below:
The solution to this problem is as follows in stackoverflow, but it has been tried and does not work. It is not the same as the compatibility package problem we encountered.
Http://stackoverflow.com/questions/19439106/cant-display-sub-menu-for-custom-actionprovider