父级
private boolean isGroupChoosed;
public boolean isGroupChoosed() {
return isGroupChoosed;
}
public void setGroupChoosed(boolean groupChoosed) {
isGroupChoosed = groupChoosed;
}
子级
private boolean isChildChoosed;
public boolean isChildChoosed() {
return isChildChoosed;
}
public void setChildChoosed(boolean childChoosed) {
isChildChoosed = childChoosed;
}
fragment
public class FragmentGWU extends Fragment implements IMGWCActivity,MyGwcListAdapter.CheckGroupItemListener,ModifyGoodsItemNumberListener{ private TextView tvAllPrice; private TextView btnAmount; private TextView btnEditor; private CheckBox btnCheckAll; private ExpandableListView expandList; private GWCPresenter presenter; private MyGwcListAdapter adapter; private List<GWCBean.DataBean> data; private int totalNum = 0; private double totalPrice= 0.00; private boolean flag; private ActionMode supportActionBar; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate( R.layout.fragment_gouwuche, container, false ); presenter = new GWCPresenter( this ); presenter.getGWC( 71 + "" ); expandList = view.findViewById( R.id.expandList ); btnCheckAll = view.findViewById( R.id.btnCheckAll ); btnAmount = view.findViewById(R.id.tv_num); tvAllPrice = view.findViewById( R.id.tvAllPrice ); adapter = new MyGwcListAdapter( getActivity() ); expandList.setAdapter( adapter ); adapter.setModifyGoodsItemNumberListener( this ); adapter.setCheckGroupItemListener( this ); btnCheckAll.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { isChoosedAll( ((CheckBox) view).isChecked() ); checkedPrice(); } } ); return view; } @Override public void GWC(GWCBean bean) { data = bean.getData(); this.data=data; adapter.setList(data); defaultExpand(); } private void defaultExpand(){ for(int i=0;i<adapter.getGroupCount();i++){ expandList.expandGroup(i); } } public void onFailed(String msg) { Toast.makeText(getActivity(),msg.toString(),Toast.LENGTH_SHORT).show(); } @Override public void doIncrease(int groupPosition, int childPosition, View view) { GWCBean.DataBean.ListBean listBean = data.get(groupPosition).getList().get(childPosition); int currentNum = listBean.getNum(); currentNum++; listBean.setNum(currentNum); adapter.notifyDataSetChanged(); checkedPrice(); } @Override public void doDecrease(int groupPosition, int childPosition, View view) { GWCBean.DataBean.ListBean listBean = data.get(groupPosition).getList().get(childPosition); int currentNum = listBean.getNum(); if(currentNum==1){ return; } currentNum--; listBean.setNum(currentNum); adapter.notifyDataSetChanged(); checkedPrice(); } public void deleteItem(int childPosition) { data.remove(childPosition); adapter.notifyDataSetChanged(); checkedPrice(); } @Override public void checkGroupItem(int groupPosition, boolean isChecked) { GWCBean.DataBean dataBean = data.get(groupPosition); dataBean.setGroupChoosed(isChecked); for(GWCBean.DataBean.ListBean listBean:dataBean.getList()){ listBean.setChildChoosed(isChecked); } if(isCheckAll()){ btnCheckAll.setChecked(true); }else{ btnCheckAll.setChecked(false); } adapter.notifyDataSetChanged(); checkedPrice(); } @Override public void checkChildItem(int groupPosition, int childPosition, boolean isChecked) { GWCBean.DataBean dataBean = data.get(groupPosition); GWCBean.DataBean.ListBean listBean = dataBean.getList().get(childPosition); listBean.setChildChoosed(isChecked); boolean result = isGoodsCheckAll(groupPosition); if(result){ dataBean.setGroupChoosed(result); }else{ dataBean.setGroupChoosed(result); } if(isCheckAll()){ btnCheckAll.setChecked(true); }else{ btnCheckAll.setChecked(false); } adapter.notifyDataSetChanged(); checkedPrice(); } private boolean isGoodsCheckAll(int groupPosition){ List<GWCBean.DataBean.ListBean> lists = this.data.get(groupPosition).getList(); for(GWCBean.DataBean.ListBean list:lists){ if(list.isChildChoosed()){ continue; }else{ return false; } } return true; } private boolean isCheckAll(){ for(GWCBean.DataBean dataBean:data){ if(!dataBean.isGroupChoosed()){ return false; } } return true; } private void isChoosedAll(boolean isChecked){ for(GWCBean.DataBean dataBean:data){ dataBean.setGroupChoosed(isChecked); for(GWCBean.DataBean.ListBean listBean:dataBean.getList()){ listBean.setChildChoosed(isChecked); } } adapter.notifyDataSetChanged(); } private void checkedPrice(){ totalNum = 0; totalPrice = 0.00; for (GWCBean.DataBean dataBean : data){ for (GWCBean.DataBean.ListBean listBean : dataBean.getList()){ if(listBean.isChildChoosed()){ totalNum++; totalPrice += listBean.getNum()*listBean.getPrice(); } } } tvAllPrice.setText(totalPrice + ""); btnAmount.setText("结算("+totalNum+")"); }
}
Adapter
public class MyGwcListAdapter extends BaseExpandableListAdapter { private List<GWCBean.DataBean> list; Context context; private ModifyGoodsItemNumberListener modifyGoodsItemNumberListener; private CheckGroupItemListener checkGroupItemListener; private boolean isEditor; public MyGwcListAdapter(Context context) { this.context = context; } public void setList(List<GWCBean.DataBean> list) { this.list = list; } @Override public int getGroupCount() { return list != null ? list.size() : 0; } @Override public int getChildrenCount(int groupPosition) { return list != null && list.get( groupPosition ).getList() != null ? list.get( groupPosition ).getList().size() : 0; } @Override public Object getGroup(int groupPosition) { return list.get( groupPosition ); } @Override public Object getChild(int groupPosition, int childPosition) { return list.get( groupPosition ).getList().get( childPosition ); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if(convertView==null){ convertView = LayoutInflater.from(context).inflate(R.layout.layout_group, parent, false); } CheckBox group_choosed = convertView.findViewById(R.id.group_choosed); group_choosed.setText(list.get(groupPosition).getSellerName()); if(list.get(groupPosition).isGroupChoosed()){ group_choosed.setChecked(true); }else{ group_choosed.setChecked(false); } group_choosed.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { checkGroupItemListener.checkGroupItem(groupPosition,((CheckBox)view).isChecked()); } }); return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final View view = LayoutInflater.from( context ).inflate( R.layout.layout_child, null ); final CheckBox child_choosed = view.findViewById( R.id.child_choosed ); ImageView img = view.findViewById( R.id.img ); TextView titleName = view.findViewById( R.id.titleName ); TextView littleTitle = view.findViewById( R.id.littleTitle ); TextView price = view.findViewById( R.id.price ); TextView count = view.findViewById( R.id.count ); TextView btnSub = view.findViewById( R.id.btnSub ); final TextView showNum = view.findViewById( R.id.showNum ); TextView btnAdd = view.findViewById( R.id.btnAdd ); titleName.setText( list.get( groupPosition ).getList().get( childPosition ).getTitle() ); littleTitle.setText( list.get( groupPosition ).getList().get( childPosition ).getSubhead() ); price.setText( "¥" + list.get( groupPosition ).getList().get( childPosition ).getPrice() ); count.setText( "x" + list.get( groupPosition ).getList().get( childPosition ).getNum() ); showNum.setText( list.get( groupPosition ).getList().get( childPosition ).getNum() + "" ); String images = list.get( groupPosition ).getList().get( childPosition ).getImages(); String[] urls = images.split( "\\|" ); Glide.with( context ).load( urls[0] ).into( img ); btnAdd.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { modifyGoodsItemNumberListener.doIncrease( groupPosition,childPosition,showNum ); } } ); btnSub.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { modifyGoodsItemNumberListener.doDecrease( groupPosition,childPosition,showNum ); } } ); child_choosed.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { checkGroupItemListener.checkChildItem( groupPosition,childPosition,((CheckBox)view).isChecked() ); } } ); if (list.get( groupPosition ).getList().get( childPosition ).isChildChoosed()){ child_choosed.setChecked( true ); }else { child_choosed.setChecked( false ); } return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } public void setModifyGoodsItemNumberListener(FragmentGWU modifyGoodsItemNumberListener) { this.modifyGoodsItemNumberListener = modifyGoodsItemNumberListener; } public void setCheckGroupItemListener(FragmentGWU checkGroupItemListener) { this.checkGroupItemListener = checkGroupItemListener; } public interface CheckGroupItemListener { void checkGroupItem(int groupPosition, boolean isChecked); void checkChildItem(int groupPosition, int childPosition, boolean isChecked); } public interface ModifyGoodsItemNumberListener { void doIncrease(int groupPosition, int childPosition, View view); void doDecrease(int groupPosition, int childPosition, View view); } }