当前位置: 移动技术网 > IT编程>移动开发>Android > Android制作简单的普通购物车

Android制作简单的普通购物车

2019年07月24日  | 移动技术网IT编程  | 我要评论

法克 尤尔,别了温哥华剧情,小魔仙找茬

本文实例为大家分享了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一样******************************/
@override
public int getgroupcount() {
 return this.group_head.size();
}

@override
public int getchildrencount(int position) {
 if (position < 0 || position >= this.child.size())
 return 0;
 return child.get(position).size();
}

@override
public orderdetailsentity getgroup(int groupposition) {
 return group_head.get(groupposition);
}

@override
public productdetails getchild(int groupposition, int childposition) {

 return child.get(groupposition).get(childposition);
}

@override
public long getgroupid(int groupposition) {
 return groupposition;
}

@override
public long getchildid(int groupposition, int childposition) {
 return childposition;
}
/**************************************************************************/
@override
public 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方法一样**********************/
@override
public 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;
}

@override
public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview,
 viewgroup parent) {
 viewholderchild childv;
 if (convertview == null) {
 convertview = inflater.inflate(r.layout.item_inner, parent, 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;
}

/****************************************************************************/

@override
public 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() {
@override
public 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() {
@override
public boolean onchildclick(expandablelistview parent, view v, int groupposition, int childposition, long id) {
 toast.maketext(getapplicationcontext(), childstrings[groupposition][childposition], toast.lengthshow();
 return true;
}
});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网