Android仿美团下拉菜单(商品选购)实例代码

Android仿美团下拉菜单(商品选购)实例代码,第1张

概述美团电商应用平台大家使用非常频繁,下面小编通过本文给大家介绍电商应用平台中常用的选择类别下拉列表的实现。先给大家展示下效果图:

美团电商应用平台大家使用非常频繁,下面小编通过本文给大家介绍电商应用平台中常用的选择类别下拉列表的实现。先给大家展示下效果图:

一、下拉列表的实现

其实实现方法有很多,这时实现的也没有什么技术含量,只是总结下自己在项目中的做法,也提供一个思路。

首先是列表的数据,一般数据都是从后台读过来,这里因为没有后台,所以写死在客户端:

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="">>();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,>

就是做了简单的封装。d出列表的实现考虑使用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,new int[] { R.ID.ListvIEw_popwind_tv });menuAdapter3 = new SimpleAdapter(this,menuData3,new int[] { R.ID.ListvIEw_popwind_tv });

设置点击标题头d出列表,并改变标题头的颜色

public voID onClick(VIEw v) {// Todo auto-generated method stubswitch (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 implementsOnClickListener {private ListVIEw ListVIEw,popListVIEw;private Progressbar progressbar;private List<map<string,string="">> menuData1,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;@OverrIDeprotected 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[] { "全部",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[] { "综合排序",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[] { "优惠活动",menuStr3[i]);menuData3.add(map3);}}@OverrIDepublic voID onClick(VIEw v) {// Todo auto-generated method stubswitch (v.getID()) {case R.ID.supplier_List_product:productTv.setTextcolor(color.parsecolor("#39ac69"));popListVIEw.setAdapter(menuAdapter1);popMenu.showAsDropDown(product,2);menuIndex = 0;break;case R.ID.supplier_List_sort:sortTv.setTextcolor(color.parsecolor("#39ac69"));popListVIEw.setAdapter(menuAdapter2);popMenu.showAsDropDown(product,2);menuIndex = 1;break;case R.ID.supplier_List_activity:activityTv.setTextcolor(color.parsecolor("#39ac69"));popListVIEw.setAdapter(menuAdapter3);popMenu.showAsDropDown(product,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);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,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,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,>

二、加载圆形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="@R_403_6822@://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仿美团下拉菜单(商品选购)实例代码,希望对大家有所帮助!

总结

以上是内存溢出为你收集整理的Android仿美团下拉菜单(商品选购)实例代码全部内容,希望文章能够帮你解决Android仿美团下拉菜单(商品选购)实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149573.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存