美团电商应用平台大家使用非常频繁,下面小编通过本文给大家介绍电商应用平台中常用的选择类别下拉列表的实现。先给大家展示下效果图:
一、下拉列表的实现
其实实现方法有很多,这时实现的也没有什么技术含量,只是总结下自己在项目中的做法,也提供一个思路。
首先是列表的数据,一般数据都是从后台读过来,这里因为没有后台,所以写死在客户端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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中:
1
2
3
4
5
6
7
8
9
|
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 }); |
设置点击标题头弹出列表,并改变标题头的颜色
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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这个选择标题的正下方。从而实现上面那种方式。
最后完整的贴出代码,还是蛮简单的。最后也会提供代码下载链接。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
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的动画来实现这种效果,非常简单:
1
|
<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来画一张图,它是环形的,且环形圈中有渐变颜色。如下:
1
2
3
4
5
|
<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仿美团下拉菜单(商品选购)实例代码,希望对大家有所帮助!