Android makes simple ordinary shopping cart _android

Source: Internet
Author: User
Tags static class

This example for you to share the Android ordinary shopping cart production process for your reference, the specific content as follows

1. The latest project has added similar shopping cart features, as shown in the following illustration:

When just saw this page, the first reaction is to use ListView nesting listview, after some operation finally also realized this function. No performance issues were considered at the time, only to be written. Later nested data, when the data volume is large, sliding ListView can obviously feel the cotton, which is very difficult for users to endure, so have to find the idea of alternative solutions, see the online mainstream is to use Expandablelistview to achieve this function, so I also use this scheme to write.

2. After the formation of the demo, as shown in the following figure:

3. Thinking:
1). Choose Expandablelistview as Control
2. Add a check mark to each data source, ischecked, control the state of the checkbox according to the ischecked;

Expandablelistview related popularization

#1. First look at the Expandablelistview adapter, unlike the ordinary ListView adapter

public class Myexpandadapter extends Baseexpandablelistadapter {//Red part of the data source list<orderdetailsentity> Group_head =
New ArrayList ();
Inner part data source list<list<productdetails>> child = new ArrayList ();
Context context;

Layoutinflater Inflater;
 Public Myexpandadapter {//initialization group, sub list item Group_head = new Arraylist<orderdetailsentity> ();
 Child = new arraylist<list<productdetails>> ();
Inflater = Layoutinflater.from (context); Public Myexpandadapter, list<orderdetailsentity> Group_head, list<list<productdetails
 >> child) {this.context = context;
 Initialize Group, sub list item this.group_head = Group_head;
 This.child = child;
Inflater = Layoutinflater.from (context);
 /**************************** as normal 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 (Ch
Ildposition);

@Override public long getgroupid (int groupposition) {return groupposition;} @Override public long Getchildid (int groupposition, int childposition) {return childposition;}/*********************** /@Override public boolean hasstableids () {//Group and child options hold a stable ID,
This means that changes in the underlying data will not affect their return true;
 Private Boolean Isselectall (orderdetailsentity entity) {int count = 0;
 For (ProductDetails p:entity.getproductdetails ()) {if (p.ischecked ()) {count++;
Return Entity.getproductdetails (). Size () = = count; /************************* is the same as the GetView method for normal adapter **********************/@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, VIEWGR
 OUP 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) {////Whether a child view of the specified location is selectable. Invoke the Childselect method//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 launched

for (int i = 0; i < Adapter.getgroupcount (); i++) { 
sp_date_list.expandgroup (i); 
}


3). Expandablelistview Remove default icon

sp_date_list.setgroupindicator (null);

4). Expandablelistview ItemClick Event Handling

1. Setonchildclicklistener
2. Setongroupclicklistener
3. Setongroupcollapselistener
4. Setongroupexpandlistener

By means of the method name we can know their purpose, they set the click of the child options, click Group Items, Group merge, group expand Listener.

Set the click-Listening event for the grouped item
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 ();
 If you are handling an event, return true and the default return false is return
 false;
}
Set child options Click on the Listener Event
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;
}
});

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.