"Kyle, take you to the application layer." A pit encountered when implementing a submenu using Actionprovider

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992

Recently in the review of Android Foundation, in the sight of Actionprovider encounter a pit, share to everyone, avoid into the pit.

First, the next actionprovider is introduced briefly.

Shareactionprovider, you should have used it? is to use the system comes with the sharing function, in fact, Shareactionprovider is a subclass of Actionprovider, can be displayed on the Actionbar as a MenuItem, but we can customize the event behavior, So we can implement sub-menu effects by inheriting actionprovider, like this



Regarding the basic usage, I no longer explain, you can refer to Guo Shen's article http://blog.csdn.net/guolin_blog/article/details/25466665

Let's focus on the pit I met.

Problem Description: with the appcompat-support-v7:22.0.0,support-v4:22.0.0 Compatibility Pack, there are problems that Actionprovider cannot display.

Problem Reason: Import and XML namespace inconsistencies caused by using Compatibility pack

Correct wording:

Special note actionprovider Be sure to use the V4 Compatibility Pack

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;}}

Also note that in the menu XML, to use the app's namespace, if you want to use the Android: namespace, the program will not error, but the submenu is not displayed, depressed death

<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:d rawable/ic _menu_search "        android:actionviewclass=" Android.widget.SearchView "        app:showasaction=" ifroom| Collapseactionview "        android:title=" search "/>    <item        android:id=" @+id/action_add "        android: icon= "@android:d rawable/ic_menu_add"        android:title= "Add"        app:showasaction= "Ifroom"        app: actionproviderclass= "Com.socks.uidemo.MyActionProvider"        /></menu>

If we use the app: namespace, but it's not in the V4 package, it will be an error, as follows



The problem in the StackOverflow solution is as follows, but tried, does not work, and we encounter the Compatibility pack problem does not belong to a

Http://stackoverflow.com/questions/19439106/cant-display-sub-menu-for-custom-actionprovider


"Kyle, take you to the application layer." A pit encountered when implementing a submenu using Actionprovider

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.