第一步:添加购物车图片
第二步:在Values下的Strings中添加
<string name="good_count">x %d</string>
<string name="price">¥%s</string>
<string name="total_0">合计:¥0.00</string>
<string name="total">合计:¥%s</string>
<string name="jiesuan_0">结算(0)</string>
<string name="jiesuan">结算(%s)</string>
<string name="cart_title_0">购物车(0)</string>
<string name="cart_title">购物车(%s)</string>
第三步:在drawable中新建selector_check_bg.xml文件
<item android:drawable="@mipmap/ic_checked" android:state_checked="true"></item>
<item android:drawable="@mipmap/ic_uncheck"></item>
第四步:1.在layout中activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.exbway.gouwuche.MainActivity">
<include layout="@layout/header_layout"
/>
<RelativeLayout
android:id="@+id/id_rl_cart_is_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FCFCFC"
android:gravity="center"
android:layout_marginTop="48dp"
android:visibility="gone">
<ImageView
android:id="@+id/id_rl_cart_is_empty_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@mipmap/ic_shopping_cart_empty"/>
<TextView
android:id="@+id/id_rl_cart_is_empty_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/id_rl_cart_is_empty_image"
android:layout_marginTop="10dp"
android:text="购物车为空"/>
</RelativeLayout>
<ExpandableListView
android:id="@+id/id_elv_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#ebebeb"
android:groupIndicator="@null"
android:dividerHeight="2dp"
android:footerDividersEnabled="false"
android:visibility="visible"
>
</ExpandableListView>
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="@layout/foot_layout"
/>
</RelativeLayout>
2.header_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/id_rl_head"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/id_iv_back"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:layout_marginTop="2dp"
android:layout_marginBottom="5dp"
android:src="@mipmap/ic_back" />
<TextView
android:id="@+id/id_tv_cart_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="48dp"
android:text="购物车"
android:textColor="#1a1a1a"
android:textSize="16dp" />
<TextView
android:id="@+id/id_tv_edit_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:minHeight="48dp"
android:text="编辑"
android:textColor="#1a1a1a"
android:textSize="16dp"
android:visibility="visible" />
</RelativeLayout>
3.foot_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="@+id/id_rl_foot"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_alignParentTop="true"
android:background="#ccc"/>
<CheckBox
android:id="@+id/id_cb_select_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@null"
android:text=" 全选"
android:button="@drawable/selector_check_bg"/>
<LinearLayout
android:id="@+id/id_ll_normal_all_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:visibility="visible"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/id_tv_totalPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:gravity="right"
android:text="@string/total_0"
android:textColor="#ff2600"
android:textSize="13sp"/>
<TextView
android:id="@+id/tvPostPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:gravity="right"
android:text="不含运费"
android:textColor="#666666"
android:textSize="11sp"/>
</LinearLayout>
<TextView
android:id="@+id/id_tv_totalCount_jiesuan"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#ff5000"
android:gravity="center_vertical"
android:paddingBottom="5dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="5dp"
android:text="@string/jiesuan_0"
android:textColor="#FFF"
android:textSize="16dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/id_ll_editing_all_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone"
android:layout_alignParentRight="true"
>
<TextView
android:id="@+id/id_tv_save_star_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="移到收藏夹"
android:background="@android:color/holo_orange_light"
android:paddingBottom="15dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="15dp"
android:textSize="16dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:textColor="#ffffff"
/>
<TextView
android:id="@+id/id_tv_delete_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff5000"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="15dp"
android:layout_gravity="center_vertical"
android:textSize="16dp"
android:text="删除"
android:textColor="#ffffff"
/>
</LinearLayout>
</RelativeLayout>
4.child_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/id_cb_select_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@null"
android:focusable="false"
android:button="@drawable/selector_check_bg"/>
<ImageView
android:id="@+id/id_iv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/id_cb_select_child"
android:padding="10dp"
android:src="@mipmap/ic_launcher"
/>
<LinearLayout
android:id="@+id/id_ll_normal"
android:layout_width="match_parent"
android:layout_toRightOf="@id/id_iv_logo"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible"
>
<TextView
android:id="@+id/tv_items_child"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商品名称"
>
</TextView>
<TextView
android:id="@+id/tv_items_child_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="颜色规格描述"
>
</TextView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
>
<TextView
android:id="@+id/id_tv_discount_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:singleLine="true"
android:text="@string/price"
android:textColor="@android:color/holo_red_light"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="@+id/id_tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="13dp"
android:layout_toRightOf="@+id/id_tv_discount_price"
android:text="@string/price"
android:textSize="10dp"
/>
<TextView
android:id="@+id/id_tv_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:layout_alignParentRight="true"
android:text="@string/good_count"
android:textSize="12dp"
/>
</RelativeLayout>
</LinearLayout>
<!-- android:visibility="gone"-->
<LinearLayout
android:id="@+id/id_ll_edtoring"
android:layout_toRightOf="@id/id_iv_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/ll_change_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/id_iv_reduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_reduce"
android:padding="10dp"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="@+id/id_tv_count_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:padding="10dp"
android:singleLine="true"
android:text="1"
android:textSize="14dp" />
<ImageView
android:id="@+id/id_iv_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@mipmap/ic_add"
android:padding="10dp"
/>
</LinearLayout>
<TextView
android:id="@+id/id_tv_price_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:text="@string/price"
android:textColor="@android:color/holo_red_light"
android:textSize="14dp"
android:padding="10dp"
/>
<TextView
android:id="@+id/id_tv_des_now"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="@id/ll_change_num"
android:text="颜色规格描述选择"
android:paddingLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_gravity="left"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:id="@+id/id_tv_goods_star"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:background="@android:color/holo_orange_light"
android:text="收藏"
android:textColor="@android:color/white"
android:padding="13dp"
android:gravity="center"
android:layout_marginRight="2dp"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:id="@+id/id_tv_goods_delete"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#ff5000"
android:text="删除"
android:textColor="@android:color/white"
android:padding="13dp"
android:gravity="center"
android:layout_gravity="center"
/>
</LinearLayout>
</RelativeLayout>
5.parent_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="@+id/id_ll_parent"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/id_cb_select_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@null"
android:focusable="false"
android:button="@drawable/selector_check_bg"/>
<TextView
android:id="@+id/tv_title_parent"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_toRightOf="@id/id_cb_select_parent"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/shop_icon"
android:drawableRight="@mipmap/ic_right_icon"
android:drawablePadding="10dp"/>
<TextView
android:id="@+id/id_tv_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编辑"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:padding="5dp"
android:layout_centerVertical="true"
/>
</RelativeLayout>
第五步:在Main中新建两个包,Adapter和Bean
然后CartBean
public class CartBean {
/** 商品ID */
private String goodsid;
/** 店铺ID */
private String storeid;
}
GoodsBean
package com.exbway.gouwuche.Bean;
/**
* Created by 张政浩 on 2017/10/18.
*/
public class GoodsBean {
public static final int STATUS_INVALID=0;
public static final int STATUS_VALID=1;
//===============================================
private String id;
private String name;
/** 商品宣传图片 */
private String imageLogo;
/** 商品规格 */
private String desc;
/** 原价,市场价 */
private double price;
/** 现价,折扣价 */
private double discountPrice;
private int count;
/** 状态 */
private int status;
/** 是否被选中 */
private boolean isChecked;
/** 是否是编辑状态 */
private boolean isEditing;
public GoodsBean(String id, String name, String imageLogo, String desc, double price, double discountPrice, int count, int status, boolean isChecked, boolean isEditing) {
this.id = id;
this.name = name;
this.imageLogo = imageLogo;
this.desc = desc;
this.price = price;
this.discountPrice = discountPrice;
this.count = count;
this.status = status;
this.isChecked = isChecked;
this.isEditing = isEditing;
}
public GoodsBean(){
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImageLogo() {
return imageLogo;
}
public void setImageLogo(String imageLogo) {
this.imageLogo = imageLogo;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getDiscountPrice() {
return discountPrice;
}
public void setDiscountPrice(double discountPrice) {
this.discountPrice = discountPrice;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public boolean isChecked() {
return isChecked;
}
public void setIsChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public boolean isEditing() {
return isEditing;
}
public void setIsEditing(boolean isEditing) {
this.isEditing = isEditing;
}
}
StoreBean
package com.exbway.gouwuche.Bean;
/**
* Created by 张政浩 on 2017/10/18.
*/
public class StoreBean {
/** 店铺ID */
private String id;
/** 店铺名称 */
private String name;
private boolean isChecked;
private boolean isEditing;
public StoreBean(String id, String name,boolean isChecked,boolean isEditing) {
this.id = id;
this.name = name;
this.isChecked = isChecked;
this.isEditing = isEditing;
}
public StoreBean() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public boolean isEditing() {
return isEditing;
}
public void setIsEditing(boolean isEditing) {
this.isEditing = isEditing;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChecked() {
return isChecked;
}
public void setIsChecked(boolean isChecked) {
this.isChecked = isChecked;
}
}
MyBaseExpandableListAdapter
package com.exbway.gouwuche.Adapter;
import android.util.Log;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.exbway.gouwuche.Bean.GoodsBean;
import com.exbway.gouwuche.Bean.StoreBean;
import com.exbway.gouwuche.R;
import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.util.List;
import java.util.Map;
import java.util.Map;
/**
* Created by 张政浩 on 2017/10/18.
*/
public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {
private static final String TAG = "MyBaseEtAdapter";
List<Map<String, Object>> parentMapList;
List<List<Map<String, Object>>> childMapList_list;
Context context;
int totalCount = 0;
double totalPrice = 0.00;
public static final String EDITING = "编辑";
public static final String FINISH_EDITING = "完成";
OnAllCheckedBoxNeedChangeListener onAllCheckedBoxNeedChangeListener;
OnGoodsCheckedChangeListener onGoodsCheckedChangeListener;
OnCheckHasGoodsListener onCheckHasGoodsListener;
public void setOnCheckHasGoodsListener(OnCheckHasGoodsListener onCheckHasGoodsListener) {
this.onCheckHasGoodsListener = onCheckHasGoodsListener;
}
public void setOnEditingTvChangeListener(OnEditingTvChangeListener onEditingTvChangeListener) {
this.onEditingTvChangeListener = onEditingTvChangeListener;
}
OnEditingTvChangeListener onEditingTvChangeListener;
public void setOnGoodsCheckedChangeListener(OnGoodsCheckedChangeListener onGoodsCheckedChangeListener) {
this.onGoodsCheckedChangeListener = onGoodsCheckedChangeListener;
}
public void setOnAllCheckedBoxNeedChangeListener(OnAllCheckedBoxNeedChangeListener onAllCheckedBoxNeedChangeListener) {
this.onAllCheckedBoxNeedChangeListener = onAllCheckedBoxNeedChangeListener;
}
public MyBaseExpandableListAdapter(Context context, List<Map<String, Object>> parentMapList, List<List<Map<String, Object>>> childMapList_list) {
this.parentMapList = parentMapList;
this.childMapList_list = childMapList_list;
this.context = context;
}
//获取当前父item的数据数量
@Override
public int getGroupCount() {
return parentMapList.size();
}
//获取当前父item下的子item的个数
@Override
public int getChildrenCount(int groupPosition) {
return childMapList_list.get(groupPosition).size();
}
//获取当前父item的数据
@Override
public Object getGroup(int groupPosition) {
return parentMapList.get(groupPosition);
}
//得到子item需要关联的数据
@Override
public Object getChild(int groupPosition, int childPosition) {
return childMapList_list.get(groupPosition).get(childPosition);
}
//得到父item的ID
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//得到子item的ID
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
//return false;
return true;
}
//设置父item组件
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.parent_layout, null);
groupViewHolder = new GroupViewHolder();
groupViewHolder.tv_title_parent = (TextView) convertView
.findViewById(R.id.tv_title_parent);
groupViewHolder.id_tv_edit = (TextView) convertView
.findViewById(R.id.id_tv_edit);
groupViewHolder.id_cb_select_parent = (CheckBox) convertView
.findViewById(R.id.id_cb_select_parent);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
StoreBean storeBean = (StoreBean) parentMapList.get(groupPosition).get("parentName");
final String parentName = storeBean.getName();
groupViewHolder.tv_title_parent.setText(parentName);
if (storeBean.isEditing()) {
groupViewHolder.id_tv_edit.setText(FINISH_EDITING);
} else {
groupViewHolder.id_tv_edit.setText(EDITING);
}
groupViewHolder.id_tv_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = "";
TextView textView = null;
if (v instanceof TextView) {
textView = (TextView) v;
}
// Toast.makeText(context, "编辑:" + groupPosition, Toast.LENGTH_SHORT).show();
textView.setText(text);
setupEditing(groupPosition);
onEditingTvChangeListener.onEditingTvChange(dealAllEditingIsEditing());
}
});
//覆盖原有收起展开事件
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "店铺:" + parentName, Toast.LENGTH_SHORT).show();
}
});
groupViewHolder.id_cb_select_parent.setChecked(storeBean.isChecked());
final boolean nowBeanChecked = storeBean.isChecked();
groupViewHolder.id_cb_select_parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setupOneParentAllChildChecked(!nowBeanChecked, groupPosition);
//控制总checkedbox 接口
onAllCheckedBoxNeedChangeListener.onCheckedBoxNeedChange(dealAllParentIsChecked());
}
});
return convertView;
}
//设置子item的组件
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.child_layout, null);
childViewHolder = new ChildViewHolder();
childViewHolder.tv_items_child = (TextView) convertView
.findViewById(R.id.tv_items_child);
childViewHolder.id_cb_select_child = (CheckBox) convertView
.findViewById(R.id.id_cb_select_child);
childViewHolder.id_ll_normal = (LinearLayout) convertView
.findViewById(R.id.id_ll_normal);
childViewHolder.id_ll_edtoring = (LinearLayout) convertView
.findViewById(R.id.id_ll_edtoring);
//常规下:
childViewHolder.tv_items_child_desc = (TextView) convertView
.findViewById(R.id.tv_items_child_desc);
childViewHolder.id_tv_price = (TextView) convertView
.findViewById(R.id.id_tv_price);
childViewHolder.id_tv_discount_price = (TextView) convertView
.findViewById(R.id.id_tv_discount_price);
childViewHolder.id_tv_count = (TextView) convertView
.findViewById(R.id.id_tv_count);
//编辑下:
childViewHolder.id_iv_reduce = (ImageView) convertView
.findViewById(R.id.id_iv_reduce);
childViewHolder.id_iv_add = (ImageView) convertView
.findViewById(R.id.id_iv_add);
childViewHolder.id_tv_count_now = (TextView) convertView
.findViewById(R.id.id_tv_count_now);
childViewHolder.id_tv_price_now = (TextView) convertView
.findViewById(R.id.id_tv_price_now);
childViewHolder.id_tv_des_now = (TextView) convertView
.findViewById(R.id.id_tv_des_now);
childViewHolder.id_tv_goods_star = (TextView) convertView
.findViewById(R.id.id_tv_goods_star);
childViewHolder.id_tv_goods_delete = (TextView) convertView
.findViewById(R.id.id_tv_goods_delete);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
final GoodsBean goodsBean = (GoodsBean) childMapList_list.get(groupPosition).get(childPosition).get("childName");
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "商品:" + goodsBean.getName(), Toast.LENGTH_SHORT).show();
}
});
childViewHolder.tv_items_child.setText(goodsBean.getName());
childViewHolder.id_tv_price.setText(String.format(context.getString(R.string.price), goodsBean.getPrice()));
// childViewHolder.id_tv_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//数字划线效果
childViewHolder.id_tv_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 设置中划线并抗锯齿
childViewHolder.id_tv_discount_price.setText(String.format(context.getString(R.string.price), goodsBean.getDiscountPrice()));
childViewHolder.tv_items_child_desc.setText(String.valueOf(goodsBean.getDesc()));
childViewHolder.id_tv_count.setText(String.format(context.getString(R.string.good_count), goodsBean.getCount()));
childViewHolder.id_tv_count_now.setText(String.valueOf(goodsBean.getCount()));
double priceNow = goodsBean.getCount() * goodsBean.getDiscountPrice();//小结
childViewHolder.id_tv_price_now.setText(String.format(context.getString(R.string.price), priceNow));
childViewHolder.id_tv_des_now.setText(goodsBean.getDesc());
childViewHolder.id_cb_select_child.setChecked(goodsBean.isChecked());
childViewHolder.id_cb_select_child.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final boolean nowBeanChecked = goodsBean.isChecked();
//更新数据
goodsBean.setIsChecked(!nowBeanChecked);
boolean isOneParentAllChildIsChecked = dealOneParentAllChildIsChecked(groupPosition);
Log.d(TAG, "getChildView:onClick: ==============");
Log.d(TAG, "getChildView:onClick:isOneParentAllChildIsChecked:" + isOneParentAllChildIsChecked);
StoreBean storeBean = (StoreBean) parentMapList.get(groupPosition).get("parentName");
storeBean.setIsChecked(isOneParentAllChildIsChecked);
notifyDataSetChanged();
//控制总checkedbox 接口
onAllCheckedBoxNeedChangeListener.onCheckedBoxNeedChange(dealAllParentIsChecked());
dealPrice();
}
});
if (goodsBean.isEditing()) {
childViewHolder.id_ll_normal.setVisibility(View.GONE);
childViewHolder.id_ll_edtoring.setVisibility(View.VISIBLE);
} else {
childViewHolder.id_ll_normal.setVisibility(View.VISIBLE);
childViewHolder.id_ll_edtoring.setVisibility(View.GONE);
}
childViewHolder.id_iv_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TextView textView= (TextView) v.getRootView().findViewById(R.id.id_tv_num);
dealAdd(goodsBean);
}
});
childViewHolder.id_iv_reduce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TextView textView= (TextView) v.getRootView().findViewById(R.id.id_tv_num);
dealReduce(goodsBean);
}
});
childViewHolder.id_tv_goods_star.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "收藏商品:" + goodsBean.getName(), Toast.LENGTH_SHORT).show();
}
});
childViewHolder.id_tv_goods_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(context, "删除商品:" + goodsBean.getName(), Toast.LENGTH_SHORT).show();
removeOneGood(groupPosition, childPosition);
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// return false;
return true;
}
//供全选按钮调用
public void setupAllChecked(boolean isChecked) {
Log.d(TAG, "setupAllChecked: ============");
Log.d(TAG, "setupAllChecked: isChecked:" + isChecked);
for (int i = 0; i < parentMapList.size(); i++) {
StoreBean storeBean = (StoreBean) parentMapList.get(i).get("parentName");
storeBean.setIsChecked(isChecked);
List<Map<String, Object>> childMapList = childMapList_list.get(i);
for (int j = 0; j < childMapList.size(); j++) {
GoodsBean goodsBean = (GoodsBean) childMapList.get(j).get("childName");
goodsBean.setIsChecked(isChecked);
}
}
notifyDataSetChanged();
dealPrice();
}
private void setupOneParentAllChildChecked(boolean isChecked, int groupPosition) {
Log.d(TAG, "setupOneParentAllChildChecked: ============");
Log.d(TAG, "setupOneParentAllChildChecked: groupPosition:" + groupPosition);
Log.d(TAG, "setupOneParentAllChildChecked: isChecked:" + isChecked);
StoreBean storeBean = (StoreBean) parentMapList.get(groupPosition).get("parentName");
storeBean.setIsChecked(isChecked);
List<Map<String, Object>> childMapList = childMapList_list.get(groupPosition);
for (int j = 0; j < childMapList.size(); j++) {
GoodsBean goodsBean = (GoodsBean) childMapList.get(j).get("childName");
goodsBean.setIsChecked(isChecked);
}
notifyDataSetChanged();
dealPrice();
}
public boolean dealOneParentAllChildIsChecked(int groupPosition) {
Log.d(TAG, "dealOneParentAllChildIsChecked: ============");
Log.d(TAG, "dealOneParentAllChildIsChecked: groupPosition:" + groupPosition);
// StoreBean storeBean= (StoreBean) parentMapList.get(groupPosition).get("parentName");
List<Map<String, Object>> childMapList = childMapList_list.get(groupPosition);
for (int j = 0; j < childMapList.size(); j++) {
GoodsBean goodsBean = (GoodsBean) childMapList.get(j).get("childName");
if (!goodsBean.isChecked()) {
return false;//如果有一个没选择 就false
}
}
return true;
}
public boolean dealAllParentIsChecked() {
Log.d(TAG, "dealAllParentIsChecked: ============");
for (int i = 0; i < parentMapList.size(); i++) {
StoreBean storeBean = (StoreBean) parentMapList.get(i).get("parentName");
if (!storeBean.isChecked()) {
return false;//如果有一个没选择 就false
}
}
return true;
}
public void dealPrice() {
// showList();
totalCount = 0;
totalPrice = 0.00;
for (int i = 0; i < parentMapList.size(); i++) {
//StoreBean storeBean= (StoreBean) parentMapList.get(i).get("parentName");
List<Map<String, Object>> childMapList = childMapList_list.get(i);
for (int j = 0; j < childMapList.size(); j++) {
GoodsBean goodsBean = (GoodsBean) childMapList.get(j).get("childName");
int count = goodsBean.getCount();
double discountPrice = goodsBean.getDiscountPrice();
if (goodsBean.isChecked()) {
totalCount++;//单品多数量只记1
totalPrice += discountPrice * count;
}
}
}
//计算回调
onGoodsCheckedChangeListener.onGoodsCheckedChange(totalCount, totalPrice);
}
//供总编辑按钮调用
public void setupEditingAll(boolean isEditingAll) {
for (int i = 0; i < parentMapList.size(); i++) {
StoreBean storeBean = (StoreBean) parentMapList.get(i).get("parentName");
storeBean.setIsEditing(isEditingAll);
List<Map<String, Object>> childMapList = childMapList_list.get(i);
for (int j = 0; j < childMapList.size(); j++) {
GoodsBean goodsBean = (GoodsBean) childMapList.get(j).get("childName");
goodsBean.setIsEditing(isEditingAll);
}
}
notifyDataSetChanged();
}
public boolean dealAllEditingIsEditing() {
for (int i = 0; i < parentMapList.size(); i++) {
StoreBean storeBean = (StoreBean) parentMapList.get(i).get("parentName");
if (storeBean.isEditing()) {//!!!
return true;//如果有一个是编辑状态 就true
}
}
return false;
}
public void setupEditing(int groupPosition) {
StoreBean storeBean = (StoreBean) parentMapList.get(groupPosition).get("parentName");
boolean isEditing = !storeBean.isEditing();
storeBean.setIsEditing(isEditing);
List<Map<String, Object>> childMapList = childMapList_list.get(groupPosition);
for (int j = 0; j < childMapList.size(); j++) {
GoodsBean goodsBean = (GoodsBean) childMapList.get(j).get("childName");
goodsBean.setIsEditing(isEditing);
}
notifyDataSetChanged();
}
public void dealAdd(GoodsBean goodsBean) {
int count = goodsBean.getCount();
count++;
goodsBean.setCount(count);
// textView.setText(String.valueOf(count));
notifyDataSetChanged();
dealPrice();
}
public void dealReduce(GoodsBean goodsBean) {
int count = goodsBean.getCount();
if (count == 1) {
return;
}
count--;
goodsBean.setCount(count);
// textView.setText(String.valueOf(count));
notifyDataSetChanged();
dealPrice();
}
public void removeOneGood(int groupPosition, int childPosition) {
//StoreBean storeBean = (StoreBean) parentMapList.get(groupPosition).get("parentName");
List<Map<String, Object>> childMapList = childMapList_list.get(groupPosition);
// GoodsBean goodsBean = (GoodsBean) childMapList.get(childPosition).get("childName");
childMapList.remove(childPosition);
//通过子项
if (childMapList != null && childMapList.size() > 0) {
} else {
parentMapList.remove(groupPosition);
childMapList_list.remove(groupPosition);//!!!!因为parentMapList和childMapList_list是pos关联的 得保持一致
}
if (parentMapList != null && parentMapList.size() > 0) {
onCheckHasGoodsListener.onCheckHasGoods(true);//
} else {
onCheckHasGoodsListener.onCheckHasGoods(false);//
}
notifyDataSetChanged();
dealPrice();
}
public void removeGoods() {
for (int i = parentMapList.size() - 1; i >= 0; i--) {//倒过来遍历 remove
StoreBean storeBean = (StoreBean) parentMapList.get(i).get("parentName");
if (storeBean.isChecked()) {
parentMapList.remove(i);
childMapList_list.remove(i);
} else {
List<Map<String, Object>> childMapList = childMapList_list.get(i);
for (int j = childMapList.size() - 1; j >= 0; j--) {//倒过来遍历 remove
GoodsBean goodsBean = (GoodsBean) childMapList.get(j).get("childName");
if (goodsBean.isChecked()) {
childMapList.remove(j);
}
}
}
}
if (parentMapList != null && parentMapList.size() > 0) {
onCheckHasGoodsListener.onCheckHasGoods(true);//
} else {
onCheckHasGoodsListener.onCheckHasGoods(false);//
}
notifyDataSetChanged();//
dealPrice();//重新计算
}
private void resetViewAfterDelete() {
for (int i = 0; i < parentMapList.size(); i++) {
StoreBean storeBean = (StoreBean) parentMapList.get(i).get("parentName");
storeBean.setIsChecked(false);
storeBean.setIsEditing(false);
List<Map<String, Object>> childMapList = childMapList_list.get(i);
for (int j = 0; j < childMapList.size(); j++) {
GoodsBean goodsBean = (GoodsBean) childMapList.get(j).get("childName");
goodsBean.setIsChecked(false);
goodsBean.setIsEditing(false);
}
}
}
void showList(String tempStr) {
Log.d(TAG, "showList:" + tempStr);
for (int i = 0; i < parentMapList.size(); i++) {
StoreBean storeBean = (StoreBean) parentMapList.get(i).get("parentName");
Log.d(TAG, "showList: parentName:【" + storeBean.getName() + "】isChecked:" + storeBean.isChecked());
List<Map<String, Object>> childMapList = childMapList_list.get(i);
for (int j = 0; j < childMapList.size(); j++) {
GoodsBean goodsBean = (GoodsBean) childMapList.get(j).get("childName");
Log.d(TAG, "showList: childName:" + goodsBean.getName() + "isChecked:" + goodsBean.isChecked());
}
}
}
public interface OnAllCheckedBoxNeedChangeListener {
void onCheckedBoxNeedChange(boolean allParentIsChecked);
}
public interface OnEditingTvChangeListener {
void onEditingTvChange(boolean allIsEditing);
}
public interface OnGoodsCheckedChangeListener {
void onGoodsCheckedChange(int totalCount, double totalPrice);
}
public interface OnCheckHasGoodsListener {
void onCheckHasGoods(boolean isHasGoods);
}
class GroupViewHolder {
TextView tv_title_parent;
TextView id_tv_edit;
CheckBox id_cb_select_parent;
}
class ChildViewHolder {
TextView tv_items_child;
CheckBox id_cb_select_child;
LinearLayout id_ll_normal;
LinearLayout id_ll_edtoring;
TextView tv_items_child_desc;
TextView id_tv_price;
TextView id_tv_discount_price;
TextView id_tv_count;
ImageView id_iv_reduce;
ImageView id_iv_add;
TextView id_tv_des_now;
TextView id_tv_count_now;
TextView id_tv_price_now;
TextView id_tv_goods_star;
TextView id_tv_goods_delete;
}
}
最后一个MainActivity
package com.exbway.gouwuche;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.exbway.gouwuche.Adapter.MyBaseExpandableListAdapter;
import com.exbway.gouwuche.Bean.GoodsBean;
import com.exbway.gouwuche.Bean.StoreBean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
//定义父列表项List数据集合
List<Map<String, Object>> parentMapList = new ArrayList<Map<String, Object>>();
//定义子列表项List数据集合
List<List<Map<String, Object>>> childMapList_list = new ArrayList<List<Map<String, Object>>>();
MyBaseExpandableListAdapter myBaseExpandableListAdapter;
CheckBox id_cb_select_all;
LinearLayout id_ll_normal_all_state;
LinearLayout id_ll_editing_all_state;
ExpandableListView expandableListView;
RelativeLayout id_rl_cart_is_empty;
RelativeLayout id_rl_foot;
TextView id_tv_edit_all;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initCartData();
expandableListView = (ExpandableListView) findViewById(R.id.id_elv_listview);
myBaseExpandableListAdapter = new MyBaseExpandableListAdapter(this, parentMapList, childMapList_list);
expandableListView.setAdapter(myBaseExpandableListAdapter);
expandableListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "click:" + position, Toast.LENGTH_SHORT).show();
}
});
for (int i = 0; i < parentMapList.size(); i++) {
expandableListView.expandGroup(i);
}
ImageView id_iv_back = (ImageView) findViewById(R.id.id_iv_back);
id_iv_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "click :back", Toast.LENGTH_SHORT).show();
}
});
id_ll_normal_all_state = (LinearLayout) findViewById(R.id.id_ll_normal_all_state);
id_ll_editing_all_state = (LinearLayout) findViewById(R.id.id_ll_editing_all_state);
id_rl_cart_is_empty = (RelativeLayout) findViewById(R.id.id_rl_cart_is_empty);
TextView id_tv_save_star_all = (TextView) findViewById(R.id.id_tv_save_star_all);
id_tv_save_star_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "收藏多选商品", Toast.LENGTH_SHORT).show();
}
});
TextView id_tv_delete_all = (TextView) findViewById(R.id.id_tv_delete_all);
id_tv_delete_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myBaseExpandableListAdapter.removeGoods();
// Toast.makeText(MainActivity.this, "删除多选商品", Toast.LENGTH_SHORT).show();
}
});
id_tv_edit_all = (TextView) findViewById(R.id.id_tv_edit_all);
id_tv_edit_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v instanceof TextView) {
TextView tv = (TextView) v;
if (MyBaseExpandableListAdapter.EDITING.equals(tv.getText())) {
myBaseExpandableListAdapter.setupEditingAll(true);
tv.setText(MyBaseExpandableListAdapter.FINISH_EDITING);
changeFootShowDeleteView(true);//这边类似的功能 后期待使用观察者模式
} else {
myBaseExpandableListAdapter.setupEditingAll(false);
tv.setText(MyBaseExpandableListAdapter.EDITING);
changeFootShowDeleteView(false);//这边类似的功能 后期待使用观察者模式
}
}
}
});
id_cb_select_all = (CheckBox) findViewById(R.id.id_cb_select_all);
/* 要么遍历判断再选择 id_cb_select_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Toast.makeText(MainActivity.this, "all isChecked:" + isChecked, Toast.LENGTH_SHORT).show();
myBaseExpandableListAdapter.setupAllChecked(isChecked);
}
});*/
id_cb_select_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v instanceof CheckBox) {
CheckBox checkBox = (CheckBox) v;
myBaseExpandableListAdapter.setupAllChecked(checkBox.isChecked());
}
}
});
final TextView id_tv_totalPrice = (TextView) findViewById(R.id.id_tv_totalPrice);
final TextView id_tv_totalCount_jiesuan = (TextView) findViewById(R.id.id_tv_totalCount_jiesuan);
id_tv_totalCount_jiesuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "click:结算", Toast.LENGTH_SHORT).show();
}
});
myBaseExpandableListAdapter.setOnGoodsCheckedChangeListener(new MyBaseExpandableListAdapter.OnGoodsCheckedChangeListener() {
@Override
public void onGoodsCheckedChange(int totalCount, double totalPrice) {
id_tv_totalPrice.setText(String.format(getString(R.string.total), totalPrice));
id_tv_totalCount_jiesuan.setText(String.format(getString(R.string.jiesuan), totalCount));
}
});
myBaseExpandableListAdapter.setOnAllCheckedBoxNeedChangeListener(new MyBaseExpandableListAdapter.OnAllCheckedBoxNeedChangeListener() {
@Override
public void onCheckedBoxNeedChange(boolean allParentIsChecked) {
id_cb_select_all.setChecked(allParentIsChecked);
}
});
myBaseExpandableListAdapter.setOnEditingTvChangeListener(new MyBaseExpandableListAdapter.OnEditingTvChangeListener() {
@Override
public void onEditingTvChange(boolean allIsEditing) {
changeFootShowDeleteView(allIsEditing);//这边类似的功能 后期待使用观察者模式
}
});
myBaseExpandableListAdapter.setOnCheckHasGoodsListener(new MyBaseExpandableListAdapter.OnCheckHasGoodsListener() {
@Override
public void onCheckHasGoods(boolean isHasGoods) {
setupViewsShow(isHasGoods);
}
});
/**====include进来方式可能会导致view覆盖listview的最后一个item 解决*/
//在onCreate方法中一般没办法直接调用view.getHeight方法来获取到控件的高度
id_rl_foot = (RelativeLayout) findViewById(R.id.id_rl_foot);
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
id_rl_foot.measure(w, h);
int r_width = id_rl_foot.getMeasuredWidth();
int r_height = id_rl_foot.getMeasuredHeight();
Log.i("MeasureSpec", "MeasureSpec r_width = " + r_width);
Log.i("MeasureSpec", "MeasureSpec r_height = " + r_height);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int top = dp2px(this, 48);
lp.setMargins(0, top, 0, r_height);
expandableListView.setLayoutParams(lp);
if (parentMapList != null && parentMapList.size() > 0) {
setupViewsShow(true);
} else {
setupViewsShow(false);
}
}
private void setupViewsShow(boolean isHasGoods) {
if (isHasGoods) {
expandableListView.setVisibility(View.VISIBLE);
id_rl_cart_is_empty.setVisibility(View.GONE);
id_rl_foot.setVisibility(View.VISIBLE);
id_tv_edit_all.setVisibility(View.VISIBLE);
} else {
expandableListView.setVisibility(View.GONE);
id_rl_cart_is_empty.setVisibility(View.VISIBLE);
id_rl_foot.setVisibility(View.GONE);
id_tv_edit_all.setVisibility(View.GONE);
}
}
public void changeFootShowDeleteView(boolean showDeleteView) {
if (showDeleteView) {
id_tv_edit_all.setText(MyBaseExpandableListAdapter.FINISH_EDITING);
id_ll_normal_all_state.setVisibility(View.INVISIBLE);
id_ll_editing_all_state.setVisibility(View.VISIBLE);
} else {
id_tv_edit_all.setText(MyBaseExpandableListAdapter.EDITING);
id_ll_normal_all_state.setVisibility(View.VISIBLE);
id_ll_editing_all_state.setVisibility(View.INVISIBLE);
}
}
public int dp2px(Context context, float dp) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
private void initCartData() {
for (int i = 0; i < 4; i++) {
String store="旗舰店";
if(i%2==0){
store="专营店";
}
//提供父列表的数据
Map<String, Object> parentMap = new HashMap<String, Object>();
parentMap.put("parentName", new StoreBean("" + i, store + i, false, false));
/* if (i%2==0) {
parentMap.put("parentIcon", R.mipmap.ic_launcher);
}else
{
parentMap.put("parentIcon", R.mipmap.louisgeek);
}*/
parentMapList.add(parentMap);
//提供当前父列的子列数据
List<Map<String, Object>> childMapList = new ArrayList<Map<String, Object>>();
for (int j = 0; j < 3; j++) {
Map<String, Object> childMap = new HashMap<String, Object>();
GoodsBean goodsBean = new GoodsBean(i + "_" + j, store+i + "下的商品" + j, "url", "均码,红色", 150, 120, 1, GoodsBean.STATUS_VALID, false, false);
childMap.put("childName", goodsBean);
childMapList.add(childMap);
}
childMapList_list.add(childMapList);
}
}
private void initData() {
for (int i = 0; i < 15; i++) {
//提供父列表的数据
Map<String, Object> parentMap = new HashMap<String, Object>();
parentMap.put("parentName", "parentName" + i);
if (i % 2 == 0) {
parentMap.put("parentIcon", R.mipmap.ic_launcher);
} else {
parentMap.put("parentIcon", R.mipmap.louisgeek);
}
parentMapList.add(parentMap);
//提供当前父列的子列数据
List<Map<String, Object>> childMapList = new ArrayList<Map<String, Object>>();
for (int j = 0; j < 10; j++) {
Map<String, Object> childMap = new HashMap<String, Object>();
childMap.put("childName", "parentName" + i + "下面的childName" + j);
childMapList.add(childMap);
}
childMapList_list.add(childMapList);
}
}
}
OK,一个简单的购物车就出来啦。