activity,实现v层返回数据,适配器的点击一级列表和二级列表的监听接口,以及点击+商品和-商品的方法
public class MainActivity extends AppCompatActivity implements IMainView, MyExpandAdapter.ModifyGoodsItemNumberListener, MyExpandAdapter.CheckGroupItemListener {
private ExpandableListView expandList;
private MyExpandAdapter adapter;
private IShooppPrensenter iShooppPrensenter;
//
private List<ShoppCarBean.DataBean> list;
private CheckBox btnCheckAll;
//购买商品的总数量
private int totalNum = 0;
//购买商品的总价
private double totalPrice= 0.00; //15.55 15 0.55亿
private TextView tvTotalPrice;
private TextView btnAmount;
private TextView btnEditor;
//默认是false
private boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//隐藏actionbar
getSupportActionBar().hide();
expandList = findViewById(R.id.expandList);
//购物车底部栏的全选框
btnCheckAll = findViewById(R.id.btnCheckAll);
//合计,计算总价
tvTotalPrice = findViewById(R.id.tvTotalPrice);
//结算按钮
btnAmount = findViewById(R.id.btnAmount);
//找到编辑控件
btnEditor = findViewById(R.id.btnEditor);
//去除默认指示器
expandList.setGroupIndicator(null);
//设置适配器
adapter = new MyExpandAdapter(this);
expandList.setAdapter(adapter);
//获取购物车信息
iShooppPrensenter = new ShooppPresenter(this);
iShooppPrensenter.getCarInfo("4582");
//设置商品加减的监听
adapter.setModifyGoodsItemNumberListener(this);
//设置商家以及商品是否被选中的监听
adapter.setCheckGroupItemListener(this);
btnCheckAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isChoosedAll(((CheckBox)view).isChecked());
//计算商品总价
statisticsPrice();
}
});
//编辑
btnEditor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!flag){//编辑 -> 完成\
flag = true;
btnEditor.setText("完成");
adapter.showDeleteButton(flag);
}else{
flag = false;
btnEditor.setText("编辑");
adapter.showDeleteButton(flag);
}
}
});
}
//二级列表默认展开
private void defaultExpand(){
for (int i = 0; i < adapter.getGroupCount();++i){
expandList.expandGroup(i);
}
}
@Override
public void onSuccess(List<ShoppCarBean.DataBean> data) {
this.list = data;
//设置数据
adapter.setList(list);
defaultExpand();
}
@Override
public void onFailed(String msg) {
}
@Override
public void doIncrease(int groupPosition, int childPosition, View view) {
//Toast.makeText(this, "用户点击了商品添加操作", Toast.LENGTH_SHORT).show();
ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition);
//取出当前的商品数量
int currentNum = listBean.getNum();
//商品++
currentNum++;
//将商品数量设置javabean上
listBean.setNum(currentNum);
//刷新适配器
adapter.notifyDataSetChanged();
//计算商品价格
statisticsPrice();
}
@Override
public void doDecrease(int groupPosition, int childPosition, View view) {
//Toast.makeText(this, "用户点击了商品减少操作", Toast.LENGTH_SHORT).show();
ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition);
//取出当前的商品数量
int currentNum = listBean.getNum();
//直接结束这个方法
if(currentNum == 1){
return;
}
currentNum--;
listBean.setNum(currentNum);
//刷新适配器
adapter.notifyDataSetChanged();
//计算商品价格
statisticsPrice();
}
//商家checkbox
@Override
public void checkGroupItem(int groupPosition, boolean isChecked) {
//Toast.makeText(this, "商家", Toast.LENGTH_SHORT).show();
//商家javabean
ShoppCarBean.DataBean dataBean = list.get(groupPosition);
dataBean.setGroupChoosed(isChecked);
//遍历商家里面的商品,将其置为选中状态
for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()){
listBean.setChildChoosed(isChecked);
}
//底部结算那个checkbox状态(全选)
if(isCheckAll()){
btnCheckAll.setChecked(true);
}else{
btnCheckAll.setChecked(false);
}
//刷新适配器
adapter.notifyDataSetChanged();
//计算价格
statisticsPrice();
}
//商品的checkbox
@Override
public void checkChildItem(int groupPosition, int childPosition, boolean isChecked) {
//Toast.makeText(this, "商品", Toast.LENGTH_SHORT).show();
//ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition);
ShoppCarBean.DataBean dataBean = list.get(groupPosition);//商家那一层
List<ShoppCarBean.DataBean.ListBean> listBeans = dataBean.getList();
ShoppCarBean.DataBean.ListBean listBean = listBeans.get(childPosition);
//你点击商家的商品条目将其选中状态记录
listBean.setChildChoosed(isChecked);
//检测商家里面的每一个商品是否被选中,如果被选中,返回boolean
boolean result = isGoodsCheckAll(groupPosition);
if(result){
dataBean.setGroupChoosed(result);
}else{
dataBean.setGroupChoosed(result);
}
//底部结算那个checkbox状态(全选)
if(isCheckAll()){
btnCheckAll.setChecked(true);
}else{
btnCheckAll.setChecked(false);
}
//刷新适配器
adapter.notifyDataSetChanged();
//计算总价
statisticsPrice();
}
/**
* 检测某个商家的商品是否都选中,如果都选中的话,商家CheckBox应该是选中状态
* @param groupPosition
* @return
*/
private boolean isGoodsCheckAll(int groupPosition){
List<ShoppCarBean.DataBean.ListBean> listBeans = this.list.get(groupPosition).getList();
//遍历某一个商家的每个商品是否被选中
for (ShoppCarBean.DataBean.ListBean listBean : listBeans){
if(listBean.isChildChoosed()){//是选中状态
continue;
}else{
return false;
}
}
return true;
}
//购物车商品是否全部选中
private boolean isCheckAll(){
for (ShoppCarBean.DataBean dataBean : list){
if(!dataBean.isGroupChoosed()){
return false;
}
}
return true;
}
//全选与反选
private void isChoosedAll(boolean isChecked){
for (ShoppCarBean.DataBean dataBean : list){
dataBean.setGroupChoosed(isChecked);
for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()){
listBean.setChildChoosed(isChecked);
}
}
//刷新适配器
adapter.notifyDataSetChanged();
}
/**
* 计算你所选中的商品总价
*/
private void statisticsPrice(){
//初始化值
totalNum = 0;
totalPrice = 0.00;
for (ShoppCarBean.DataBean dataBean : list){
for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()){
if(listBean.isChildChoosed()){
totalNum++;
System.out.println("number : "+totalNum);
totalPrice += listBean.getNum()*listBean.getPrice();
}
}
}
//设置文本信息 合计、结算的商品数量
tvTotalPrice.setText("合计:¥"+totalPrice);
btnAmount.setText("结算("+totalNum+")");
}
}
二级列表适配器,有两个接口被activity实现
public class MyExpandAdapter extends BaseExpandableListAdapter {
private List<ShoppCarBean.DataBean> data;
private Context context;
//商品加减的一个接口变量
private ModifyGoodsItemNumberListener modifyGoodsItemNumberListener;
private CheckGroupItemListener checkGroupItemListener;
//接收是否处于编辑状态的一个boolean值
private boolean isEditor;
public MyExpandAdapter(Context context){
this.context = context;
}
//设置数据
public void setList(List<ShoppCarBean.DataBean> data){
this.data = data;
notifyDataSetChanged();
}
//商家以及商品是否被选中的一个监听
public void setCheckGroupItemListener(CheckGroupItemListener checkGroupItemListener){
this.checkGroupItemListener = checkGroupItemListener;
}
//设置商品的加减监听
public void setModifyGoodsItemNumberListener(ModifyGoodsItemNumberListener modifyGoodsItemNumberListener){
this.modifyGoodsItemNumberListener = modifyGoodsItemNumberListener;
}
//是否显示删除按钮
public void showDeleteButton(boolean isEditor){
this.isEditor = isEditor;
//刷新适配器
notifyDataSetChanged();
}
@Override
public int getGroupCount() {
return data != null ? data.size() : 0;
}
@Override
public int getChildrenCount(int i) {
return data != null && data.get(i).getList() != null ? data.get(i).getList().size() : 0;
}
@Override
public Object getGroup(int groupPosition) {
return data.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return data.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 b, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.layout_group_item,parent,false);
}
//查找控件
CheckBox ck_group_choosed = convertView.findViewById(R.id.ck_group_choosed);
//设置商家checkbox复选框的状态
if(data.get(groupPosition).isGroupChoosed()){
ck_group_choosed.setChecked(true);
}else{
ck_group_choosed.setChecked(false);
}
//ck_group_choosed.setChan
ck_group_choosed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkGroupItemListener.checkGroupItem(groupPosition,((CheckBox)view).isChecked());
}
});
//赋值
ck_group_choosed.setText(data.get(groupPosition).getSellerName());
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean b, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.layout_child_item,parent,false);
}
//商品选择
CheckBox ck_child_choosed = convertView.findViewById(R.id.ck_child_choose);
//商品图片
ImageView iv_show_pic = convertView.findViewById(R.id.iv_show_pic);
//商品主标题
TextView tv_commodity_name = convertView.findViewById(R.id.tv_commodity_name);
//商品副标题
TextView tv_commodity_attr = convertView.findViewById(R.id.tv_commodity_attr);
//商品价格
TextView tv_commodity_price = convertView.findViewById(R.id.tv_commodity_price);
//商品数量
TextView tv_commodity_num = convertView.findViewById(R.id.tv_commodity_num);
//商品减
TextView iv_sub = convertView.findViewById(R.id.iv_sub);
//商品加减中的数量变化
final TextView tv_commodity_show_num = convertView.findViewById(R.id.tv_commodity_show_num);
//商品加
TextView iv_add = convertView.findViewById(R.id.iv_add);
//删除按钮
Button btn_commodity_delete = convertView.findViewById(R.id.btn_commodity_delete);
//设置文本信息
tv_commodity_name.setText(data.get(groupPosition).getList().get(childPosition).getTitle());
tv_commodity_attr.setText(data.get(groupPosition).getList().get(childPosition).getSubhead());
tv_commodity_price.setText("¥"+data.get(groupPosition).getList().get(childPosition).getPrice());
tv_commodity_num.setText("x"+data.get(groupPosition).getList().get(childPosition).getNum());
tv_commodity_show_num.setText(data.get(groupPosition).getList().get(childPosition).getNum()+"");
//分割图片地址
String images = data.get(groupPosition).getList().get(childPosition).getImages();
String[] urls = images.split("\\|");
//加载商品图片
Glide.with(context)
.load(urls[0])
.crossFade()
.into(iv_show_pic);
//商品加
iv_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
modifyGoodsItemNumberListener.doIncrease(groupPosition,childPosition,tv_commodity_show_num);
}
});
//设置商品加减的按钮
//商品减
iv_sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
modifyGoodsItemNumberListener.doDecrease(groupPosition,childPosition,tv_commodity_show_num);
}
});
//商品复选框是否被选中
ck_child_choosed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//isChecked false true
checkGroupItemListener.checkChildItem(groupPosition,childPosition,((CheckBox)view).isChecked());
}
});
//处理商品的选中状态
if(data.get(groupPosition).getList().get(childPosition).isChildChoosed()){
ck_child_choosed.setChecked(true);
}else{
ck_child_choosed.setChecked(false);
}
//控制删除按钮的隐藏与显示
if(isEditor){
btn_commodity_delete.setVisibility(View.VISIBLE);
}else{
btn_commodity_delete.setVisibility(View.GONE);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
/**
* 商家的复选框以及商品的复选框是否被选中的接口
*/
/**
* 商家复选框以及商品复选框是否被选中接口
*/
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);
}
}