/**
*1.点击全选:选中/未选中...调用适配器中的方法...myAdapter.setIsCheckAll(true);来设置所有的一级和二级是否选中,计算
*
* 2.一级列表的点击事件:
* 2.1改变当前一级选中的状态...dataBean.setChoosed(! dataBean.isChoosed());
* 2.2根据当前一级的状态,改变该组里面二级列表的状态....changeChilState(groupPosition,dataBean.isChoosed());
* 2.3通过判断所有的一级组是否选中,来决定是否全选选中:...changeAllState(isAllGroupChecked());
* 2.4发送价格个数量:...sendPriceAndCount();
* 2.5刷新适配器
* 3.二级列表点击事件:
* 3.1点击改变当前子条目状态:...listBean.setChildChoosed(! listBean.isChildChoosed());//相反
* 3.2发送价钱和数量给界面显示....sendPriceAndCount();
* 3.3如果当前子条目是选中状态
* 3.3.1选中
* 判断一下当前组中所有的子条目是否全部选中:...isAllChildSelected(groupPosition)
* 如果全部选中改变一下当前组的状态:...changGroupState(groupPosition,true);...确定是否改变全选changeAllState(isAllGroupChecked());
* 3.3.2未选中
* changGroupState(groupPosition,false);改变当前组false...是否全选changeAllState(isAllGroupChecked());
*/
public class MainActivity extends AppCompatActivity implements IMainActivity, View.OnClickListener {
private CartExpanableListview expanableListview;
private String cartUrl = "https://www.zhaoapi.cn/product/getCarts?uid=3690";
private CartPresenter cartPresenter;
private Gson gson;
private MyAdapter myAdapter;
private CheckBox check_all;
private TextView text_total;
private TextView text_buy;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 0){//更改价钱和数量的消息
CountPriceBean countPriceBean = (CountPriceBean) msg.obj;
text_total.setText("合计:¥"+countPriceBean.getPrice());
text_buy.setText("去计算("+countPriceBean.getCount()+")");
}else if (msg.what == 1){//改变全选的状态
boolean flag = (boolean) msg.obj;
check_all.setChecked(flag);
}
}
};
private List<CartBean.DataBean> listGroup;
private List<List<CartBean.DataBean.ListBean>> listChilds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
check_all = findViewById(R.id.check_all);
text_total = findViewById(R.id.text_total);
text_buy = findViewById(R.id.text_buy);
expanableListview = findViewById(R.id.expanable_listview);
//去掉默认的指示器
expanableListview.setGroupIndicator(null);
cartPresenter = new CartPresenter(this);
gson = new Gson();
//1.点击全选:选中/未选中...调用适配器中的方法...myAdapter.setIsCheckAll(true);来设置所有的一级和二级是否选中,计算
check_all.setOnClickListener(this);
text_buy.setOnClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
//请求数据
cartPresenter.getCartData(cartUrl);
}
@Override
public void getSuccessCartData(String json) {
Log.e("-----",json);
if(json == null || "null".equals(json) || json==""){
Toast.makeText(MainActivity.this,"购物车空,去逛逛",Toast.LENGTH_SHORT).show();
}else {
//解析数据
CartBean cartBean = gson.fromJson(json, CartBean.class);
//一个是一级标题的数据
listGroup = cartBean.getData();
//所有子条目的数据
listChilds = new ArrayList<>();
for (int i = 0; i< listGroup.size(); i++){
listChilds.add(listGroup.get(i).getList());
}
//根据网路上是否选中的状态,决定当前组是否选中
for (int i = 0;i<listGroup.size();i++){
if (isAllChildInGroupSelected(i)){
listGroup.get(i).setGroupChecked(true);
}
}
//所有的组是否选中,确定全选是否选中
check_all.setChecked(isAllGroupChecked());
//初始化一下价格和数量
sendPriceAndCount();
//设置适配器
myAdapter = new MyAdapter(MainActivity.this, listGroup, listChilds,handler);
expanableListview.setAdapter(myAdapter);
//展开所有
for (int i = 0; i< listGroup.size(); i++){
expanableListview.expandGroup(i);
}
}
}
/**
* 计算价格数量
*/
private void sendPriceAndCount() {
double price = 0;
int count = 0;
//通过判断二级列表是否勾选,,,,计算价格数量
for (int i=0;i<listGroup.size();i++){
for (int j = 0;j<listChilds.get(i).size();j++){
if (listChilds.get(i).get(j).getSelected() == 1){
price += listChilds.get(i).get(j).getNum() * listChilds.get(i).get(j).getPrice();
count += listChilds.get(i).get(j).getNum();
}
}
}
CountPriceBean countPriceBean = new CountPriceBean(price, count);
//传给activity进行显示
Message msg = Message.obtain();
msg.what = 0;
msg.obj = countPriceBean;
handler.sendMessage(msg);
}
/**
* 所有的一级列表是否选中
*/
private boolean isAllGroupChecked() {
for (int i =0;i<listGroup.size();i++){
if (! listGroup.get(i).isGroupChecked()){//代表一级列表有没选中的
return false;
}
}
return true;
}
/**
* 判断当前组里面所有的子条目是否选中
* @param groupPosition
* @return
*/
private boolean isAllChildInGroupSelected(int groupPosition) {
for (int i= 0;i<listChilds.get(groupPosition).size();i++){
//只要有一个没选中就返回false
if (listChilds.get(groupPosition).get(i).getSelected() ==0){
return false;
}
}
return true;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.check_all://1.点击全选:选中/未选中...调用适配器中的方法...myAdapter.setIsCheckAll(true);来设置所有的一级和二级是否选中,计算
myAdapter.setIsCheckAll(check_all.isChecked());//适配器中的所有checkBox是否选中
break;
case R.id.text_buy:
double price = 0;
int count = 0;
//通过判断二级列表是否勾选,,,,计算价格数量
for (int i=0;i<listGroup.size();i++){
for (int j = 0;j<listChilds.get(i).size();j++){
if (listChilds.get(i).get(j).getSelected() == 1){
price += listChilds.get(i).get(j).getNum() * listChilds.get(i).get(j).getPrice();
count += listChilds.get(i).get(j).getNum();
}
}
}
CountPriceBean countPriceBean = new CountPriceBean(price, count);
Intent intent = new Intent(MainActivity.this, QueRenOrderActivity.class);
intent.putExtra("order",countPriceBean);
startActivity(intent);
break;
}
}
}
*1.点击全选:选中/未选中...调用适配器中的方法...myAdapter.setIsCheckAll(true);来设置所有的一级和二级是否选中,计算
*
* 2.一级列表的点击事件:
* 2.1改变当前一级选中的状态...dataBean.setChoosed(! dataBean.isChoosed());
* 2.2根据当前一级的状态,改变该组里面二级列表的状态....changeChilState(groupPosition,dataBean.isChoosed());
* 2.3通过判断所有的一级组是否选中,来决定是否全选选中:...changeAllState(isAllGroupChecked());
* 2.4发送价格个数量:...sendPriceAndCount();
* 2.5刷新适配器
* 3.二级列表点击事件:
* 3.1点击改变当前子条目状态:...listBean.setChildChoosed(! listBean.isChildChoosed());//相反
* 3.2发送价钱和数量给界面显示....sendPriceAndCount();
* 3.3如果当前子条目是选中状态
* 3.3.1选中
* 判断一下当前组中所有的子条目是否全部选中:...isAllChildSelected(groupPosition)
* 如果全部选中改变一下当前组的状态:...changGroupState(groupPosition,true);...确定是否改变全选changeAllState(isAllGroupChecked());
* 3.3.2未选中
* changGroupState(groupPosition,false);改变当前组false...是否全选changeAllState(isAllGroupChecked());
*/
public class MainActivity extends AppCompatActivity implements IMainActivity, View.OnClickListener {
private CartExpanableListview expanableListview;
private String cartUrl = "https://www.zhaoapi.cn/product/getCarts?uid=3690";
private CartPresenter cartPresenter;
private Gson gson;
private MyAdapter myAdapter;
private CheckBox check_all;
private TextView text_total;
private TextView text_buy;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 0){//更改价钱和数量的消息
CountPriceBean countPriceBean = (CountPriceBean) msg.obj;
text_total.setText("合计:¥"+countPriceBean.getPrice());
text_buy.setText("去计算("+countPriceBean.getCount()+")");
}else if (msg.what == 1){//改变全选的状态
boolean flag = (boolean) msg.obj;
check_all.setChecked(flag);
}
}
};
private List<CartBean.DataBean> listGroup;
private List<List<CartBean.DataBean.ListBean>> listChilds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
check_all = findViewById(R.id.check_all);
text_total = findViewById(R.id.text_total);
text_buy = findViewById(R.id.text_buy);
expanableListview = findViewById(R.id.expanable_listview);
//去掉默认的指示器
expanableListview.setGroupIndicator(null);
cartPresenter = new CartPresenter(this);
gson = new Gson();
//1.点击全选:选中/未选中...调用适配器中的方法...myAdapter.setIsCheckAll(true);来设置所有的一级和二级是否选中,计算
check_all.setOnClickListener(this);
text_buy.setOnClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
//请求数据
cartPresenter.getCartData(cartUrl);
}
@Override
public void getSuccessCartData(String json) {
Log.e("-----",json);
if(json == null || "null".equals(json) || json==""){
Toast.makeText(MainActivity.this,"购物车空,去逛逛",Toast.LENGTH_SHORT).show();
}else {
//解析数据
CartBean cartBean = gson.fromJson(json, CartBean.class);
//一个是一级标题的数据
listGroup = cartBean.getData();
//所有子条目的数据
listChilds = new ArrayList<>();
for (int i = 0; i< listGroup.size(); i++){
listChilds.add(listGroup.get(i).getList());
}
//根据网路上是否选中的状态,决定当前组是否选中
for (int i = 0;i<listGroup.size();i++){
if (isAllChildInGroupSelected(i)){
listGroup.get(i).setGroupChecked(true);
}
}
//所有的组是否选中,确定全选是否选中
check_all.setChecked(isAllGroupChecked());
//初始化一下价格和数量
sendPriceAndCount();
//设置适配器
myAdapter = new MyAdapter(MainActivity.this, listGroup, listChilds,handler);
expanableListview.setAdapter(myAdapter);
//展开所有
for (int i = 0; i< listGroup.size(); i++){
expanableListview.expandGroup(i);
}
}
}
/**
* 计算价格数量
*/
private void sendPriceAndCount() {
double price = 0;
int count = 0;
//通过判断二级列表是否勾选,,,,计算价格数量
for (int i=0;i<listGroup.size();i++){
for (int j = 0;j<listChilds.get(i).size();j++){
if (listChilds.get(i).get(j).getSelected() == 1){
price += listChilds.get(i).get(j).getNum() * listChilds.get(i).get(j).getPrice();
count += listChilds.get(i).get(j).getNum();
}
}
}
CountPriceBean countPriceBean = new CountPriceBean(price, count);
//传给activity进行显示
Message msg = Message.obtain();
msg.what = 0;
msg.obj = countPriceBean;
handler.sendMessage(msg);
}
/**
* 所有的一级列表是否选中
*/
private boolean isAllGroupChecked() {
for (int i =0;i<listGroup.size();i++){
if (! listGroup.get(i).isGroupChecked()){//代表一级列表有没选中的
return false;
}
}
return true;
}
/**
* 判断当前组里面所有的子条目是否选中
* @param groupPosition
* @return
*/
private boolean isAllChildInGroupSelected(int groupPosition) {
for (int i= 0;i<listChilds.get(groupPosition).size();i++){
//只要有一个没选中就返回false
if (listChilds.get(groupPosition).get(i).getSelected() ==0){
return false;
}
}
return true;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.check_all://1.点击全选:选中/未选中...调用适配器中的方法...myAdapter.setIsCheckAll(true);来设置所有的一级和二级是否选中,计算
myAdapter.setIsCheckAll(check_all.isChecked());//适配器中的所有checkBox是否选中
break;
case R.id.text_buy:
double price = 0;
int count = 0;
//通过判断二级列表是否勾选,,,,计算价格数量
for (int i=0;i<listGroup.size();i++){
for (int j = 0;j<listChilds.get(i).size();j++){
if (listChilds.get(i).get(j).getSelected() == 1){
price += listChilds.get(i).get(j).getNum() * listChilds.get(i).get(j).getPrice();
count += listChilds.get(i).get(j).getNum();
}
}
}
CountPriceBean countPriceBean = new CountPriceBean(price, count);
Intent intent = new Intent(MainActivity.this, QueRenOrderActivity.class);
intent.putExtra("order",countPriceBean);
startActivity(intent);
break;
}
}
}