Android仿美团下拉菜单(商品选购)实例代码
美团电商应用平台大家使用非常频繁,下面小编通过本文给大家介绍电商应用平台中常用的选择类别下拉列表的实现。先给大家展示下效果图:
一、下拉列表的实现
其实实现方法有很多,这时实现的也没有什么技术含量,只是总结下自己在项目中的做法,也提供一个思路。
首先是列表的数据,一般数据都是从后台读过来,这里因为没有后台,所以写死在客户端:
private void initmenudata() { menudata = new arraylist<map<string, string="">>(); string[] menustr = new string[] { "全部", "粮油", "衣服", "图书", "电子产品", "酒水饮料", "水果" }; map<string, string=""> map; for (int i = , len = menustr.length; i < len; ++i) { map = new hashmap<string, string="">(); map.put("name", menustr[i]); menudata.add(map); } menudata = new arraylist<map<string, string="">>(); string[] menustr = new string[] { "综合排序", "配送费最低" }; map<string, string=""> map; for (int i = , len = menustr.length; i < len; ++i) { map = new hashmap<string, string="">(); map.put("name", menustr[i]); menudata.add(map); } menudata = new arraylist<map<string, string="">>(); string[] menustr = new string[] { "优惠活动", "特价活动", "免配送费", "可在线支付" }; map<string, string=""> map3; for (int i = 0, len = menustr3.length; i < len; ++i) { map3 = new hashmap<string, string="">(); map3.put("name", menustr3[i]); menudata3.add(map3); } }</string,></string,></map<string,></string,></string,></map<string,></string,></string,></map<string,>
就是做了简单的封装。弹出列表的实现考虑使用popwindow。
popmenu = new popupwindow(contentview, linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent); popmenu.setoutsidetouchable(true); popmenu.setbackgrounddrawable(new bitmapdrawable()); popmenu.setfocusable(true); popmenu.setanimationstyle(r.style.popwin_anim_style); popmenu.setondismisslistener(new ondismisslistener() { public void ondismiss() { producttv.settextcolor(color.parsecolor("#5a5959")); sorttv.settextcolor(color.parsecolor("#5a5959")); activitytv.settextcolor(color.parsecolor("#5a5959")); } });
接着将数据封装到adapter中:
menuadapter1 = new simpleadapter(this, menudata1, r.layout.item_listview_popwin, new string[] { "name" }, new int[] { r.id.listview_popwind_tv }); menuadapter2 = new simpleadapter(this, menudata2, r.layout.item_listview_popwin, new string[] { "name" }, new int[] { r.id.listview_popwind_tv }); menuadapter3 = new simpleadapter(this, menudata3, r.layout.item_listview_popwin, new string[] { "name" }, new int[] { r.id.listview_popwind_tv });
设置点击标题头弹出列表,并改变标题头的颜色
public void onclick(view v) { // todo auto-generated method stub switch (v.getid()) { case r.id.supplier_list_product: producttv.settextcolor(color.parsecolor("#ac")); poplistview.setadapter(menuadapter); popmenu.showasdropdown(product, , ); menuindex = ; break; case r.id.supplier_list_sort: sorttv.settextcolor(color.parsecolor("#ac")); poplistview.setadapter(menuadapter); popmenu.showasdropdown(product, , ); menuindex = ; break; case r.id.supplier_list_activity: activitytv.settextcolor(color.parsecolor("#ac")); poplistview.setadapter(menuadapter); popmenu.showasdropdown(product, , ); menuindex = ; break; } }
showasdropdown是为了让popwindow定位在product这个选择标题的正下方。从而实现上面那种方式。
最后完整的贴出代码,还是蛮简单的。最后也会提供代码下载链接。
public class mainactivity extends activity implements onclicklistener { private listview listview, poplistview; private progressbar progressbar; private list<map<string, string="">> menudata1, menudata2, menudata3; private popupwindow popmenu; private simpleadapter menuadapter1, menuadapter2, menuadapter3; private linearlayout product, sort, activity; private imageview cartiv; private textview producttv, sorttv, activitytv, titletv; private int green, grey; private string currentproduct, currentsort, currentactivity; private int menuindex = 0; private intent intent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_supplier_list); findview(); initmenudata(); initpopmenu(); } private void initmenudata() { menudata1 = new arraylist<map<string, string="">>(); string[] menustr1 = new string[] { "全部", "粮油", "衣服", "图书", "电子产品", "酒水饮料", "水果" }; map<string, string=""> map1; for (int i = 0, len = menustr1.length; i < len; ++i) { map1 = new hashmap<string, string="">(); map1.put("name", menustr1[i]); menudata1.add(map1); } menudata2 = new arraylist<map<string, string="">>(); string[] menustr2 = new string[] { "综合排序", "配送费最低" }; map<string, string=""> map2; for (int i = 0, len = menustr2.length; i < len; ++i) { map2 = new hashmap<string, string="">(); map2.put("name", menustr2[i]); menudata2.add(map2); } menudata3 = new arraylist<map<string, string="">>(); string[] menustr3 = new string[] { "优惠活动", "特价活动", "免配送费", "可在线支付" }; map<string, string=""> map3; for (int i = 0, len = menustr3.length; i < len; ++i) { map3 = new hashmap<string, string="">(); map3.put("name", menustr3[i]); menudata3.add(map3); } } @override public void onclick(view v) { // todo auto-generated method stub switch (v.getid()) { case r.id.supplier_list_product: producttv.settextcolor(color.parsecolor("#39ac69")); poplistview.setadapter(menuadapter1); popmenu.showasdropdown(product, 0, 2); menuindex = 0; break; case r.id.supplier_list_sort: sorttv.settextcolor(color.parsecolor("#39ac69")); poplistview.setadapter(menuadapter2); popmenu.showasdropdown(product, 0, 2); menuindex = 1; break; case r.id.supplier_list_activity: activitytv.settextcolor(color.parsecolor("#39ac69")); poplistview.setadapter(menuadapter3); popmenu.showasdropdown(product, 0, 2); menuindex = 2; break; } } protected void findview() { listview = (listview) findviewbyid(r.id.supplier_list_lv); product = (linearlayout) findviewbyid(r.id.supplier_list_product); sort = (linearlayout) findviewbyid(r.id.supplier_list_sort); activity = (linearlayout) findviewbyid(r.id.supplier_list_activity); producttv = (textview) findviewbyid(r.id.supplier_list_product_tv); sorttv = (textview) findviewbyid(r.id.supplier_list_sort_tv); activitytv = (textview) findviewbyid(r.id.supplier_list_activity_tv); titletv = (textview) findviewbyid(r.id.supplier_list_title_tv); cartiv = (imageview) findviewbyid(r.id.supplier_list_cart_iv); progressbar = (progressbar) findviewbyid(r.id.progress); product.setonclicklistener(this); sort.setonclicklistener(this); activity.setonclicklistener(this); cartiv.setonclicklistener(this); } private void initpopmenu() { initmenudata(); view contentview = view.inflate(this, r.layout.popwin_supplier_list, null); popmenu = new popupwindow(contentview, linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent); popmenu.setoutsidetouchable(true); popmenu.setbackgrounddrawable(new bitmapdrawable()); popmenu.setfocusable(true); popmenu.setanimationstyle(r.style.popwin_anim_style); popmenu.setondismisslistener(new ondismisslistener() { public void ondismiss() { producttv.settextcolor(color.parsecolor("#5a5959")); sorttv.settextcolor(color.parsecolor("#5a5959")); activitytv.settextcolor(color.parsecolor("#5a5959")); } }); poplistview = (listview) contentview .findviewbyid(r.id.popwin_supplier_list_lv); contentview.findviewbyid(r.id.popwin_supplier_list_bottom) .setonclicklistener(new onclicklistener() { public void onclick(view arg0) { popmenu.dismiss(); } }); menuadapter1 = new simpleadapter(this, menudata1, r.layout.item_listview_popwin, new string[] { "name" }, new int[] { r.id.listview_popwind_tv }); menuadapter2 = new simpleadapter(this, menudata2, r.layout.item_listview_popwin, new string[] { "name" }, new int[] { r.id.listview_popwind_tv }); menuadapter3 = new simpleadapter(this, menudata3, r.layout.item_listview_popwin, new string[] { "name" }, new int[] { r.id.listview_popwind_tv }); poplistview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview arg0, view arg1, int pos, long arg3) { popmenu.dismiss(); if (menuindex == 0) { currentproduct = menudata1.get(pos).get("name"); titletv.settext(currentproduct); producttv.settext(currentproduct); toast.maketext(mainactivity.this, currentproduct, toast.length_short).show(); } else if (menuindex == 1) { currentsort = menudata2.get(pos).get("name"); titletv.settext(currentsort); sorttv.settext(currentsort); toast.maketext(mainactivity.this, currentsort, toast.length_short).show(); } else { currentactivity = menudata3.get(pos).get("name"); titletv.settext(currentactivity); activitytv.settext(currentactivity); toast.maketext(mainactivity.this, currentactivity, toast.length_short).show(); } } }); } }</string,></string,></map<string,></string,></string,></map<string,></string,></string,></map<string,></map<string,>
二、加载圆形progressbar的显示
就是效果图中的那种加载progressbar,圆形progresbar可以用原生的bar来实现,但样式单一,之前我做这种效果第一时间总是考虑到帧动画,但用这种方式需要有很多图片来链接起来,这样一来实现麻烦,二来图片多了占内存。下面用改变原生progressbar的动画来实现这种效果,非常简单:
<progressbar android:id="@+id/progress" android:indeterminatedrawable="@drawable/shape_progress" android:indeterminateduration="1000" android:layout_centerinparent="true" android:layout_height="wrap_content" android:layout_width="wrap_content"></progressbar>
indeterminatedrawable是加载背景图片,indeterminateduration是旋转的速度。这里的思路是用xml来画一张图,它是环形的,且环形圈中有渐变颜色。如下:
<rotate android:fromdegrees="" android:pivotx="%" android:pivoty="%" android:todegrees="" xmlns:android="http://schemas.android.com/apk/res/android"> <shape android:innerradiusratio="" android:shape="ring" android:thicknessratio="" android:uselevel="false"> <gradient android:centercolor="#cdaa" android:centery="." android:endcolor="#ffffff" android:startcolor="#ac" android:type="sweep" android:uselevel="false"> </gradient></shape> </rotate>
rotate设置旋转动画,360度旋转。shape="ring"设置背景为圆。android:innerradiusratio="3"设置内环半径,android:thicknessratio="10"设置外环半径。最后为了让环中颜色有渐变效果,使用gradient来设置。gradient可以有三种渐变方式,线性,辐射,扫描。这里type要设置成扫描。然后设置中心点,开始颜色和结束颜色,就能实现上面的那种效果了。
以上内容是小编给大家介绍的android仿美团下拉菜单(商品选购)实例代码,希望对大家有所帮助!