add_sub_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:background="#000"
android:orientation="horizontal"
android:padding="2dp"
>
<TextView
android:id="@+id/tv_sub_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:text="-"
android:textColor="#000"
/>
<TextView
android:id="@+id/tv_number_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:text="1"
android:textColor="#000"
/>
<TextView
android:id="@+id/tv_add_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:text="+"
android:textColor="#000"
/>
</LinearLayout>
child_view_layout_shopcar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:gravity="center_vertical"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/cb_child_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
/>
<ImageView
android:id="@+id/iv_icon_child"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_title_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="标题"
android:textColor="#000"
/>
<TextView
android:id="@+id/tv_price_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
/>
</LinearLayout>
<com.example.yuekaolianxi2018.MyAddSubView
android:id="@+id/add_sub_view_child"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
>
</com.example.yuekaolianxi2018.MyAddSubView>
</LinearLayout>
group_view_layout_shopcar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/cb_group_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_title_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ellipsize="end"
android:maxLines="2"
android:text="标题"
android:textColor="#000"
android:textSize="20sp"
/>
</LinearLayout>
item_gwc.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.example.yuekaolianxi2018.Fragment.GwcFragment">
<ExpandableListView
android:id="@+id/elc_show_main"
android:layout_width="match_parent"
android:background="#fff"
android:layout_weight="1"
android:layout_height="0dp"></ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_allCheck_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
android:textColor="#000"
/>
<TextView
android:id="@+id/btn_allprice_main"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="价格"
android:textColor="@color/colorAccent"
android:textSize="20sp"
/>
<Button
android:id="@+id/btn_allNumber_main"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="去结算(0)"
/>
</LinearLayout>
</LinearLayout>
Adapter<MyAdapter>购物车的
public class MyAdapter extends BaseExpandableListAdapter {
private List<CarInfo.DataBean> sellerData;
private Context context;
public MyAdapter(List<CarInfo.DataBean> sellerData, Context context) {
this.sellerData = sellerData;
this.context = context;
}
@Override
public int getGroupCount(){
return sellerData.size();
}
@Override
public int getChildrenCount(int groupPosition){
return sellerData.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition){
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition){
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return 0;
}
@Override
public boolean hasStableIds(){
return false;
}
//设置外层
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder = null;
if (convertView == null){
groupViewHolder = new GroupViewHolder();
convertView = View.inflate(context, R.layout.group_view_layout_shopcar, null);
groupViewHolder.cb_group_item = convertView.findViewById(R.id.cb_group_item);
groupViewHolder.tv_title_group = convertView.findViewById(R.id.tv_title_group);
convertView.setTag(groupViewHolder);
}else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
//赋值
CarInfo.DataBean dataBean = sellerData.get(groupPosition);
//商家名称
groupViewHolder.tv_title_group.setText(dataBean.getSellerName());
boolean b = isCurrentSellerAllProductSelected(groupPosition);
//商家的checkbox
groupViewHolder.cb_group_item.setChecked(b);
//点击checkbox触发监听
groupViewHolder.cb_group_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onCartListChangeListener != null) {
onCartListChangeListener.SellerSelectedChange(groupPosition);
}
}
});
return convertView;
}
public void changeCurrentSellerAllProductSelected(int Position, boolean b) {
List<CarInfo.DataBean.ListBean> list = sellerData.get(Position).getList();
for (int i = 0; i < list.size(); i++) {
list.get(i).setSelected(b ? 1 : 0);
}
}
//用来判断外层的Selected值 如果是0 不选中 否则选中
public boolean isCurrentSellerAllProductSelected(int position) {
List<CarInfo.DataBean.ListBean> list = sellerData.get(position).getList();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getSelected() == 0) {
return false;
}
}
return true;
}
//设置子层
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder = null;
if (convertView == null) {
childViewHolder = new ChildViewHolder();
convertView = View.inflate(context, R.layout.child_view_layout_shopcar, null);
childViewHolder.cb_child_item = convertView.findViewById(R.id.cb_child_item);
childViewHolder.tv_title_child = convertView.findViewById(R.id.tv_title_child);
childViewHolder.tv_price_child = convertView.findViewById(R.id.tv_price_child);
childViewHolder.iv_icon_child = convertView.findViewById(R.id.iv_icon_child);
childViewHolder.add_sub_view_child = convertView.findViewById(R.id.add_sub_view_child);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
//赋值
CarInfo.DataBean.ListBean listBean = sellerData.get(groupPosition).getList().get(childPosition);
//商品名称
childViewHolder.tv_title_child.setText(listBean.getTitle());
//商品图片
String images = listBean.getImages();
String[] split = images.split("\\|");
Glide.with(context).load(split[0]).into(childViewHolder.iv_icon_child);
//商品价格
childViewHolder.tv_price_child.setText("¥" + listBean.getPrice());
//商品checkb0x
childViewHolder.cb_child_item.setChecked(listBean.getSelected() == 1);
childViewHolder.cb_child_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onCartListChangeListener.changeCurrentProductSelected(groupPosition, childPosition);
}
});
childViewHolder.add_sub_view_child.setOnNumberChangeListener(new MyAddSubView.OnNumberChangeListener() {
@Override
public void OnNumberChange(int number) {
onCartListChangeListener.ProductNumberChange(groupPosition, childPosition, number);
}
});
return convertView;
}
public void changeCurrentProductNumber(int groupPosition, int childPosition, int number) {
CarInfo.DataBean.ListBean listBean = sellerData.get(groupPosition).getList().get(childPosition);
listBean.setNum(number);
}
public void changeCurrentProductSelected(int groupPosition, int childPosition) {
CarInfo.DataBean.ListBean listBean = sellerData.get(groupPosition).getList().get(childPosition);
listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0);
}
public double calculateTotalPrice() {
double totalPrice = 0;
for (int i = 0; i < sellerData.size(); i++) {
List<CarInfo.DataBean.ListBean> list = sellerData.get(i).getList();
for (int j = 0; j < list.size(); j++) {
//只有选中采取计算
if (list.get(j).getSelected() == 1) {
double price = list.get(j).getPrice();
int num = list.get(j).getNum();
totalPrice += price * num;
}
}
}
return totalPrice;
}
public int calculateTotalNumber() {
int totalNumber = 0;
for (int i = 0; i < sellerData.size(); i++) {
List<CarInfo.DataBean.ListBean> list = sellerData.get(i).getList();
for (int j = 0; j < list.size(); j++) {
//只有选中采取计算
if (list.get(j).getSelected() == 1) {
int num = list.get(j).getNum();
totalNumber += num;
}
}
}
return totalNumber;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public boolean isAllProductsSelected() {
for (int i = 0; i < sellerData.size(); i++) {
List<CarInfo.DataBean.ListBean> list = sellerData.get(i).getList();
for (int j = 0; j < list.size(); j++) {
if (list.get(j).getSelected() == 0) {
return false;
}
}
}
return true;
}
public void changeAllProductsSelected(boolean b) {
for (int i = 0; i < sellerData.size(); i++) {
List<CarInfo.DataBean.ListBean> list = sellerData.get(i).getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setSelected(b ? 1 : 0);
}
}
}
public static class GroupViewHolder {
public CheckBox cb_group_item;
public TextView tv_title_group;
}
public static class ChildViewHolder {
public CheckBox cb_child_item;
public ImageView iv_icon_child;
public TextView tv_title_child;
public TextView tv_price_child;
public MyAddSubView add_sub_view_child;
}
OnCartListChangeListener onCartListChangeListener;
public void setOnCartListChangeListener(OnCartListChangeListener onCartListChangeListener) {
this.onCartListChangeListener = onCartListChangeListener;
}
public interface OnCartListChangeListener {
void SellerSelectedChange(int groupPosition);
void changeCurrentProductSelected(int groupPosition, int childPosition);
void ProductNumberChange(int groupPosition, int childPosition, int number);
}
}
Fragment<GwcFragment>
public class GwcFragment extends Fragment implements View.OnClickListener {
String url = "http://www.zhaoapi.cn/product/getCarts?uid=71";
private ExpandableListView elc_show_main;
private CheckBox cb_allCheck_main;
private TextView btn_allPrice_main;
private Button btn_allNumber_main;
private MyAdapter myAdapter;
private void initData() {
//得到okhttp
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(final Call call, Response response) throws IOException {
final String string = response.body().string();
getActivity().runOnUiThread(new Runnable() {
private List<CarInfo.DataBean> sellerData;
@Override
public void run() {
Gson gson = new Gson();
CarInfo cartInfo = gson.fromJson(string, CarInfo.class);
sellerData = cartInfo.getData();
//创建适配
//创建适配
myAdapter = new MyAdapter(sellerData, getActivity());
//调用适配器的借口 来实现回电数据
myAdapter.setOnCartListChangeListener(new MyAdapter.OnCartListChangeListener() {
@Override
public void SellerSelectedChange(int groupPosition) {
//先得到 checkbox
boolean b = myAdapter.isCurrentSellerAllProductSelected(groupPosition);
//改变所有当前商家的选中状态
myAdapter.changeCurrentSellerAllProductSelected(groupPosition, !b);
myAdapter.notifyDataSetChanged();
refreshAllSelectedAndTotalPriceAndTotalNumber();
}
@Override
public void changeCurrentProductSelected(int groupPosition, int childPosition) {
myAdapter.changeCurrentProductSelected(groupPosition, childPosition);
myAdapter.notifyDataSetChanged();
refreshAllSelectedAndTotalPriceAndTotalNumber();
}
@Override
public void ProductNumberChange(int groupPosition, int childPosition, int number) {
myAdapter.changeCurrentProductNumber(groupPosition, childPosition, number);
myAdapter.notifyDataSetChanged();
//刷新底部的方法
refreshAllSelectedAndTotalPriceAndTotalNumber();
}
});
elc_show_main.setAdapter(myAdapter);
//
for (int i = 0; i < sellerData.size(); i++) {
elc_show_main.expandGroup(i);
}
}
});
}
});
}
private void refreshAllSelectedAndTotalPriceAndTotalNumber() {
boolean allProductsSelected = myAdapter.isAllProductsSelected();
cb_allCheck_main.setChecked(allProductsSelected);
//计算总金额
Double totalPrice = myAdapter.calculateTotalPrice();
btn_allPrice_main.setText("总价:¥" + totalPrice);
//计算总数量
int totalNumber = myAdapter.calculateTotalNumber();
btn_allNumber_main.setText("去结算(" + totalNumber + ")");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_allNumber_main:
break;
case R.id.cb_allCheck_main:
boolean allProductsSelected = myAdapter.isAllProductsSelected();
myAdapter.changeAllProductsSelected(!allProductsSelected);
myAdapter.notifyDataSetChanged();
//刷新底部的方法
refreshAllSelectedAndTotalPriceAndTotalNumber();
break;
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initData();
View view = inflater.inflate(R.layout.item_gwc, container, false);
elc_show_main = view.findViewById(R.id.elc_show_main);
cb_allCheck_main = view.findViewById(R.id.cb_allCheck_main);
btn_allPrice_main = view.findViewById(R.id.btn_allprice_main);
btn_allNumber_main = view.findViewById(R.id.btn_allNumber_main);
cb_allCheck_main.setOnClickListener(this);
btn_allNumber_main.setOnClickListener(this);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
MyAddSubView
public class MyAddSubView extends LinearLayout implements View.OnClickListener{ TextView tv_sub_view; TextView tv_number_view; TextView tv_add_view; private int number = 1; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; tv_number_view.setText(number+""); } public MyAddSubView(Context context) { super(context,null); } public MyAddSubView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); View inflate = View.inflate(context, R.layout.add_sub_view, this); tv_sub_view = inflate.findViewById(R.id.tv_sub_view); tv_number_view = inflate.findViewById(R.id.tv_number_view); tv_add_view = inflate.findViewById(R.id.tv_add_view); tv_add_view.setOnClickListener(this); tv_sub_view.setOnClickListener(this); } OnNumberChangeListener onNumberChangeListener; public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) { this.onNumberChangeListener = onNumberChangeListener; } @Override public void onClick(View v) { switch (v.getId()){ case R.id.tv_sub_view: if (number>1){ --number; tv_number_view.setText(number+""); if (onNumberChangeListener != null){ onNumberChangeListener.OnNumberChange(number); } }else { Toast.makeText(getContext(), "不能再少啦!", Toast.LENGTH_SHORT).show(); } break; case R.id.tv_add_view: ++number; tv_number_view.setText(number+""); if (onNumberChangeListener!=null){ onNumberChangeListener.OnNumberChange(number); } break; } } public interface OnNumberChangeListener { void OnNumberChange(int number); } }