上面一篇博客介绍了在写一个类似于淘宝购物车页面的时候,使用ExpandableListView和BaseExpandableListAdapter的时候遇到的一些问题,这篇博客就来记录一下仿淘宝购物车页面具体是如何实现的。
其实最主要的就是BaseExpandableListAdapter,activity中就只有一个ExpandableListView,和一个全选按钮,一个显示当前价格的TextView,在此就不贴出布局了,主要贴出adapter中是如何是实现的。
public class ShopBagExpandAdapter extends BaseExpandableListAdapter {
private ShopBagGoodsListModel shopBagGoodsListModel = new ShopBagGoodsListModel();
private Context context;
private static final int shopMinNum = 1;
private ShopAllSelectListener shopAllSelectListener;
private PriceChangeListener priceChangeListener;
private PriceCaculatorErrorListener priceCaculatorErrorListener;
private DeleteGoodsInfoListener deleteGoodsInfoListener;
private Map<String, ShopBagGoodsModel> selectGoodsMap = new HashMap<>();
public ShopBagExpandAdapter(Context context, ExpandableListView expandableListView) {
this.context = context;
this.expandableListView = expandableListView;
}
public void setData(ShopBagGoodsListModel shopBagGoodsListModel) {
this.shopBagGoodsListModel = shopBagGoodsListModel;
}
public void setIsSelectAll(boolean isSelectAll) {
for (ShopBagStoreModel shopBagStoreModel : shopBagGoodsListModel.getGoods_list()) {
shopBagStoreModel.setSelect(isSelectAll);
for (ShopBagGoodsModel shopBagGoodsModel : shopBagStoreModel.getGoods_list()) {
shopBagGoodsModel.setChecked(isSelectAll);
if (isSelectAll) {
selectGoodsMap.put(shopBagGoodsModel.getGoods_id(), shopBagGoodsModel);
}
}
}
if (!isSelectAll) {
selectGoodsMap.clear();
}
caculatorAllPrice();
notifyDataSetChanged();
}
@Override
public int getGroupCount() {
if (shopBagGoodsListModel != null && shopBagGoodsListModel.getGoods_list() != null) {
return shopBagGoodsListModel.getGoods_list().size();
}
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
return shopBagGoodsListModel.getGoods_list().get(groupPosition).getGoods_list().size();
}
@Override
public Object getGroup(int groupPosition) {
return shopBagGoodsListModel.getGoods_list().get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return shopBagGoodsListModel.getGoods_list().get(groupPosition).getGoods_list().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 true;
}
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
final GroupViewHolder groupViewHolder;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.adapter_shop_bag_store, null);
groupViewHolder = new GroupViewHolder();
groupViewHolder.storeSelect = view.findViewById(R.id.iv_adapter_shop_bag_store_select);
groupViewHolder.storeName = view.findViewById(R.id.tv_adapter_shop_bag_store_name);
view.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) view.getTag();
}
groupViewHolder.storeSelect.setChecked(shopBagGoodsListModel.getGoods_list().get(i).isSelect());
if (shopBagGoodsListModel.getGoods_list().get(i).isSelect()) {
for(ShopBagGoodsModel shopBagGoodsModel : shopBagGoodsListModel.getGoods_list().get(i).getGoods_list()){
shopBagGoodsModel.setChecked(true);
}
}
groupViewHolder.storeName.setText(shopBagGoodsListModel.getGoods_list().get(i).getRu_name());
groupViewHolder.storeName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean currentIsSelect = groupViewHolder.storeSelect.isChecked();
groupViewHolder.storeSelect.setChecked(!currentIsSelect);
shopBagGoodsListModel.getGoods_list().get(i).setSelect(!currentIsSelect);
setChildViewSelect(i, !currentIsSelect);
addStore(i,!currentIsSelect);
notifyDataSetChanged();
}
});
return view;
}
@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
final ChildViewHolder childViewHolder;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.adapter_shop_bag_goods, null);
childViewHolder = new ChildViewHolder();
childViewHolder.shopSelect = view.findViewById(R.id.iv_adapter_shop_bag_goods_select);
childViewHolder.shopImage = view.findViewById(R.id.iv_adapter_shop_bag_goods_product_img);
childViewHolder.shopName = view.findViewById(R.id.tv_adapter_shop_bag_goods_product_about);
childViewHolder.shopWeight = view.findViewById(R.id.tv_adapter_shop_bag_goods_product_weight);
childViewHolder.shopPrice = view.findViewById(R.id.tv_adapter_shop_bag_goods_product_price);
childViewHolder.shopLess = view.findViewById(R.id.tv_adapter_shop_bag_goods_minus);
childViewHolder.shopNum = view.findViewById(R.id.tv_adapter_shop_bag_goods_num);
childViewHolder.shopAdd = view.findViewById(R.id.tv_adapter_shop_bag_goods_add);
childViewHolder.shopInfo = view.findViewById(R.id.ll_shop_info);
view.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) view.getTag();
}
childViewHolder.shopSelect.setChecked(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).isChecked());
childViewHolder.shopInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean childViewSelect = childViewHolder.shopSelect.isChecked();
shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).setChecked(!childViewSelect);
childViewHolder.shopSelect.setChecked(!childViewSelect);
setGroupViewSelect(i, i1, !childViewSelect);
addGood(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1),!childViewSelect);
notifyDataSetChanged();
}
});
childViewHolder.shopSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean childViewSelect = childViewHolder.shopSelect.isChecked();
shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).setChecked(childViewSelect);
childViewHolder.shopSelect.setChecked(childViewSelect);
setGroupViewSelect(i, i1, childViewSelect);
addGood(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1),childViewSelect);
notifyDataSetChanged();
}
});
childViewHolder.shopInfo.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if(deleteGoodsInfoListener != null){
deleteGoodsInfoListener.deleteGoodsInfo(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1));
}
return false;
}
});
childViewHolder.shopLess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
int currentNum = Integer.parseInt(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).getGoods_number());
if(currentNum <= shopMinNum){
Toast.makeText(context, "最少购买一件商品,长按可删除商品", Toast.LENGTH_SHORT).show();
}else{
shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).setGoods_number(String.valueOf(currentNum - 1));
childViewHolder.shopNum.setText(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).getGoods_number());
caculatorAllPrice();
}
}catch (NumberFormatException exception){
Toast.makeText(context, "请稍后重试", Toast.LENGTH_SHORT).show();
}
}
});
childViewHolder.shopAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
int currentNum = Integer.parseInt(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).getGoods_number());
shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).setGoods_number(String.valueOf(currentNum + 1));
childViewHolder.shopNum.setText(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).getGoods_number());
caculatorAllPrice();
} catch (NumberFormatException exception) {
Toast.makeText(context, "请稍后重试", Toast.LENGTH_SHORT).show();
}
}
});
Glide.with(context)
.load(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).getGoods_thumb())
.into(childViewHolder.shopImage);
childViewHolder.shopName.setText(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).getGoods_name());
childViewHolder.shopWeight.setText(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).getTid());
childViewHolder.shopPrice.setText(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).getMarket_price());
childViewHolder.shopNum.setText(shopBagGoodsListModel.getGoods_list().get(i).getGoods_list().get(i1).getGoods_number());
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
class GroupViewHolder {
public CheckBox storeSelect;
public TextView storeName;
}
class ChildViewHolder {
public CheckBox shopSelect;
public ImageView shopImage;
public TextView shopName;
public TextView shopWeight;
public TextView shopPrice;
public TextView shopLess;
public TextView shopNum;
public TextView shopAdd;
public TextView shopDelete;
public LinearLayout shopInfo;
}
private void setChildViewSelect(int groupPosition, boolean isSelect) {
for (ShopBagGoodsModel shopBagGoodsModel : shopBagGoodsListModel.getGoods_list().get(groupPosition).getGoods_list()) {
shopBagGoodsModel.setChecked(isSelect);
}
for (int i = 0; i < shopBagGoodsListModel.getGoods_list().size(); i++) {
if (!shopBagGoodsListModel.getGoods_list().get(i).isSelect()) {
if (shopAllSelectListener != null) {
shopAllSelectListener.shopAllSelect(false);
return;
}
}
}
if (shopAllSelectListener != null) {
shopAllSelectListener.shopAllSelect(true);
}
}
private void setGroupViewSelect(int groupPosition, int childPosition, boolean isSelect) {
Log.i("TAG","当前的父view是:"+groupPosition+",子view是:"+childPosition);
if (isSelect) {
for (ShopBagGoodsModel shopBagGoodsModel : shopBagGoodsListModel.getGoods_list().get(groupPosition).getGoods_list()) {
if (!shopBagGoodsModel.isChecked()) {
shopBagGoodsListModel.getGoods_list().get(groupPosition).setSelect(false);
notifyDataSetChanged();
return;
}
}
shopBagGoodsListModel.getGoods_list().get(groupPosition).setSelect(true);
for (ShopBagStoreModel shopBagStoreModel : shopBagGoodsListModel.getGoods_list()) {
if (!shopBagStoreModel.isSelect()) {
shopAllSelectListener.shopAllSelect(false);
}
}
shopAllSelectListener.shopAllSelect(true);
} else {
shopAllSelectListener.shopAllSelect(false);
shopBagGoodsListModel.getGoods_list().get(groupPosition).setSelect(false);
}
}
private void addStore(int groupPosition, boolean isSelect) {
if (isSelect) {
for (ShopBagGoodsModel shopBagGoodsModel : shopBagGoodsListModel.getGoods_list().get(groupPosition).getGoods_list()) {
selectGoodsMap.put(shopBagGoodsModel.getGoods_id(), shopBagGoodsModel);
}
} else {
Iterator iterator = selectGoodsMap.keySet().iterator();
while (iterator.hasNext()) {
String str = (String) iterator.next();
for (ShopBagGoodsModel shopBagGoodsModel : shopBagGoodsListModel.getGoods_list().get(groupPosition).getGoods_list()) {
if (str.equals(shopBagGoodsModel.getGoods_id())) {
iterator.remove();
selectGoodsMap.remove(shopBagGoodsModel.getGoods_id());
}
}
}
}
caculatorAllPrice();
}
private void addGood(ShopBagGoodsModel shopBagGoodsModel, boolean isSelect){
if(isSelect){
selectGoodsMap.put(shopBagGoodsModel.getGoods_id(),shopBagGoodsModel);
}else{
Iterator iterator = selectGoodsMap.keySet().iterator();
while (iterator.hasNext()) {
String str = (String) iterator.next();
if(str.equals(shopBagGoodsModel.getGoods_id())){
iterator.remove();
selectGoodsMap.remove(shopBagGoodsModel.getGoods_id());
}
}
}
caculatorAllPrice();
}
private void caculatorAllPrice() {
if (selectGoodsMap != null && selectGoodsMap.size() > 0) {
double totalPrice = 0.0;
for (Map.Entry entry : selectGoodsMap.entrySet()) {
ShopBagGoodsModel model = (ShopBagGoodsModel) entry.getValue();
double currentPrice = 0.0;
int currentNum = shopMinNum;
try {
if (model.getMarket_price() != null && model.getMarket_price().length() > 0 && model.getMarket_price().contains("¥")) {
currentPrice = Double.parseDouble(model.getMarket_price().replace("¥", ""));
}
if (model.getGoods_number() != null && model.getGoods_number().length() > 0) {
currentNum = Integer.parseInt(model.getGoods_number());
}
totalPrice += (currentPrice * currentNum);
BigDecimal bigDecimal = new BigDecimal(totalPrice);
totalPrice = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
} catch (NumberFormatException exception) {
Log.i("TAG", "数字转换失败");
priceCaculatorErrorListener.priceCaculatorError();
}
priceChangeListener.nowPrice(totalPrice);
}
} else {
priceChangeListener.nowPrice(0.0);
}
}
public void setShopAllSelectListener(ShopAllSelectListener shopAllSelectListener) {
this.shopAllSelectListener = shopAllSelectListener;
}
public interface ShopAllSelectListener {
void shopAllSelect(boolean selectAll);
}
public void setPriceChangeListener(PriceChangeListener priceChangeListener) {
this.priceChangeListener = priceChangeListener;
}
public interface PriceChangeListener {
void nowPrice(double price);
}
public void setPriceCaculatorErrorListener(PriceCaculatorErrorListener priceCaculatorErrorListener) {
this.priceCaculatorErrorListener = priceCaculatorErrorListener;
}
public interface PriceCaculatorErrorListener {
void priceCaculatorError();
}
public void setDeleteGoodsInfoListener(DeleteGoodsInfoListener deleteGoodsInfoListener){
this.deleteGoodsInfoListener = deleteGoodsInfoListener;
}
public interface DeleteGoodsInfoListener{
void deleteGoodsInfo(ShopBagGoodsModel shopBagGoodsModel);
}
}