Android制作简单的普通购物车

Android制作简单的普通购物车,第1张

概述本文实例为大家分享了Android普通购物车制作过程,供大家参考,具体内容如下

本文实例为大家分享了AndroID普通购物车制作过程,供大家参考,具体内容如下

1.最新项目新增了类似购物车功能,如下图所示:

当时刚看到此页面的时候,第一反应是利用 ListVIEw嵌套ListvIEw,经过一番 *** 作最终也实现了此功能。当时也没有考虑性能问题,只考虑能写出来。后来嵌套数据,当数据量较大时,滑动ListvIEw可以明显感觉到卡顿,这对用户来说是很难忍受的,所以才有了找到替代方案的想法,看到网上主流的是用ExpandableListVIEw来实现此功能,所以我也用此方案来写一下。

2.成型后的Demo,如下图所示:

3.思路:
 1).选用ExpandableListVIEw作为控件
 2).给每个数据源添加一个选中标志,isChecked,根据ischecked,控制checkBox的状态;

ExpandableListVIEw相关普及

#1.首先看下ExpandableListVIEw的adapter,与普通的ListVIEw adapter不同

public class MyExpandAdapter extends Baseexpandablelistadapter {//红色部分的数据源List<OrderDetailsEntity> group_head = new ArrayList();//内层部分数据源List<List<ProductDetails>> child = new ArrayList();Context context;LayoutInflater inflater;public MyExpandAdapter(Context content) { // 初始化组、子列表项 group_head = new ArrayList<OrderDetailsEntity>(); child = new ArrayList<List<ProductDetails>>(); inflater = LayoutInflater.from(context);}public MyExpandAdapter(Context context,List<OrderDetailsEntity> group_head,List<List<ProductDetails>> child) { this.context = context; // 初始化组、子列表项 this.group_head = group_head; this.child = child; inflater = LayoutInflater.from(context);}/****************************跟普通adapter一样******************************/@OverrIDepublic int getGroupCount() { return this.group_head.size();}@OverrIDepublic int getChildrenCount(int position) { if (position < 0 || position >= this.child.size()) return 0; return child.get(position).size();}@OverrIDepublic OrderDetailsEntity getGroup(int groupposition) { return group_head.get(groupposition);}@OverrIDepublic ProductDetails getChild(int groupposition,int childposition) { return child.get(groupposition).get(childposition);}@OverrIDepublic long getGroupID(int groupposition) { return groupposition;}@OverrIDepublic long getChildID(int groupposition,int childposition) { return childposition;}/**************************************************************************/@OverrIDepublic boolean hasStableIDs() {//分组和子选项是否持有稳定的ID,就是说底层数据的改变会不会影响到它们 return true;}private boolean isSelectAll(OrderDetailsEntity entity) { int count = 0; for (ProductDetails p : entity.getProductDetails()) { if (p.isChecked()) { count++; } } return entity.getProductDetails().size() == count;}/*************************与普通adapter的getVIEw方法一样**********************/@OverrIDepublic VIEw getGroupVIEw(int groupposition,boolean isExpanded,VIEw convertVIEw,VIEwGroup parent) { VIEwHolderGroup group; if (convertVIEw == null) { convertVIEw = inflater.inflate(R.layout.item,parent,false); group = new VIEwHolderGroup(); group.cb_all = (CheckBox) convertVIEw.findVIEwByID(R.ID.cb_checkAll); group.tv_name = (TextVIEw) convertVIEw.findVIEwByID(R.ID.tv_name); convertVIEw.setTag(group); } else { group = (VIEwHolderGroup) convertVIEw.getTag(); } group.tv_name.setText( group_head.get(groupposition).getMemberID() + "," + group_head.get(groupposition).getShopname()); group.cb_all.setChecked(isSelectAll(getGroup(groupposition))); return convertVIEw;}@OverrIDepublic VIEw getChildVIEw(int groupposition,int childposition,boolean isLastChild,VIEwGroup parent) { VIEwHolderChild childV; if (convertVIEw == null) { convertVIEw = inflater.inflate(R.layout.item_inner,false); childV = new VIEwHolderChild(); childV.cb = (CheckBox) convertVIEw.findVIEwByID(R.ID.cb_check); childV.iv = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.iv_img); childV.name = (TextVIEw) convertVIEw.findVIEwByID(R.ID.name); convertVIEw.setTag(childV); } else { childV = (VIEwHolderChild) convertVIEw.getTag(); } childV.name.setText(getChild(groupposition,childposition).getProductname()); childV.cb.setChecked(getChild(groupposition,childposition).isChecked()); return convertVIEw;}/****************************************************************************/@OverrIDepublic boolean isChildSelectable(int groupposition,int childposition) { //// 指定位置的子视图是否可选择。 // 调用Activity里的ChildSelect方法 // Toast.makeText(context,"gp="+groupposition+",cp="+childposition,// Toast.LENGTH_LONG).show(); return true;}static class VIEwHolderGroup { CheckBox cb_all; TextVIEw tv_name;}static class VIEwHolderChild { CheckBox cb; ImageVIEw iv; TextVIEw name;}}

 

#2.ExpandableListVIEw 展开 

for (int i = 0; i < adapter.getGroupCount(); i++) { sp_date_List.expandGroup(i); }


3).ExpandableListVIEw 去掉默认图标

 sp_date_List.setGroupIndicator(null);

4).ExpandableListVIEw itemClick事件处理

 1. setonChildClickListener
2. setonGroupClickListener
3. setonGroupCollapseListener
4. setonGroupExpandListener

通过方法名我们就能知道各自的用途,它们分别设置单击子选项、单击分组项、分组合并、分组展开的监听器。

// 设置分组项的点击监听事件expandableListVIEw.setonGroupClickListener(new ExpandableListVIEw.OnGroupClickListener() {@OverrIDepublic boolean onGroupClick(ExpandableListVIEw expandableListVIEw,VIEw vIEw,int i,long l) { Toast.makeText(getApplicationContext(),groupStrings[i],Toast.LENGTH_SHORT).show(); //如果处理事件,return true,默认return false return false;}// 设置子选项点击监听事件expandableListVIEw.setonChildClickListener(new ExpandableListVIEw.OnChildClickListener() {@OverrIDepublic boolean onChildClick(ExpandableListVIEw parent,VIEw v,int groupposition,long ID) { Toast.makeText(getApplicationContext(),childStrings[groupposition][childposition],Toast.LENGTHshow(); return true;}});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android制作简单的普通购物车全部内容,希望文章能够帮你解决Android制作简单的普通购物车所遇到的程序开发问题。

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

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

原文地址:https://www.54852.com/web/1148704.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存