Android 购物车的功能逻辑

public class ShopcartExpanableFragment extends Fragment implements ShopcartVIew {

private Activity mActivity;
private View rootView;
private TextView tvSum;
private CheckBox cbCheckAll;
private ExpandableListView expandableListView;
private ShopcartPresenter shopcartPresenter;
private ShopcartExpanListAdapter expanListAdapter;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    mActivity = (Activity) context;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_shop_cart, null);
    expandableListView = rootView.findViewById(R.id.expanListView);
    tvSum = rootView.findViewById(R.id.tvSum);
    cbCheckAll = rootView.findViewById(R.id.cbCheckAll);
    return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    shopcartPresenter = new ShopcartPresenter(this);

    initData();


    cbCheckAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked) {
                setCheckAll(1);
            } else {
                setCheckAll(0);
            }
        }
    });
}


private void setCheckAll(int s) {
    int itemCount = expanListAdapter.getGroupCount();

    for (int i = 0; i < itemCount; i++) {

        ShopcartEntity item = (ShopcartEntity) expanListAdapter.getGroup(i);

//            item.setChecked(s!=0);

        List itemList = item.getList();

        for (int j = 0; j < itemList.size(); j++) {
            ShopcartInnerEntity innerEntity = itemList.get(j);
            innerEntity.setSelected(s);
        }
    }

    expanListAdapter.notifyDataSetChanged();
    getTotal();

}


public void getTotal() {

    double total = 0;
    int itemCount = expanListAdapter.getGroupCount();
    for (int i = 0; i < itemCount; i++) {
        ShopcartEntity item = (ShopcartEntity) expanListAdapter.getGroup(i);
        List list = item.getList();
        for (int j = 0; j < list.size(); j++) {
            ShopcartInnerEntity innerEntity = list.get(j);

            boolean checked = innerEntity.isChecked();

            if (checked) {
                double price = innerEntity.getPrice();
                total += price * innerEntity.getNum();
            }
        }
    }

    // TODO: 2018/11/20 小数保留2位
    tvSum.setText("合计:" + total);

}


private void initData() {
    shopcartPresenter.getShopcart();
}


@Override
public void onDestroyView() {
    super.onDestroyView();
}

@Override
public void onDetach() {
    super.onDetach();
    mActivity = null;
}

@Override
public void onDestroy() {
    super.onDestroy();
}


@Override
public void onDataSucess(List entityList) {

    if (expanListAdapter == null) {
        expanListAdapter = new ShopcartExpanListAdapter(entityList, getActivity());
        expandableListView.setAdapter(expanListAdapter);

    }

    for (int i = 0; i < expanListAdapter.getGroupCount(); i++) {
        //展开分组
        expandableListView.expandGroup(i);
    }

    initShopcartChange();

}


private void initShopcartChange() {


    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

            ShopcartEntity group = (ShopcartEntity) expanListAdapter.getGroup(groupPosition);
            group.setChecked(!group.isChecked());
            int c = 0;

            if (group.isChecked()) {
                c = 1;
            }

            List list = group.getList();
            for (int i = 0; i < list.size(); i++) {
                ShopcartInnerEntity shopcartInnerEntity = list.get(i);
                shopcartInnerEntity.setSelected(c);
            }
            expanListAdapter.notifyDataSetChanged();
            getTotal();
            return true;
        }
    });

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

            ShopcartInnerEntity child = (ShopcartInnerEntity) expanListAdapter.getChild(groupPosition, childPosition);
            boolean checked = child.isChecked();
            if (checked) {
                child.setSelected(0);
            } else {
                child.setSelected(1);
            }

            expanListAdapter.notifyDataSetChanged();
            getTotal();

            return true;
        }
    });

    expanListAdapter.setOnNumChangedListener(new AddSumLayout.OnNumChangedListener() {
        @Override
        public void onNumChanged(View view, int curNum) {
            getTotal();
        }
    });
}

@Override
public void onFailer(String msg) {

}
}

你可能感兴趣的:(Android 购物车的功能逻辑)