您的位置:首页 > 其它

二级列表购物车的全选反选

2017-11-19 20:07 387 查看
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/shop_top"/>
<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="wrap_content"
android:layout_height="408dp"
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/shop_item"

/>

</RelativeLayout>
引入的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/id_rl_head"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@android:color/transparent"
android:orientation="vertical">
<ImageView
android:id="@+id/id_iv_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:layout_marginTop="5dp"
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_marginTop="5dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:minHeight="48dp"
android:text="编辑"
android:textColor="#1a1a1a"
android:textSize="16dp"
android:visibility="visible" />
</RelativeLayout>
引入二
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/id_rl_foot"

android:background="#00f7f3f3">
<ImageView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_alignParentTop="true"
android:background="#ccc"/>

<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="#fcfbfb"
/>
<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="#fcfbfb"
/>
</LinearLayout>

<CheckBox
android:id="@+id/id_cb_select_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="@null"
android:text=" 全选"
android:layout_marginStart="34dp" />
</RelativeLayout>



适配器
import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.Map;

/**
* Created by louisgeek on 2016/4/27.
*/
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.listone_item, null);
groupViewHolder = new GroupViewHolder();

groupViewHolder.tv_title_parent =convertView
.findViewById(R.id.tv_title_parent);
groupViewHolder.id_tv_edit =convertView
.findViewById(R.id.id_tv_edit);
groupViewHolder.id_cb_select_parent =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;
}
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.listtwo_item, 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) {
dealAdd(goodsBean);
}
});
childViewHolder.id_iv_reduce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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) {
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);
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() {
totalCount = 0;
totalPrice = 0.00;
for (int i = 0; i < parentMapList.size(); i++) {

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);
notifyDataSetChanged();
dealPrice();
}

public void dealReduce(GoodsBean goodsBean) {
int count = goodsBean.getCount();
if (count == 1) {
return;
}
count--;
goodsBean.setCount(count);
notifyDataSetChanged();
dealPrice();
}

public void removeOneGood(int groupPosition, int childPosition) {
List<Map<String, Object>> childMapList = childMapList_list.get(groupPosition);
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;

}

}
购物车Fragment
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ShopFragment extends Fragment {
//定义父列表项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;

private View view;
private ImageView mIdRlCartIsEmptyImage;
/**
* 购物车为空
*/
private TextView mIdRlCartIsEmptyText;
private RelativeLayout mIdRlCartIsEmpty;
private ExpandableListView mIdElvListview;
/**
* 全选
*/
private CheckBox mIdCbSelectAll;
/**
* 合计:¥0.00
*/
private TextView mIdTvTotalPrice;
/**
* 不含运费
*/
private TextView mTvPostPrice;
/**
* 结算(0)
*/
private TextView mIdTvTotalCountJiesuan;
private LinearLayout mIdLlNormalAllState;
/**
* 移到收藏夹
*/
private TextView mIdTvSaveStarAll;
/**
* 删除
*/
private TextView mIdTvDeleteAll;
private LinearLayout mIdLlEditingAllState;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.shop, null);

into();
ButterKnife.bind(this, view);
initCartData();
// initView(view);

expandableListView = (ExpandableListView) view.findViewById(R.id.id_elv_listview);
myBaseExpandableListAdapter = new MyBaseExpandableListAdapter(getContext(), 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+1, Toast.LENGTH_SHORT).show();
//            }
//        });

for (int i = 0; i < parentMapList.size(); i++) {
expandableListView.expandGroup(i);
}

ImageView id_iv_back = (ImageView) view.findViewById(R.id.id_iv_back);
//点击返回按钮
id_iv_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "click :back", Toast.LENGTH_SHORT).show();
}
});

id_ll_normal_all_state = (LinearLayout) view.findViewById(R.id.id_ll_normal_all_state);
id_ll_editing_all_state = (LinearLayout) view.findViewById(R.id.id_ll_editing_all_state);
id_rl_cart_is_empty = (RelativeLayout) view.findViewById(R.id.id_rl_cart_is_empty);

//移动到收藏夹
TextView id_tv_save_star_all = (TextView) view.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(getActivity(), "收藏多选商品", Toast.LENGTH_SHORT).show();
}
});

//删除按钮
TextView id_tv_delete_all = (TextView) view.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) view.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) view.findViewById(R.id.id_cb_select_all);

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) view.findViewById(R.id.id_tv_totalPrice);
//结算
final TextView id_tv_totalCount_jiesuan = (TextView) view.findViewById(R.id.id_tv_totalCount_jiesuan);
id_tv_totalCount_jiesuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getActivity(), JieSuanActivity.class);
startActivity(intent);
Toast.makeText(getActivity(), "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) view.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(getContext(), 48);
lp.setMargins(0, top, 0, r_height);//48

expandableListView.setLayoutParams(lp);
/**==========*/

if (parentMapList != null && parentMapList.size() > 0) {

setupViewsShow(true);
} else {
setupViewsShow(false);
}

return view;
}

//显示和隐藏
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));

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 into() {
OkHttpClient okHttpClient = new OkHttpClient();
Request build = new Request.Builder()
.url("http://120.27.23.105/product/getCarts?uid=1496")
.build();
okHttpClient.newCall(build).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {
String s = response.body().toString();
Gson gson = new Gson();
//final ShopBean shopBean = gson.fromJson(s, ShopBean.class);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {

}
});
}
});
}

}
 大致的效果可能会有一些不足的地方

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: