效果图:
所需图片:
分别是:shop_icon;shopcart_add_red;shopcart_minus_grey;shopcart_minus_red
在敲代码之前需在Bean中手动添加几个属性: num:计算总价 ;
cou:计算选中商品数量默认是 1
oneischeck:一级列表是否被选中
twoischeck:二级列表是否被选中
MainActivity页面
public class ThreeFrgment extends Fragment implements Carview {
private View view;
private ExpandableListView car_edl;
private ImageView shopping_cart_title_img;
private TextView shopping_cart_title_edit;
private TextView shopping_cart_bottom_Price;
private TextView shopping_cart_bottom_jiesuan;
private CheckBox shopping_cart_bottom_CheckBox;
private List<CarBean.DataBean> list = new ArrayList<CarBean.DataBean>();
private ThreeAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.threefragment, container, false);
//注册EventBus
EventBus.getDefault().register(this);
initview();
huoqu();
shopping_cart_bottom_CheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判断全选的状态
adapter.checkALLStatus(shopping_cart_bottom_CheckBox.isChecked());
}
});
adapter = new ThreeAdapter(getActivity(), list);
car_edl.setAdapter(adapter);
//将一级列表的展开标志隐藏
car_edl.setGroupIndicator(null);
adapter.notifyDataSetChanged();
return view;
}
private void huoqu() {
ThreePresaenter presaenter = new ThreePresaenter();
presaenter.attchview(this);
presaenter.getnews();
}
private void initview() {
shopping_cart_title_img = (ImageView) view.findViewById(R.id.shopping_cart_title_img);//返回按钮
shopping_cart_title_edit = (TextView) view.findViewById(R.id.shopping_cart_title_edit);//编辑
shopping_cart_bottom_Price = (TextView) view.findViewById(R.id.shopping_cart_bottom_Price);//总价格
shopping_cart_bottom_jiesuan = (TextView) view.findViewById(R.id.shopping_cart_bottom_jiesuan);//结算
shopping_cart_bottom_CheckBox = (CheckBox) view.findViewById(R.id.shopping_cart_bottom_CheckBox);//全选
car_edl = (ExpandableListView) view.findViewById(R.id.car_edl);
}
@Override
public void success(List<CarBean.DataBean> news) {
if (news != null) {
list.addAll(news);
//循环集合
for (int i = 0; i < list.size(); i++) {
//将二级列表展开
car_edl.expandGroup(i);
}
adapter.notifyDataSetChanged();
}
}
@Override
public void failed(Exception e) {
Log.d("Car错误:", e.getMessage());
}
//接收适配器传过来的值
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
public void onMessageEvent(boolean event) {
//将全选的状态设置为true或者false
shopping_cart_bottom_CheckBox.setChecked(event);
}
//接收适配器传过来的数量和总价
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
public void onMessageEvent(CountPrice event) {
shopping_cart_bottom_jiesuan.setText("结算(" + event.getCount() + ")" + "");
shopping_cart_bottom_Price.setText("总价:¥"+event.getPrice() + "");
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
注:CountPrice是自定义的一个类存放结算和价格的变量,为了让EventBus发送一个对象;
MainActivity布局页面