Android Two-level menu final version

Source: Internet
Author: User
Tags getcolor

Last Picture:

Finally have a end, before because their analysis is not in place, led to the previous level two menu a bit of a problem, so re-think the next, change to now this look, using the Magic Point 9 picture, achieve this effect.

Always, is to be clicked on the background of the change, the small arrow is actually clicked on the background of the item, and the following is the GridView, because by the relatively close, it seems to be linked together. Look at the code:

Cusmenu.java

Package Com.example.twolevelmenu;import Java.util.arraylist;import Java.util.list;import android.content.Context; Import Android.content.res.typedarray;import Android.graphics.bitmap;import Android.graphics.color;import Android.graphics.drawable.colordrawable;import Android.graphics.drawable.drawable;import Android.util.attributeset;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.View ; Import Android.view.viewgroup;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.baseadapter;import Android.widget.ImageView ; import Android.widget.imageview.scaletype;import Android.widget.linearlayout;import Android.widget.TextView; Import Com.example.twolevelmenu.menubean.menuchildbean;import Com.lidroid.xutils.bitmaputils;import Com.lidroid.xutils.bitmap.bitmapdisplayconfig;import Com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack; Import Com.lidroid.xutils.bitmap.callback.bitmaploadfrom;public class Cusmenu EXtends LinearLayout {/** * number of columns */private int colum = 3;/** * Load data subscript */private int index;/** * Record click id */private int ID;PRI vate bitmaputils bitmaputils = null;/** * Related data sources and view */private list<menubean> list;private List<MyGridView> g Ridviewlist = new arraylist<mygridview> ();p rivate list<linearlayout> linearlayoutlist = new ArrayList< Linearlayout> ();p ublic Cusmenu (context context) {super (context);} Public Cusmenu (context context, AttributeSet Attrs) {Super (context, attrs);//TODO Auto-generated constructor Stubtypedarray a = context.obtainstyledattributes (attrs,r.styleable.cusmenu); colum = A.getint (R.styleable.CusMenu_ Colum, 3); a.recycle ();//setbackgroundresource (r.drawable.order_list_04);} public void init (list<menubean> list) {if (List = = NULL | | list.size () = = 0) return;this.setorientation (linearlayout . VERTICAL); This.setbackgroundcolor (Getresources (). GetColor (Android. R.color.white)); if (bitmaputils = = null) {bitmaputils = new Bitmaputils (GetContext ()); bitMaputils.configdefaultloadingimage (R.drawable.ic_launcher); Bitmaputils.configdefaultloadfailedimage ( R.drawable.ic_launcher); Bitmaputils.configdefaultbitmapconfig (Bitmap.Config.RGB_565);} This.list = list;int row = 0;int size = this.list.size ();//Calculate the number of rows if (size% Colum = = size) {///and the number of columns is equal to a row = 1;} El Se if (size% Colum = = 0) {//equals 0 description exactly except for row = Size/colum;} else {//exceeds, +1 rows row = Size/colum + 1;} for (int i = 1; I <= row; i++) {//layout of each row, including Colum column layout linearlayout rowlinearlayout = new LinearLayout (GetContext ()); Linearlayout.layoutparams rowlinearlayoutparams = new Layoutparams (LAYOUTPARAMS.FILL_PARENT,LAYOUTPARAMS.WRAP_ CONTENT); rowlinearlayoutparams.setmargins (0, 0, 0); Rowlinearlayout.setlayoutparams (Rowlinearlayoutparams); Rowlinearlayout.setorientation (linearlayout.horizontal); Rowlinearlayout.setgravity (Gravity.CENTER_HORIZONTAL| gravity.center_vertical); Rowlinearlayout.setbackgroundcolor (Getresources (). GetColor (Android. R.color.white)); if (i = = 1) {//The first line should be left with more space rowlineaRlayout.setpadding (0, 30, 0, 0);} Else{rowlinearlayout.setpadding (0, 20, 0, 0);} Rowlinearlayout.setpadding (0, 20, 0, 0);//Hide a GridView under each line layout, Display submenu MyGridView Rowgridview = new MyGridView ( GetContext ()); Rowgridview.setnumcolumns (Colum); rowgridview.sethorizontalspacing (0); Rowgridview.setverticalspacing (2); rowgridview.setvisibility (View.gone); Rowgridview.setid (i+1000);//1000 is a random number, Prevent ID duplication//rowgridview.setpadding (0, 5, 0, 0); Rowgridview.setselector (new Colordrawable (color.transparent)); Rowgridview.setbackgroundcolor (Color.rgb (230, 230, 230)); Linearlayout.layoutparams rowgridviewparams = new Linearlayout.layoutparams (layoutparams.fill_parent, layoutparams.wrap_content); rowgridviewparams.setmargins (0, 0); Rowgridview.setlayoutparams ( Rowgridviewparams); Gridviewlist.add (Rowgridview); This.addview (rowlinearlayout); This.addview (RowGridView); for ( int j = 1; J <= Colum; J + +) {//layout of each column, including ImageView and textview//create Linearyoutlinearlayout columlinearlayout with ImageView and TextView = new LinearlayouT (GetContext ()); Columlinearlayout.setorientation (linearlayout.vertical); Linearlayout.layoutparams columlinearlayoutparams = new Layoutparams (0, layoutparams.wrap_content, 1); Columlinearlayoutparams.gravity = gravity.center_horizontal| Gravity.center_vertical;columlinearlayout.setlayoutparams (Columlinearlayoutparams); Columlinearlayout.setgravity (gravity.center_horizontal| gravity.center_vertical); Linearlayoutlist.add (Columlinearlayout), if (Index < size) {Columlinearlayout.settag ( R.id.index, index); Columlinearlayout.settag (Rowgridview); Columlinearlayout.setid (index); Columlinearlayout.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stubfinal int index = (Integer) v.gettag (r.id.index);//Gets the subscript for the data to be loaded mygridview GridView = (mygridvie W) v.gettag ();//Get gridviewfinal list<menubean.menuchildbean> childlist = CusMenu.this.list.get (index). childlist;//If the submenu is empty or the size of the submenu is 0,childindex returns -1if (childlist==null| | Childlist.size () = = 0) {if (Onchildgridviewitemclick = null) {Onchildgridviewitemclick.onitemclick (index,-1);} Gridview.setvisibility (view.gone); id = V.getid ();//record The clicked parent menu return;} ELSE{RESETLINEARLAYOUTBG (); V.setbackgroundresource (R.DRAWABLE.NEWMAIN_COLUM_BG);} Sub-menu Listener, callback Gridview.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (Adapterview <?> parent,view View, int position, long id) {if (Onchildgridviewitemclick! = null && childlist.get (position ) = null) {Onchildgridviewitemclick.onitemclick (index, position);}}); /submenu has not shown if (gridview.getvisibility () = = View.gone) {gridview.setvisibility (view.visible);//submenu for (MyGridView Mygridview:gridviewlist) {//Traverse gridviewif per line (Gridview.getid ()! = Mygridview.getid ()) {//If the GridView is not on this line, Hide Mygridview.setvisibility (View.gone);}} if (gridview.getadapter () = = null) {Gridview.setadapter (new Adapter (Childlist));} else {((Adapter) Gridview.getadapter ()). Changelist (Childlist);}} else {if (id = = V.getid ()) {///here to determine whether to click on the same parent menu, if yes, hide the submenu, or moreNew submenu gridview.setvisibility (View.gone); V.setbackgroundresource (R.drawable.newmain_colum_bg_nor);} else {((Adapter) Gridview.getadapter ()). Changelist (Childlist);}} id = V.getid ();//record The clicked parent menu}); ImageView Columimageview = new ImageView (GetContext ()); Columimageview.setscaletype ( SCALETYPE.FIT_XY); final int width = (int) (screeninfo.screenwidth * 0.185);//0.185 is the proportion of the screen to the picture Linearlayout.layoutparams Imgparams = new Layoutparams (width, width); imgparams.setmargins (0, 0, 0,) imgparams.gravity = Gravity.center_ horizontal| Gravity.center_vertical;columimageview.setlayoutparams (Imgparams); Menubean Menubean = CusMenu.this.list.get (index);//Picture load Order network > Local > Default if (Menubean.logo_url!=null && MenuBean.logo_url.length () > 0) {//If online picture Bitmaputils.display (Columimageview, menubean.logo_url,new Bitmaploadcallback<view> () {@Overridepublic void onloadcompleted (View container,string URI, Bitmap Bitmap, Bitmapdisplayconfig Config,bitmaploadfrom from) {//TODO auto-generated Method Stub ((ImageView) CONtainer). Setimagebitmap (bitmap);//cutterbitmap (bitmap, width, width));} @Overridepublic void onloadfailed (View container,string URI, drawable drawable) {//TODO auto-generated Method stub}}); else if (menubean.logo_id! = 0) {//If there is a local picture columimageview.setimageresource (menubean.logo_id);} else{//default picture Columimageview.setimageresource (R.DRAWABLE.NEWMAIN_ICON5);} TextView Columtextview = new TextView (GetContext ()); Columtextview.setgravity (gravity.center_horizontal| gravity.center_vertical); Columtextview.settext (This.list.get (index). Desc); Columtextview.settextcolor ( Getresources (). GetColor (Android. R.color.black));//Add to Column Layout Columlinearlayout.addview (columimageview); Columlinearlayout.addview (ColumTextView);} else {//Add to Row layout columlinearlayout.setvisibility (view.invisible);} Rowlinearlayout.addview (columlinearlayout); index++;}} Class Adapter extends Baseadapter {private list<menuchildbean> list;public Adapter (list<menuchildbean> childlist) {this.list = Childlist;checklist ();} public void Changelist (List<menuchildbean> list) {this.list = List;checklist (); notifydatasetchanged ();} private void Checklist () {int remainder = List.size ()%colum;if (remainder = = 0) return;for (int i = remainder; i < Colum; i + +) {list.add (null);}} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {if (Convertview = = null) {Convertview = Layo Utinflater.from (GetContext ()). Inflate (R.layout.cusmenu_gridview_item, null);} TextView TV = Viewholder.get (Convertview, R.ID.CUSMENU_GRIDVIEW_TV); Tv.settextcolor (Color.rgb (70, 70, 70)); Tv.setvisibility (view.visible); tv.settextsize (13); View line = Viewholder.get (Convertview, r.id.line); line.setvisibility (view.visible); if (List.get (position)! = null) { Tv.settext (List.get (position). desc);} else {tv.setvisibility (view.gone); line.setvisibility (View.gone);} if (position% Colum = = colum-1) line.setvisibility (View.gone); return convertview;} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn 0;} @Overridepublic Object GetItem (int position) {//TODO auto-generated method Stubreturn null;} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn list.size ();}} public void Hiddenchildmenu () {for (MyGridView mygridview:gridviewlist) {//Traversal gridviewmygridview.setvisibility per row ( View.gone);}} public void Resetlinearlayoutbg () {for (LinearLayout linerlayout:linearlayoutlist) { Linerlayout.setbackgroundresource (R.drawable.newmain_colum_bg_nor);}} Private Onchildgridviewitemclick onchildgridviewitemclick;public void Setonchildgridviewitemclick ( Onchildgridviewitemclick onchildgridviewitemclick) {this.onchildgridviewitemclick = OnChildGridViewItemClick;} public interface Onchildgridviewitemclick {void Onitemclick (int parentindex, int childindex);}}
menuactivity. java

Package Com.example.twolevelmenu;import Java.util.arraylist;import Java.util.list;import android.app.Activity; Import Android.os.bundle;import Android.widget.toast;import Com.example.twolevelmenu.r;public class MenuActivity Extends activity{private list<menubean> List = new arraylist<menubean> ();p rivate cusmenu cusmenu;@ overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Screeninfo.initscreent (this); Cusmenu=new Cusmenu (this);//cusmenu.setimagename ("A", ". png"); Setcontentview ( Cusmenu); Getdatatest ();} private void Getdatatest () {for (int i = 0; i < 5; i++) {Menubean bean = new Menubean (); Bean.desc = "plug-in" + I;bean.logo_ id = R.drawable.newmain_icon5;bean.logo_url = ""; list<menubean.menuchildbean> childlist = new arraylist<menubean.menuchildbean> (); for (int j=0;j<=i;j+ +) {Menubean.menuchildbean Childbean = bean.new Menuchildbean (); childbean.desc= "submenu" +j;childlist.add (ChildBean);} Bean.childList=childlist;list.add (bean);} Cusmenu.init (list); Cusmenu.setonchildgridviewitemclick (new Cusmenu.onchildgridviewitemclick () {@Overridepublic void Onitemclick (int parentindex, int childindex) {//TODO auto-generated method Stubtoast.maketext ( Getapplicationcontext (), parentindex+ "--" +childindex, Toast.length_short). Show ();}});}}

See demo, click here to download

Android Two-level menu final version

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.