二级列表展示

首先自定义view 做一个加减

public class AddJianView extends LinearLayout implements View.OnClickListener {

    private TextView add,jian,sum;

    public AddJianView(Context context) {
        super(context);
    }

    public AddJianView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        View view = View.inflate(context, R.layout.add_jian, this);
        add = view.findViewById(R.id.add);
        jian= view.findViewById(R.id.jian);
        sum= view.findViewById(R.id.sum);
        add.setOnClickListener(this);
        jian.setOnClickListener(this);
    }

    public AddJianView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onClick(View view) {
        String s = sum.getText().toString();
        int sum1 = Integer.parseInt(s);
        switch (view.getId()){
            case R.id.add:
                sum1++;
                sum.setText(sum1+"");
                break;
            case R.id.jian:
                if(sum1==1){
                    Toast.makeText(getContext(), "数量不能小于1", Toast.LENGTH_SHORT).show();
                    return;
                }
                sum1--;
                sum.setText(sum1+"");
                break;
        }
        if(countBack!=null){
            countBack.count(sum1);
        }
    }

    public void setCountBack(CountBack countBack) {
        this.countBack = countBack;
    }

    CountBack countBack;
    public interface CountBack{
        void count(int count);
    }
    public void setConut(String conut){
        sum.setText(conut);
    }
}
public class MainActivity extends BaseActivity implements IMainView<GoodBean> {

    private ExpandableListView expandableListView;
    private MainPresenter mainPresenter;
    private MyAdapter adapter;
    private CheckBox checkBox;

    @Override
    protected int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        expandableListView = findViewById(R.id.expandable);
        checkBox = findViewById(R.id.select_all);
        expandableListView.setIndicatorBounds(expandableListView.getWidth()+1000,expandableListView.getWidth());
    }

    @Override
    protected void initData() {

        mainPresenter = new MainPresenter();
        mainPresenter.setView(this);
        mainPresenter.showData();

    }

    @Override
    public void onSuccgs(GoodBean goodBean) {
        adapter = new MyAdapter(this);
        adapter.setData(goodBean);
        adapter.setCheckBox(checkBox);
        expandableListView.setAdapter(adapter);
        //默认展开数据
        for (int i = 0; i < goodBean.getData().size(); i++) {
            expandableListView.expandGroup(i);
        }
    }

    @Override
    public void Err(String err) {

    }
}

适配器

public class ShopAdapter extends BaseExpandableListAdapter {
    private Context context;
    private ArrayList<ShopBean.DataBean> arr=new ArrayList<>();
    private float sum;

    public ShopAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<ShopBean.DataBean> list){
        if(list!=null){
            arr.addAll(list);
            notifyDataSetChanged();
        }
    }
    //全选
    private CheckBox mqx;
    public void setCheckbox(CheckBox qx){
        this.mqx=qx;
        mqx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox checkBox= (CheckBox) view;
                //全部选中
                selectAll(checkBox.isChecked());
                //计算价格
                sum();
            }
        });
    }
    //全部选中
    private void selectAll(boolean checked) {
        for (int i = 0; i <arr.size() ; i++) {
            ShopBean.DataBean dataBean = arr.get(i);
            dataBean.setCheck(checked);
            for (int j = 0; j <dataBean.getList().size() ; j++) {
                ShopBean.DataBean.ListBean listBean = dataBean.getList().get(j);
                listBean.setCheck(checked);
            }
        }
        notifyDataSetChanged();
    }
    private TextView zongjia;
    public void setText(TextView text){
        this.zongjia=text;
    }


    @Override
    public int getGroupCount() {
        if(arr==null){
            return 0;
        }else{
            return arr.size();
        }

    }

    @Override
    public int getChildrenCount(int i) {
        if(arr.get(i)==null){
            return 0;
        }else{
            return arr.get(i).getList().size();
        }

    }

    @Override
    public Object getGroup(int i) {
        return null;
    }

    @Override
    public Object getChild(int i, int i1) {
        return null;
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view==null){
            view = LayoutInflater.from(context).inflate(R.layout.shop_item, null);
            viewHolder=new ViewHolder();
            viewHolder.checkBox=view.findViewById(R.id.check);
            view.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) view.getTag();
        }
        viewHolder.checkBox.setChecked(arr.get(i).isCheck());
        viewHolder.checkBox.setText(arr.get(i).getSellerName());
        //通过tag传值
        viewHolder.checkBox.setTag(i);
        viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox checkBox= (CheckBox) view;
                //获取 点击商家时的状态
                boolean checked = checkBox.isChecked();
                //获取tag传过来的position
                int groupPosition = Integer.parseInt(checkBox.getTag().toString());
                //通过点击时的状态值  设置点击的商家的checkbox状态值
                arr.get(groupPosition).setCheck(checked);
                //商家选中的话  商家里面的商品 也都跟着选中
                selectGroup(groupPosition,checked);
                //判断 所有商家 是否都选中
                boolean selectAllGroup = isSelectAllGroup();
                //通过上面所获取到的状态值  给全选赋值
                mqx.setChecked(selectAllGroup);
                //计算价格
                sum();
                notifyDataSetChanged();
            }
        });
        return view;
    }
    //遍历 商家里面的所有商品
    private void selectGroup(int groupPosition, boolean checked) {
        for (int i = 0; i <arr.get(groupPosition).getList().size() ; i++) {
            ShopBean.DataBean.ListBean listBean = arr.get(groupPosition).getList().get(i);
            listBean.setCheck(checked);
        }
    }

    static class ViewHolder{
        CheckBox checkBox;
    }
    private void sum(){
        sum=0;
        for (int i = 0; i < arr.size(); i++) {
            List<ShopBean.DataBean.ListBean> list = arr.get(i).getList();
            for (int j = 0; j <list.size() ; j++) {
                ShopBean.DataBean.ListBean listBean = list.get(j);
                if(listBean.isCheck()==true){
                    float price = Float.parseFloat(listBean.getPrice());
                    int count = Integer.parseInt(listBean.getNum());
                    float sum1=price*count;
                    sum +=sum1;
                }
            }

        }
        zongjia.setText("总价:¥"+sum);
    }
    @Override
    public View getChildView(final int i,final int i1, boolean b, View view, ViewGroup viewGroup) {
        ViewHolder1 vh;
        if(view==null){
            view = LayoutInflater.from(context).inflate(R.layout.shop_two, null);
            vh=new ViewHolder1();
            vh.checkBox=view.findViewById(R.id.check);
            vh.name=view.findViewById(R.id.name);
            vh.price=view.findViewById(R.id.price);
            vh.num=view.findViewById(R.id.addview);
            vh.img=view.findViewById(R.id.img);
            view.setTag(vh);
        }else{
            vh= (ViewHolder1) view.getTag();
        }
        vh.img.setImageURI(Uri.parse(arr.get(i).getList().get(i1).getImages()));
        //自定义view里面的一个方法 传值
        vh.num.setConut(arr.get(i).getList().get(i1).getNum());
        vh.price.setText("¥:"+arr.get(i).getList().get(i1).getPrice());
        vh.name.setText(arr.get(i).getList().get(i1).getTitle());
        //价格联动
        vh.num.setCountBack(new AddJianView.CountBack() {
            @Override
            public void count(int count) {
                arr.get(i).getList().get(i1).setNum(count+"");
                //计算价格
                sum();
            }
        });
        //每个checkbox设置成状态 此时都为false
        vh.checkBox.setChecked(arr.get(i).getList().get(i1).isCheck());
        //通过Tag  把position传过去
        vh.checkBox.setTag(i+"#"+i1);
        //点击事件
        vh.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //此时的View 就是我们的CheckBox
                CheckBox checkBox= (CheckBox) view;
                //获取position
                String tag = (String) checkBox.getTag();
                //转换成int类型
                int groupPosition = Integer.parseInt(tag.split("#")[0]);
                int childPosition = Integer.parseInt(tag.split("#")[1]);
                //获取点击的这个checkbox
                ShopBean.DataBean.ListBean listBean = arr.get(groupPosition).getList().get(childPosition);
                //然后赋状态值
                listBean.setCheck(checkBox.isChecked());
                //判断 商家里面所有的商品 是否都选中
                boolean selectGruop = isSelectGruop(groupPosition);
                //通过上面的 返回值  设置商家是否选中
                arr.get(groupPosition).setCheck(selectGruop);
                //判断 所有的商家 是否都选中
                boolean selectAllGroup = isSelectAllGroup();
                //通过上面的返回值 设置全选是否选中
                mqx.setChecked(selectAllGroup);
                //计算价格
                sum();
                notifyDataSetChanged();
            }
        });
        return view;
    }
    //判断所有的商家 方法
    private boolean isSelectAllGroup() {
        for (int i = 0; i <arr.size() ; i++) {
            ShopBean.DataBean dataBean = arr.get(i);
            boolean ischeck = dataBean.isCheck();
            if(!ischeck){
                return false;
            }
        }
        return true;
    }
    //判断商家里面所有的商品 是否都选中
    private boolean isSelectGruop(int gruopPosition){
        for (int i = 0; i <arr.get(gruopPosition).getList().size() ; i++) {
            ShopBean.DataBean.ListBean listBean = arr.get(gruopPosition).getList().get(i);
            boolean check = listBean.isCheck();
            if(!check){
                return false;
            }
        }
        return true;
    }
    static class ViewHolder1{
        CheckBox checkBox;
        SimpleDraweeView img;
        TextView name,price;
        AddJianView num;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}

布局

<ExpandableListView
        android:id="@+id/expandable"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        ></ExpandableListView>
    <LinearLayout
        android:id="@+id/linear_bottom"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="#f00"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/select_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" />

        <TextView
            android:id="@+id/price_all"
            android:textColor="#fff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textSize="20sp"
            android:text="¥0" />
    </LinearLayout>

子布局(解决了 复选框和点击事件的冲突)

<CheckBox
        android:id="@+id/checkbox_group"
        android:focusable="false"
        android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/group_name"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:textSize="20sp"
        android:layout_height="wrap_content" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值