IOS client menu features imitation Baidu glutinous rice/American Group II menu _ios

Source: Internet
Author: User
Tags getcolor static class

I just recently in the development of a mall project, the implementation of a simple control, and we share it.

The effect of the control is similar to the Baidu glutinous rice or the United States Group level Two menu, I developed iOS client menu function, a direct reference to git a project, the corresponding UI effect:

In fact, the effect looks good. After the development of iOS, you are ready to develop Android and find that the case on the web is very small, or not the desired effect. I would like to refer to someone else's project code, but also for the open source project to make some contribution, ready to develop their own Android menu project;

Toss about three hours, finally finished, the effect is as follows:

From the picture is not difficult to see, this is a multi-level menu, the controller fills the data source, so when implemented, as much as possible to encapsulate the use of the user is best able to two or three lines of code.

The concrete realization thought:

1, Menuview, the implementation of the first level of the package of menus

①, view initialization and data source definition;

②, draw a level menu;

③, Control submenu Popupwindow pop-up box

The code is specific as follows:

Package Com.spring.sky.menuproject.view; 
Import Android.content.Context; 
Import Android.util.AttributeSet; 
Import android.view.Gravity; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.LinearLayout; 
Import Android.widget.TextView; 
Import Com.spring.sky.menuproject.AppInfoUtils; 
Import COM.SPRING.SKY.MENUPROJECT.R; 
Import java.util.List; 
/** * Created by Springsky on 16/10/24. * * public class Menuview extends LinearLayout implements View.onclicklistener, Menupopupwindow.onmenulistener {private 
String[] hinttexts; 
Public list[] DataSource; 
Public textview[] Textviews; 
private int textcolor = r.color.gray_80; 
private int textcolorselected = R.color.orange; 
private int textsize; 
private int lineheight; 
Private Menupopupwindow Menupopupwindow; 
Private Onmenulistener Onmenulistener; 
View Lineview; 
TextView Lasttv; 
Private indexpath[] indexpaths; 
Public Menuview {Super (context); 
Init (context); } Public Menuview (Context, AttributeSet Attrs) {Super (context, attrs); 
Init (context); 
Public Menuview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); 
Init (context); 
} public void Sethinttexts (string[] hinttexts) {this.hinttexts = hinttexts; 
} public void Setdatasource (list[] dataSource) {this.datasource = DataSource; 
Reloaddata (); /*** * Sets the currently selected data * @param indexpath */public void Setindexpath (Indexpath indexpath) {Setindexpath Indexpath 
SE); /*** * Sets the current selection * @param indexpath * @param actionmenu notify listener/public void Setindexpath (Indexpath Indexpath, b 
Oolean actionmenu) {Indexpaths[indexpath.column] = Indexpath; 
if (actionmenu) {TextView Lasttv = Textviews[indexpath.column]; 
list<menumodel> list = Datasource[indexpath.column]; 
if (list = = NULL | | indexpath.row >= list.size ()) {return; 
} Menumodel left = List.get (Indexpath.row); 
Menumodel Menumodel = null; if (Indexpath.item < 0) {MeNumodel = left; 
else {Menumodel right = Left.chindMenu.get (Indexpath.item); 
Menumodel = right; 
} lasttv.settext (Menumodel.value); 
if (Onmenulistener!= null) {Onmenulistener.onmenu (Indexpath, Menumodel); 
}} public list[] Getdatasource () {return dataSource; /*** * Initialization * @param context/private void init (context context) {Menupopupwindow = new Menupopupwindow (context) 
; 
Menupopupwindow.setonmenulistener (this); 
Appinfoutils.getviewheight (this); 
Textsize = APPINFOUTILS.SPTOPX (6); 
Lineheight = APPINFOUTILS.DIPTOPX (1); 
/*** * Draw a level menu category * * private void Reloaddata () {removeallviews (); 
if (DataSource = null | | Datasource.length < 1) {return; 
int count = Datasource.length; 
int height = getmeasuredheight ()-lineheight; 
SetOrientation (linearlayout.vertical); 
LinearLayout Menubaseview = new LinearLayout (GetContext ()); 
Menubaseview.setlayoutparams (New Layoutparams (layoutparams.match_parent, height)); 
Menubaseview.setweightsum (count); MenubAseview.setgravity (Gravity.center); 
Menubaseview.setorientation (linearlayout.horizontal); 
Indexpaths = new Indexpath[count]; 
Textviews = new Textview[count]; 
for (int i = 0; i < count; i++) {indexpaths[i] = new Indexpath (i, 0,-1); 
LinearLayout Tempbaseview = new LinearLayout (GetContext ()); 
Tempbaseview.setlayoutparams (New Layoutparams (layoutparams.match_parent, height, 1)); 
Tempbaseview.setgravity (Gravity.center); 
TextView TV = new TextView (GetContext ()); 
Tv.settextcolor (Getresources (). GetColor (TextColor)); 
Tv.settextsize (TEXTSIZE); 
Linearlayout.layoutparams params = new Layoutparams (layoutparams.wrap_content, layoutparams.match_parent); 
Tv.setgravity (Gravity.center); 
Tv.setlayoutparams (params); 
Tv.setmaxlines (1); 
Tv.setcompounddrawableswithintrinsicbounds (0, 0, r.mipmap.triangle_down, 0); 
Tv.setcompounddrawablepadding (APPINFOUTILS.DIPTOPX (2)); 
Tv.setid (i); 
Tv.setonclicklistener (this); 
Textviews[i] = TV; 
Tempbaseview.addview (TV); Menubaseview.addview (tempbaseView); 
if (hinttexts!= null && i < hinttexts.length) {Tv.settext (hinttexts[i)); 
View Lineview = new View (GetContext ()); 
Lineview.setbackgroundcolor (Getresources (). GetColor (r.color.main_bg_in)); 
Menubaseview.addview (Lineview, New Layoutparams (APPINFOUTILS.DIPTOPX (1), HEIGHT-APPINFOUTILS.DIPTOPX (8)); 
} addview (Menubaseview); 
Lineview = new View (GetContext ()); 
Lineview.setbackgroundcolor (Getresources (). GetColor (r.color.main_bg_in)); 
AddView (Lineview, New Layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, lineheight)); 
/*** * Level Menu click event Trigger * @param v/@Override public void OnClick (View v) {Lasttv = (TextView) v; 
int column = V.getid (); 
list<menumodel> list = Datasource[column]; 
Lasttv.setcompounddrawableswithintrinsicbounds (0, 0, r.mipmap.triangle_up, 0); 
Lasttv.settextcolor (Getresources (). GetColor (textcolorselected)); 
Menupopupwindow.setleftlist (column, list); 
Indexpath Indexpath = Indexpaths[column]; Menupopupwindow.setselect (INDEXPATH.Row, Indexpath.item); 
int[] location = new INT[2]; 
Lineview.getlocationonscreen (location); 
Menupopupwindow.showasdropdown (Lineview); 
Menupopupwindow.showatlocation (this,gravity.bottom,0,0); /*** * Pop-up Box click event Processing * @param column * @param row * @param item * @param menumodel/@Override public void OnMenu ( 
int column, int row, int item, Menumodel menumodel) {TextView Lasttv = Textviews[column]; 
Lasttv.settext (Menumodel.value); 
Indexpath Indexpath = Indexpaths[column]; 
Indexpath.row = row; 
Indexpath.item = Item; 
Onmenudismiss (); 
if (Onmenulistener!= null) {Onmenulistener.onmenu (Indexpath, Menumodel); }/*** * Pop-up box closed/@Override public void Onmenudismiss () {Lasttv.settextcolor () (Getresources (). GetColor (R.color.gra 
Y_80)); 
Lasttv.setcompounddrawableswithintrinsicbounds (0, 0, r.mipmap.triangle_down, 0); /*** * Set Listener * @param onmenulistener */public void Setonmenulistener (Onmenulistener onmenulistener) {This.onmenu 
Listener = Onmenulistener; } 
public static interface Onmenulistener {void OnMenu (Indexpath indexpath, Menumodel Menumodel);  /**** * Menu single row, line, level two line/public static class Indexpath {public int column;//First level menu public int row;//left Row public int item; 
Right row public indexpath (int column, int row, int item) {this.column = column; 
This.row = row; 
This.item = Item; } 
} 
}

2, Popupwindow is the main implementation of the pop-up box to display the first and two levels of the menu data.

I used two ListView to dynamically load the data.

The specific code is as follows:

Package Com.spring.sky.menuproject.view; 
Import Android.content.Context; 
Import android.graphics.drawable.PaintDrawable; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import android.view.animation.Animation; 
Import Android.view.animation.AnimationUtils; 
Import Android.widget.AdapterView; 
Import Android.widget.LinearLayout; 
Import Android.widget.ListView; 
Import Android.widget.PopupWindow; 
Import COM.SPRING.SKY.MENUPROJECT.R; 
Import java.util.List; 
/** * Created by Springsky on 16/10/20. 
* * public class Menupopupwindow extends Popupwindow implements Adapterview.onitemclicklistener {context Mcontext; 
Private ListView LEFTLV,RIGHTLV; 
Private Onmenulistener Onmenulistener; 
Private list<menumodel> leftlist,rightlist; 
Private Menuadapter Menuleftadapter,menurightadapter; 
private int column; 
Boolean Hassecond; 
/*** * Initialization * @param context */public Menupopupwindow (context context) {This.mcontext = context; ViEW view = Layoutinflater.from (Mcontext). Inflate (R.layout.menu_popup_window, NULL); 
LEFTLV = (ListView) View.findviewbyid (R.ID.LEFTLV); 
Leftlv.setchoicemode (Listview.choice_mode_single); 
RIGHTLV = (ListView) View.findviewbyid (R.ID.RIGHTLV); 
Rightlv.setchoicemode (Listview.choice_mode_single); 
Setcontentview (view); 
Setbackgrounddrawable (New paintdrawable ()); 
Setfocusable (TRUE); 
SetWidth (ViewGroup.LayoutParams.MATCH_PARENT); 
SetHeight (LinearLayout.LayoutParams.WRAP_CONTENT);  
Setondismisslistener (New Popupwindow.ondismisslistener () {@Override public void Ondismiss () {leftlv.setselection (0); 
Rightlv.setselection (0); 
if (Onmenulistener!= null) {Onmenulistener.onmenudismiss (); 
} 
} 
}); 
Menuleftadapter = new Menuadapter (mcontext); 
Menuleftadapter.setcolumn (0); 
Menuleftadapter.setlist (leftlist); 
Leftlv.setadapter (Menuleftadapter); 
Leftlv.setonitemclicklistener (this); 
Menurightadapter = new Menuadapter (mcontext); 
Menurightadapter.setcolumn (1); Menurightadapter.setlisT (rightlist); 
Rightlv.setadapter (Menurightadapter); 
Rightlv.setonitemclicklistener (this); 
@Override public void Showasdropdown (View anchor) {Super.showasdropdown (anchor); /*** * Load Data * @param column * @param leftlist/public void setleftlist (int column,list<menumodel> leftlist 
) {this.column = column; 
This.leftlist = leftlist; 
Hassecond = false; 
for (Menumodel childmodel:leftlist) {if (Childmodel.haschind ()) {Hassecond = true; 
Break 
} menuleftadapter.setlist (Leftlist); 
if (!hassecond) {rightlv.setvisibility (view.gone); 
Setrightlist (NULL); 
}else {rightlv.setvisibility (view.visible); }/*** * Default selected level and two rows * @param row * @param item */public void setselect (int row,int Item) {if (Row < 0 | | lef Tlist = = NULL | | 
Row >= leftlist.size ()) {return; 
} Menumodel Leftmodel = Leftlist.get (row); 
Leftlv.setselection (row); 
Menuleftadapter.setselectposition (row); 
Setrightlist (Leftmodel.chindmenu); if (Item < 0 | | rightlist ==null | | iTEM >= rightlist.size ()) {return; 
} rightlv.setselection (item); 
Menurightadapter.setselectposition (item); 
} private void Setrightlist (list<menumodel> rightlist) {this.rightlist = rightlist; 
Menurightadapter.setlist (rightlist); @Override public void Onitemclick (adapterview<?> parent, view view, int position, long id) {if (Parent.getid () 
= = Leftlv.getid ()) {Menumodel model = Leftlist.get (position); 
if (leftlv.getselecteditemposition () = = position) {return; 
} if (Model.haschind ()) {menuleftadapter.setselectposition (position); 
Setrightlist (Model.chindmenu); 
}else {dismiss (); 
} onmenuclick (Position,0,model); 
}else {menurightadapter.setselectposition (position); 
Menumodel model = rightlist.get (position); 
Onmenuclick (Menuleftadapter.getselectposition (), Position,model); 
Dismiss (); } void Onmenuclick (int row,int Item,menumodel model) {if (Onmenulistener!= null) {Onmenulistener.onmenu (column,row,i 
Tem,model); }} public void Setonmenulistener(Onmenulistener Onmenulistener) 
{This.onmenulistener = Onmenulistener; 
public static interface onmenulistener{void OnMenu (int column, int row, int item, Menumodel Menumodel); 
void Onmenudismiss (); } 
}

3, the other is Menumodel, consider the multi-level relationship, so we recommend the use of chain structure.

Package Com.spring.sky.menuproject.view; 
Import java.util.List; 
/** 
* Created by Springsky on 16/10/20. 
*/Public 
class Menumodel {public 
Object key;//key public 
String value;//display content public 
list< Menumodel> Chindmenu; Sub-list data public 
Menumodel () { 
super (); 
} 
Public Menumodel (Object key, String value, list<menumodel> chindmenu) { 
super (); 
This.key = key; 
This.value = value; 
This.chindmenu = Chindmenu; 
} 
/*** 
* Do you have sub-list data 
* @return 
/Public 
Boolean Haschind () {return 
(chindmenu!= null && Chindmenu.size () > 0); 
} 

Eh, life pressure, and will not write a blog, simply describe, I hope we do not take offense.

The source code of the project, I have submitted to git on.

Download Address: Https://github.com/skyfouk/AndroidMenuProject.git

The above is a small set of iOS to introduce the client menu features imitation Baidu glutinous rice/American Group II menu, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.