Android (10) left menu bar

Source: Internet
Author: User

One. Left menu bar:

Download a copy of the source code, see this effect, in fact, is to show hidden, will certainly be able to use, a little change, add a bit of comments, so record (to leave the code of the mailbox).

:



Main interface:

public class Mainactivity extends Activity implementsmenuitemcallback {private Menuview rbmview;private Button button1;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main) button1 = (Button) Findviewbyid (r.id.button1); Rbmview = (Menuview) Findviewbyid ( R.ID.RIBBONMENUVIEW1); Rbmview.setmenuclickcallback (this); Rbmview.setmenuitems (R.menu.ribbon_menu); Button1.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View arg0) {Toast.maketext ( Mainactivity.this, "I'm just a useless button!"        ", Toast.length_short). Show ();}});    Icon can be clicked Getactionbar (). Setdisplayhomeasupenabled (true);} When you click menu, call @overridepublic boolean onoptionsitemselected (MenuItem item) {int id = item.getitemid (); if (id = = Android).    R.id.home) {rbmview.togglemenu (); return true;} else {return super.onoptionsitemselected (item);}} @Overridepublic void Menuitemclick (int itemId) {Toast.maketext (This, "click", Toast.length_short). ShoW ();}} 
Menuview:

public class Menuview extends LinearLayout {private ListView rbmlistview;private View rbmoutsideview;private Menuitemcallback callback;private static arraylist<ribbonmenuitem> menuitems;public MenuView (context context) { Super (context); load ();}    Public Menuview (context context, AttributeSet Attrs) {Super (context, attrs); load ();} Add vegetable private void load () {if (Isineditmode ()) return;inflatelayout (); Initui ();} Initialize the layout private void Inflatelayout () {try {Layoutinflater.from (GetContext ()). Inflate (R.layout.activity_menu, this, true);} catch (Exception e) {}}//Initialize uiprivate void Initui () {Rbmlistview = (ListView) Findviewbyid (R.id.rbm_listview); Rbmoutsi Deview = (View) Findviewbyid (R.id.rbm_outside_view);//Click Shadow to Hide Rbmoutsideview.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {hidemenu ();}}); /Item Click Hide Rbmlistview.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick ( Adapterview<?> parent, View view,int position, long ID) {Callback if (callback! = NULL) callback. Menuitemclick (Menuitems.get (position). ID); Hidemenu ();}}); public void Setmenuclickcallback (Menuitemcallback callback) {this.callback = callback;} public void Setmenuitems (int menu) {parsexml (menu); if (MenuItems! = null && menuitems.size () > 0) {rbmlistview. Setadapter (New Adapter ());}} Load picture resource public void Setbackgroundresource (int resource) {Rbmlistview.setbackgroundresource (Resource);} Show menu public void ShowMenu () {rbmoutsideview.setvisibility (view.visible); Rbmoutsideview.startanimation ( Animationutils.loadanimation (GetContext (), r.anim.menu_in_from_left)); rbmlistview.setvisibility (View.VISIBLE); Rbmlistview.startanimation (Animationutils.loadanimation (GetContext (), r.anim.menu_in_from_left)); Hidden menu public void Hidemenu () {rbmoutsideview.setvisibility (view.gone); Rbmoutsideview.startanimation ( Animationutils.loadanimation (GetContext (), r.anim.menu_out_to_left)); rbmlistview.setvisibility (View.GONE); Rbmlistview.startanimation (Animationutils.loadanimatIon (GetContext (), r.anim.menu_out_to_left));} Hide and show alternating public void Togglemenu () {if (rbmoutsideview.getvisibility () = = View.gone) {ShowMenu ();} else {hidemenu ();}} Read XML file private void Parsexml (int menu) {MenuItems = new arraylist<menuview.ribbonmenuitem> (); try { Xmlresourceparser xpp = Getresources (). GETXML (menu); Xpp.next (); int eventtype = Xpp.geteventtype (); while (eventtype! = Xmlpullparser.end_document) {if (EventType = = Xmlpullparser.start_tag) {String elemname = Xpp.getname (); if ( Elemname.equals ("item")) {//The first parameter is a namespace, the second parameter is the property name string text = Xpp.getattributevalue ("http://schemas.android.com/ Apk/res/android "," title "); String iconID = Xpp.getattributevalue ("http://schemas.android.com/apk/res/android", "icon"); String resId = Xpp.getattributevalue ("Http://schemas.android.com/apk/res/android", "id"); System.out.println (text + "" + iconID + "" + resId); Ribbonmenuitem item = new Ribbonmenuitem ();//ID value is @id so remove @item.id = integer.valueof (Resid.replace ("@", "")); System.out.println (item.id); item.text = resourceidtostring (text); Item.icon = integer.valueof (Iconid.replace ("@", "")); Menuitems.add ( Item);}} EventType = Xpp.next ();}} catch (Exception e) {e.printstacktrace ();}} Determines whether the string contains the @ character, private string resourceidtostring (string text) {if (!text.contains ("@")) {return text;} else {String id =    Text.replace ("@", ""); return Getresources (). GetString (integer.valueof (ID));}} Determines whether the current menu hides public boolean ismenuvisible () {return rbmoutsideview.getvisibility () = = view.visible;} @Overrideprotected void Onrestoreinstancestate (parcelable state) {savedstate SS = (savedstate) state; Super.onrestoreinstancestate (Ss.getsuperstate ()); if (Ss.bshowmenu) ShowMenu (); Elsehidemenu ();} Save UI State @overrideprotected parcelable Onsaveinstancestate () {parcelable superstate = super.onsaveinstancestate (); savedstate ss = new savedstate (superstate); ss.bshowmenu = Ismenuvisible (); return SS;} Save interface state Static class Savedstate extends Basesavedstate {boolean bshowmenu; Savedstate (parcelable superstate) {Super(superstate);} Private savedstate (Parcel in) {super (in); Bshowmenu = (in.readint () = = 1);} @Overridepublic void Writetoparcel (Parcel out, int. flags) {Super.writetoparcel (out, flags); Out.writeint (Bshowmenu? 1:0 );} public static final parcelable.creator<savedstate> Creator = new parcelable.creator<savedstate> () {public Savedstate Createfromparcel (Parcel in) {return new savedstate (in);} Public savedstate[] NewArray (int size) {return new savedstate[size];};} Class Ribbonmenuitem {int id; String text;int icon;} Private class Adapter extends Baseadapter {private layoutinflater inflater;public Adapter () {inflater = layoutinflater.fr Om (GetContext ());} @Overridepublic int GetCount () {return menuitems.size ();} @Overridepublic Object getItem (int position) {return null;} @Overridepublic long Getitemid (int position) {return 0;}  @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {final Viewholder holder;if (Convertview = = null | | Convertview instanceof TextView) {ConvErtview = inflater.inflate (R.layout.activity_menu_item, null); holder = new Viewholder (); holder.image = (ImageView) Convertview.findviewbyid (r.id.rbm_item_icon); holder.text = (TextView) Convertview.findviewbyid (R.id.rbm_item_text ); Convertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();} Holder.image.setImageResource (Menuitems.get (position). Icon) Holder.text.setText (Menuitems.get (position). text); return Convertview;} Class Viewholder {TextView text;imageview image;}}}
Callback method:
Public interface Menuitemcallback {//Click on item when public void Menuitemclick (int itemId) is called;}

Main layout:

<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent ">    <button        android:id= "@+id/button1"        android:layout_width= "fill_parent"        android:layout_height= "Wrap_content"        android:text= "Click the arrow to the left of the top left icon to see the effect!" "        />        <com.example.testtexiao7.menuview        android:id=" @+id/ribbonmenuview1 "        android:layout _width= "Match_parent"        android:layout_height= "Match_parent"/></framelayout>

Menu layout:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "Horizontal" >    <listview         android:id= "@+id/rbm_listview"        android:layout_width= "@ Dimen/menu_width "        android:layout_height=" match_parent "         android:background=" @color/menu_background "           android:visibility= "Gone"        android:cachecolorhint= "#00000000" >    </ListView>        <!-- Shadow--    <view         android:id= "@+id/rbm_outside_view"        android:layout_width= "0DP"        android: layout_height= "Match_parent"           android:background= "#e0000000"             android:layout_weight= "1"        android: Clickable= "true"        android:visibility= "Gone"        /></linearlayout>

Item

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "Horizontal" >    <listview         android:id= "@+id/rbm_listview"        android:layout_width= "@ Dimen/menu_width "        android:layout_height=" match_parent "         android:background=" @color/menu_background "           android:visibility= "Gone"        android:cachecolorhint= "#00000000" >    </ListView>        <!-- Shadow--    <view         android:id= "@+id/rbm_outside_view"        android:layout_width= "0DP"        android: layout_height= "Match_parent"           android:background= "#e0000000"             android:layout_weight= "1"        android: Clickable= "true"        android:visibility= "Gone"        /></linearlayout>

This is the main part of the code, the other files are not posted first.

Android (10) left menu bar

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.