最近项目需要实现类似淘宝购物车的功能,仿了一个,直接上代码:
public class MainActivity extends Activity implements OnCartListener, View.OnClickListener { private static final boolean DEBUG = true; private static final String TAG = MainActivity.class.getSimpleName(); private ExpandableListView mCartExpandableListView; private TextView mAllCostView; private TextView mSettlementButton; private CheckBox mAllCheckView; private CartAdapter mCartAdapter; private ArrayList<CartEntity> mCartEntityList = new ArrayList<CartEntity>(); private boolean isAllCheck; private void initView() { mAllCostView = (TextView) findViewById(R.id.all_cost); mSettlementButton = (TextView) findViewById(R.id.settlement); mCartExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView); mAllCheckView = (CheckBox) findViewById(R.id.all_check); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setUpCart(); mAllCheckView.setOnClickListener(this); mSettlementButton.setOnClickListener(this); } private void setUpCart() { mCartAdapter = new CartAdapter(this, mCartEntityList); mCartExpandableListView.setAdapter(mCartAdapter); mCartAdapter.setOnCartListener(this); mCartEntityList.addAll(getData()); mCartAdapter.notifyDataSetChanged(); for (int i = 0; i < mCartEntityList.size(); i++) { mCartExpandableListView.expandGroup(i); } } private ArrayList<CartEntity> getData() { ArrayList<CartEntity> cartEntityList = new ArrayList<CartEntity>(); for (int i = 0; i < 3; i++) { CartEntity cartEntity = new CartEntity(); ArrayList<ProductEntity> productEntityList = new ArrayList<ProductEntity>(); for (int j = 0; j < Math.random() * 2 + 1; j++) { ProductEntity productEntity = new ProductEntity(); productEntity.setProductPrice((float) (Math.random() * 100)); productEntity.setProductCount(1); productEntityList.add(productEntity); } cartEntity.setProductEntityList(productEntityList); cartEntityList.add(cartEntity); } return cartEntityList; } private void setCheckGroup(int groupPosition, boolean check) { int len = mCartEntityList.get(groupPosition).getProductEntityList().size(); mCartEntityList.get(groupPosition).setGroupCheck(check); for (int i = 0; i < len; i++) { mCartEntityList.get(groupPosition).getProductEntityList().get(i).setChildCheck(check); } } private void setCheckChild(int groupPosition, int childPosition, boolean check) { mCartEntityList.get(groupPosition).getProductEntityList().get(childPosition).setChildCheck(check); int len = mCartEntityList.get(groupPosition).getProductEntityList().size(); boolean groupCheck = true; for (int i = 0; i < len; i++) { if (!mCartEntityList.get(groupPosition).getProductEntityList().get(i).isChildCheck()) { groupCheck = false; } } mCartEntityList.get(groupPosition).setGroupCheck(groupCheck); } private void handleAllCost() { float allCost = 0f; int allCount = 0; int groupLen = mCartEntityList.size(); for (int i = 0; i < groupLen; i++) { int childLen = mCartEntityList.get(i).getProductEntityList().size(); for (int j = 0; j < childLen; j++) { if (mCartEntityList.get(i).getProductEntityList().get(j).isChildCheck()) { allCost += mCartEntityList.get(i).getProductEntityList().get(j).getProductPrice() * mCartEntityList.get(i).getProductEntityList().get(j).getProductCount(); allCount++; } } } mAllCostView.setText("¥" + String.format("%.2f", allCost)); mSettlementButton.setText("结算(" + allCount + ")"); } private void deleteProduct(int groupPosition, int childPosition) { mCartEntityList.get(groupPosition).getProductEntityList().remove(childPosition); if (mCartEntityList.get(groupPosition).getProductEntityList().isEmpty()) { mCartEntityList.remove(groupPosition); } } @Override public void groupCheck(int groupPosition) { log("---groupCheck()---" + "groupPosition:" + groupPosition); setCheckGroup(groupPosition, !mCartEntityList.get(groupPosition).isGroupCheck()); handleAllCost(); mCartAdapter.notifyDataSetChanged(); } @Override public void groupClick() { log("---groupClick()---"); mCartAdapter.notifyDataSetChanged(); } @Override public void groupEdit(int groupPosition) { log("---groupEdit()---"); mCartEntityList.get(groupPosition).setGroupEdit(!mCartEntityList.get(groupPosition).isGroupEdit()); mCartAdapter.notifyDataSetChanged(); } @Override public void childCheck(int groupPosition, int childPosition) { log("---childCheck()---" + "groupPosition:" + groupPosition + ",childPosition:" + childPosition); setCheckChild(groupPosition, childPosition, !mCartEntityList.get(groupPosition).getProductEntityList().get(childPosition).isChildCheck()); handleAllCost(); mCartAdapter.notifyDataSetChanged(); } @Override public void childDelete(int groupPosition, int childPosition) { log("---childDelete()---" + "groupPosition:" + groupPosition + ",childPosition:" + childPosition); deleteProduct(groupPosition, childPosition); handleAllCost(); mCartAdapter.notifyDataSetChanged(); } @Override public void childIncrease(int groupPosition, int childPosition) { log("---childIncrease()---"); mCartEntityList.get(groupPosition).getProductEntityList().get(childPosition).setProductCount (mCartEntityList.get(groupPosition).getProductEntityList().get(childPosition).getProductCount() + 1); mCartAdapter.notifyDataSetChanged(); } @Override public void childReduce(int groupPosition, int childPosition) { log("---childReduce()---"); if(mCartEntityList.get(groupPosition).getProductEntityList().get(childPosition).getProductCount() <= 1) return; mCartEntityList.get(groupPosition).getProductEntityList().get(childPosition).setProductCount (mCartEntityList.get(groupPosition).getProductEntityList().get(childPosition).getProductCount() - 1); mCartAdapter.notifyDataSetChanged(); } @Override public void childClick() { log("---childClick()---"); mCartAdapter.notifyDataSetChanged(); } @Override public void childEdit() { log("---childEdit()---"); mCartAdapter.notifyDataSetChanged(); } private void log(String message) { if (DEBUG) { Log.d(TAG, message); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.all_check: isAllCheck = !isAllCheck; setAllCheck(isAllCheck); break; case R.id.settlement: if (!verifyCart()) { Toast.makeText(MainActivity.this, R.string.select_tip, Toast.LENGTH_SHORT).show(); } break; } } private void setAllCheck(boolean check) { int groupLen = mCartEntityList.size(); for (int i = 0; i < groupLen; i++) { setCheckGroup(i, check); } handleAllCost(); mCartAdapter.notifyDataSetChanged(); } private boolean verifyCart() { int groupLen = mCartEntityList.size(); boolean flag = false; for (int i = 0; i < groupLen; i++) { int childLen = mCartEntityList.get(i).getProductEntityList().size(); for (int j = 0; j < childLen; j++) { if (mCartEntityList.get(i).getProductEntityList().get(j).isChildCheck()) { flag = true; } } } return flag; } }
public interface OnCartListener { public void groupCheck(int groupPosition); public void groupClick(); public void groupEdit(int groupPosition); public void childCheck(int groupPosition,int childPosition); public void childDelete(int groupPosition,int childPosition); public void childIncrease(int groupPosition,int childPosition); public void childReduce(int groupPosition,int childPosition); public void childClick(); public void childEdit(); }
适配器:
public class CartAdapter extends BaseExpandableListAdapter implements View.OnClickListener{ private Context context; private ArrayList<CartEntity> mCartEntityList; public CartAdapter(Context context,ArrayList<CartEntity> cartEntitiyList){ this.context = context; mCartEntityList = cartEntitiyList; } private OnCartListener mCartListener; public void setOnCartListener(OnCartListener cartListener){ mCartListener = cartListener; } @Override public int getGroupCount() { return mCartEntityList.size(); } @Override public int getChildrenCount(int groupPosition) { return mCartEntityList.get(groupPosition).getProductEntityList().size(); } @Override public Object getGroup(int groupPosition) { return mCartEntityList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return mCartEntityList.get(groupPosition).getProductEntityList().get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupViewHolder groupViewHolder = null; if(null == convertView){ groupViewHolder = new GroupViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.item_cart_group,parent,false); groupViewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.group_checkBox); groupViewHolder.shopName = (TextView) convertView.findViewById(R.id.shop_name); groupViewHolder.shopIcon = (ImageView) convertView.findViewById(R.id.shop_icon); groupViewHolder.shoplayout = convertView.findViewById(R.id.shop_layout); groupViewHolder.groupEdit = (TextView) convertView.findViewById(R.id.group_edit); groupViewHolder.groupDividerView = convertView.findViewById(R.id.divider_group); convertView.setTag(groupViewHolder); }else { groupViewHolder = (GroupViewHolder) convertView.getTag(); } if(0 == groupPosition){ groupViewHolder.groupDividerView.setVisibility(View.GONE); }else { groupViewHolder.groupDividerView.setVisibility(View.VISIBLE); } groupViewHolder.checkBox.setChecked(mCartEntityList.get(groupPosition).isGroupCheck()); if(mCartEntityList.get(groupPosition).isGroupEdit()){ groupViewHolder.groupEdit.setText(R.string.complete); }else { groupViewHolder.groupEdit.setText(R.string.edit); } groupViewHolder.checkBox.setTag(R.integer.view_group_tag, groupPosition); groupViewHolder.groupEdit.setTag(R.integer.view_group_tag, groupPosition); groupViewHolder.checkBox.setOnClickListener(this); groupViewHolder.shoplayout.setOnClickListener(this); groupViewHolder.groupEdit.setOnClickListener(this); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHolder childViewHolder = null; if(null == convertView){ childViewHolder = new ChildViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.item_cart_child,parent,false); childViewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.child_checkBox); childViewHolder.productName = (TextView) convertView.findViewById(R.id.product_name); childViewHolder.productIcon = (ImageView) convertView.findViewById(R.id.product_icon); childViewHolder.productCount = (TextView) convertView.findViewById(R.id.product_count); childViewHolder.productPrice = (TextView) convertView.findViewById(R.id.product_price); childViewHolder.productInfo = (TextView) convertView.findViewById(R.id.product_info); childViewHolder.childDeleteButton = (TextView) convertView.findViewById(R.id.child_delete); childViewHolder.viewEdit = convertView.findViewById(R.id.layout_edit); childViewHolder.viewComplete = convertView.findViewById(R.id.layout_complete); childViewHolder.increaseBtn = (ImageView) convertView.findViewById(R.id.btn_increase); childViewHolder.reduceBtn = (ImageView) convertView.findViewById(R.id.btn_reduce); childViewHolder.editProdcutCount = (TextView) convertView.findViewById(R.id.edit_product_count); convertView.setTag(childViewHolder); }else { childViewHolder = (ChildViewHolder) convertView.getTag(); } if(mCartEntityList.get(groupPosition).isGroupEdit()){ childViewHolder.viewEdit.setVisibility(View.VISIBLE); childViewHolder.viewComplete.setVisibility(View.INVISIBLE); }else { childViewHolder.viewEdit.setVisibility(View.INVISIBLE); childViewHolder.viewComplete.setVisibility(View.VISIBLE); } childViewHolder.checkBox.setChecked(mCartEntityList.get(groupPosition).getProductEntityList().get(childPosition).isChildCheck()); childViewHolder.productPrice.setText("¥" + String.format("%.2f", mCartEntityList.get(groupPosition).getProductEntityList().get(childPosition).getProductPrice())); childViewHolder.editProdcutCount.setText(mCartEntityList.get(groupPosition).getProductEntityList().get(childPosition).getProductCount()+""); childViewHolder.productCount.setText("x"+mCartEntityList.get(groupPosition).getProductEntityList().get(childPosition).getProductCount()); childViewHolder.checkBox.setTag(R.integer.view_group_tag, groupPosition); childViewHolder.checkBox.setTag(R.integer.view_child_tag, childPosition); childViewHolder.checkBox.setOnClickListener(this); childViewHolder.childDeleteButton.setTag(R.integer.view_group_tag, groupPosition); childViewHolder.childDeleteButton.setTag(R.integer.view_child_tag, childPosition); childViewHolder.childDeleteButton.setOnClickListener(this); childViewHolder.increaseBtn.setTag(R.integer.view_group_tag, groupPosition); childViewHolder.increaseBtn.setTag(R.integer.view_child_tag, childPosition); childViewHolder.increaseBtn.setOnClickListener(this); childViewHolder.reduceBtn.setTag(R.integer.view_group_tag, groupPosition); childViewHolder.reduceBtn.setTag(R.integer.view_child_tag, childPosition); childViewHolder.reduceBtn.setOnClickListener(this); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } @Override public void onClick(View v) { if(null == mCartListener) return; switch (v.getId()){ case R.id.group_checkBox: mCartListener.groupCheck((int) v.getTag(R.integer.view_group_tag)); break; case R.id.child_checkBox: mCartListener.childCheck((int) v.getTag(R.integer.view_group_tag), (int) v.getTag(R.integer.view_child_tag)); break; case R.id.shop_layout: mCartListener.groupClick(); break; case R.id.group_edit: mCartListener.groupEdit((int) v.getTag(R.integer.view_group_tag)); break; case R.id.child_delete: mCartListener.childDelete((int)v.getTag(R.integer.view_group_tag),(int)v.getTag(R.integer.view_child_tag)); break; case R.id.btn_increase: mCartListener.childIncrease((int)v.getTag(R.integer.view_group_tag),(int)v.getTag(R.integer.view_child_tag)); break; case R.id.btn_reduce: mCartListener.childReduce((int) v.getTag(R.integer.view_group_tag), (int) v.getTag(R.integer.view_child_tag)); break; } } private static class GroupViewHolder{ CheckBox checkBox; ImageView shopIcon; TextView shopName; TextView groupEdit; View shoplayout; View groupDividerView; } private static class ChildViewHolder{ CheckBox checkBox; ImageView productIcon; TextView productName; TextView productInfo; TextView productPrice; TextView productCount; TextView childDeleteButton; View viewEdit; View viewComplete; ImageView increaseBtn; ImageView reduceBtn; TextView editProdcutCount; } }
下载地址:https://github.com/piratecode/TaoBaoCart
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/38353.html