Adapter

public class ShopAdapter extends  ExpandableListAdapter {
    private List<Shop.DataBean> shopData;
    private double price;
    private int num;

    public ShopAdapter(List<Shop.DataBean> shopData) {
        this.shopData = shopData;
    }

    //1.有多少按钮
    @Override
    public int getGroupCount() {
        return shopData.size();
    }

    //2.一个组里有多少子条目
    @Override
    public int getChildrenCount(int groupPosition) {
        return shopData.get(groupPosition).getList().size();
    }



    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        //P1.获取组的下标
        Shop.DataBean dataBean = shopData.get(groupPosition);
        ParentHolder parentHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.parent_item, null);
            parentHolder = new ParentHolder(convertView);
            convertView.setTag(parentHolder);
        } else {
            parentHolder = (ParentHolder) convertView.getTag();
        }
        //P2.获取商家名称
        parentHolder.textParent.setText(dataBean.getSellerName());
        return convertView;
    }

    ////////////C.子布局///////////
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        Shop.DataBean dataBean = shopData.get(groupPosition);
        List<Shop.DataBean.ListBean> list = dataBean.getList();
        //C1.拿到list集合里具体商品
        Shop.DataBean.ListBean listBean = list.get(childPosition);
        ChildHolder childHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.child_item, null);
            //   convertView = View.inflate(parent.getContext(), R.layout.child_item, null);
            childHolder = new ChildHolder(convertView);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
//C2.截取图片picasso
        String images = listBean.getImages();
        String[] strings = images.split(\"!\");
        Picasso.with(parent.getContext()).load(strings[0]).into(childHolder.imageChild);
        //C.获取商品名称
        childHolder.childText.setText(listBean.get ());
        //C.单价
        childHolder.childPrice.setText(listBean.getPrice() + \"\");
        //C.设置子条目复选框是否选中
        childHolder.boxChild.setChecked(listBean.getSelected() == 1);
        //C.设置加减器内部数量
        //childHolder.addSub.setNumber(listBean.getNum());

        //C3.设置商品checkbox的点击事件,接口回调
        childHolder.boxChild.set Listener(new View. Listener() {
            @Override
            public void  (View v) {

            }
        });

        return convertView;
    }





    public static class ParentHolder {

        private final CheckBox boxParent;
        private final TextView textParent;

        public ParentHolder(View rootView) {
            boxParent = rootView.findViewById(R.id.box_parent);
            textParent = rootView.findViewById(R.id.text_parent);
        }
    }

    public static class ChildHolder {

        private final CheckBox boxChild;
        private final ImageView imageChild;
        private final TextView childText;
        private final TextView childPrice;
        private final MyAddSub addSub;

        public ChildHolder(View rootView) {
            boxChild = rootView.findViewById(R.id.child_box);
            imageChild = rootView.findViewById(R.id.child_image);
            childText = rootView.findViewById(R.id.child_text);
            childPrice = rootView.findViewById(R.id.child_price);
            addSub = rootView.findViewById(R.id.add_sub);
        }
    }








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

    @Override
    public   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 false;
    }
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}

Presenter

public class ShopPresenter {
public void ShopData(String url) {
    DataUtils.getInstance().doGet(url, new DataUtils.OKhttpInterface() {
        @Override
        public void Failed(Exception e) {
            mOnshopInterface.failed(e);
        }

        @Override
        public void Success(String data) {
            Shop shop = new Gson().fromJson(data, Shop.class);
            List<Shop.DataBean> shopData = shop.getData();
            mOnshopInterface.success(shopData);
        }
    });
}

public interface onShopInterface {
    void failed(Exception e);

    void success(List<Shop.DataBean> shopData);
}

private onShopInterface mOnshopInterface;

public void setOnshopInterface(onShopInterface onshopInterface) {
    mOnshopInterface = onshopInterface;
}

}

Utils

	public class DataUtils {

        private final Handler handler;
        private final OkHttpClient httpClient;
        private static DataUtils mHttpUtils;

        private DataUtils(){
            //创建主线程的handler
            handler = new Handler(Looper.getMainLooper());
            httpClient = new OkHttpClient.Builder()
                    .connectTimeout(500, TimeUnit.MILLISECONDS)
                    .readTimeout(5000, TimeUnit.MILLISECONDS)
                    .writeTimeout(5000, TimeUnit.MILLISECONDS)
                    .build();
        }
        //创建接口
        public interface OKhttpInterface{
            void Failed(Exception e);
            void Success(String data);
        }
        public void doGet(String url, final OKhttpInterface mOkhttpInterface){
            Request request = new Request.Builder()
                    .get()
                    .url(url)
                    .build();
            httpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, final IOException e) {
                    if (mOkhttpInterface!=null){
                        //切换到主线程
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                mOkhttpInterface.Failed(e);
                            }
                        });
                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response!=null&&response.isSuccessful()){
                        final String data = response.body().string();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (mOkhttpInterface!=null){
                                    mOkhttpInterface.Success(data);
                                    return;
                                }
                            }
                        });
                    }
                }
            });
        }
        //单例模式
        public static DataUtils getInstance(){
            if (mHttpUtils==null){
                synchronized (DataUtils.class){
                    if (mHttpUtils==null){
                        return mHttpUtils=new DataUtils();
                    }
                }
            }
            return mHttpUtils;
        }
}

MainActivity

public class MainActivity extends AppCompatActivity implements View. Listener{

    private ExpandableListView ex_listView;
    private CheckBox all_box;
    private TextView all_price;
    private Button jiesuan_btn;
    private ShopAdapter shopAdapter;
    private String url = \"http://www.zhaoapi.cn/product/getCarts?uid=71\";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //A.获取控件
        initView();
        //B.获取数据
        initData();
    }

    private void initData() {
        ShopPresenter shopPresenter = new ShopPresenter();
        shopPresenter.ShopData(url);
        shopPresenter.setOnshopInterface(new ShopPresenter.onShopInterface(){

            @Override
            public void failed(Exception e) {

            }

            @Override
            public void success(List<Shop.DataBean> shopData) {
                //B1.获取适配器
                shopAdapter = new ShopAdapter(shopData);


                //B3.设置adapter对象
                ex_listView.setAdapter(shopAdapter);
                for (int i=0;i<shopData.size();i++){
                    ex_listView.expandGroup(i);
                }
            }
        });
    }

    private void initView() {
        ex_listView = findViewById(R.id.ex_listView);
        all_box = findViewById(R.id.all_box);
        all_price = findViewById(R.id.all_price);
        jiesuan_btn = findViewById(R.id.jiesuan_btn);
        all_box.set Listener(this);
    }

    @Override
    public void  (View view) {

    }
}

MyAddSub

	public class MyAddSub extends LinearLayout implements View. Listener {
	
	    private TextView addText;
	    private TextView subText;
	    private TextView numText;
	    private int number = 1;
	
	    public MyAddSub(Context context) {
	        this(context, null);
	    }
	
	    public MyAddSub(Context context, AttributeSet attrs) {
	        this(context, attrs, 0);
	    }
	
	    public MyAddSub(Context context, AttributeSet attrs, int defStyleAttr) {
	        super(context, attrs, defStyleAttr);
	        View view = inflate(context, R.layout.add_sub_item, this);
	        addText = view.findViewById(R.id.add_text);
	        subText = view.findViewById(R.id.sub_text);
	        numText = view.findViewById(R.id.num_text);
	
	        addText.set Listener(this);
	        subText.set Listener(this);
	
	    }
	
	    @Override
	    public void  (View v) {
	        switch (v.getId()) {
	            case R.id.add_text:
	                ++number;
	                numText.setText(number + \"\");
	                if (mOnNumberChangeInterface != null) {
	                    mOnNumberChangeInterface.onNumberChang(number);
	                }
	                break;
	            case R.id.sub_text:
	                //判断如果数字比1大就减,比1小就吐司
	                if (number > 1) {
	                    --number;
	                    numText.setText(number + \"\");
	                    if (mOnNumberChangeInterface != null) {
	                        mOnNumberChangeInterface.onNumberChang(number);
	                    }
	                } else {
	                    Toast.makeText(getContext(), \"不能再少了\", Toast.LENGTH_SHORT).show();
	                }
	                break;
	        }
	    }
	
	    public int getNumber() {
	        return number;
	    }
	
	    public void setNumber(int number) {
	        this.number = number;
	        numText.setText(number + \"\");
	    }
	
	    //创建接口
	    public interface OnNumberChangeInterface {
	        void onNumberChang(int num);
	    }
	
	    //声明接口名
	    private OnNumberChangeInterface mOnNumberChangeInterface;
	
	    //暴露方法
	    public void setOnNumberChangeInterface(OnNumberChangeInterface onNumberChangeInterface) {
	        mOnNumberChangeInterface = onNumberChangeInterface;
	    }
	}

布局

	<LinearLayout  ns:android=\"http://schemas.android.com/apk/res/android\"
	     ns:app=\"http://schemas.android.com/apk/res-auto\"
	     ns:tools=\"http://schemas.android.com/tools\"
	    android:layout_width=\"match_parent\"
	    android:layout_height=\"match_parent\"
	    android:orientation=\"vertical\"
	    tools:context=\".MainActivity\">
	    <ExpandableListView
	        android:id=\"@+id/ex_listView\"
	        android:layout_width=\"match_parent\"
	        android:layout_height=\"0dp\"
	        android:layout_weight=\"10\" />
	
	    <LinearLayout
	        android:layout_width=\"match_parent\"
	        android:layout_height=\"0dp\"
	        android:layout_weight=\"1\"
	        android:orientation=\"horizontal\">
	
	        <CheckBox
	            android:id=\"@+id/all_box\"
	            android:layout_width=\"wrap_content\"
	            android:layout_height=\"wrap_content\"
	            android:layout_weight=\"1\"
	            android:text=\"全选\" />
	
	        <TextView
	            android:id=\"@+id/all_price\"
	            android:layout_width=\"wrap_content\"
	            android:layout_height=\"wrap_content\"
	            android:layout_weight=\"1\"
	            android:text=\"总价:¥0.00\"
	            android:textSize=\"18sp\" />
	
	        <Button
	            android:id=\"@+id/jiesuan_btn\"
	            android:layout_width=\"wrap_content\"
	            android:layout_height=\"wrap_content\"
	            android:layout_weight=\"1\"
	            android:text=\"结算\" />
	    </LinearLayout>
	
	</LinearLayout>

z主

LinearLayout  ns:android=\"http://schemas.android.com/apk/res/android\"
    android:layout_width=\"80dp\"
    android:layout_height=\"40dp\"
    android:orientation=\"horizontal\">

<TextView
    android:id=\"@+id/sub_text\"
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:layout_weight=\"1\"
    android:text=\"-\"
    android:textSize=\"20sp\" />

<TextView
    android:id=\"@+id/num_text\"
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:layout_weight=\"1\"
    android:text=\"1\"
    android:textSize=\"20sp\" />

<TextView
    android:id=\"@+id/add_text\"
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:layout_weight=\"1\"
    android:text=\"+\"
    android:textSize=\"20sp\" />

LinearLayout  ns:android=\"http://schemas.android.com/apk/res/android\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"120dp\">

<LinearLayout
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:orientation=\"horizontal\">

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

    <ImageView
        android:id=\"@+id/child_image\"
        android:layout_width=\"100dp\"
        android:layout_height=\"100dp\"
        android:padding=\"10dp\"
        android:src=\"@mipmap/ic_launcher\" />


</LinearLayout>

<LinearLayout
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:layout_weight=\"1\"
    android:orientation=\"vertical\">

    <TextView
        android:id=\"@+id/child_text\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:paddingTop=\"15dp\"
        android:text=\"jhsjhdsk\"
        android:textSize=\"17sp\" />

    <TextView
        android:id=\"@+id/child_price\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:paddingTop=\"10dp\"
        android:text=\"¥0.0\" />


</LinearLayout>
<com.bwei.yk01.MyAddSub
    android:id=\"@+id/add_sub\"
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:layout_marginTop=\"10dp\"
    android:gravity=\"center_vertical\"/>

复选框

LinearLayout  ns:android=\"http://schemas.android.com/apk/res/android\"
    android:layout_width=\"80dp\"
    android:layout_height=\"40dp\"
    android:orientation=\"horizontal\">

<CheckBox
    android:id=\"@+id/box_parent\"
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:layout_gravity=\"center\" />

<TextView
    android:id=\"@+id/text_parent\"
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:layout_gravity=\"center\"
    android:text=\"商家\"
    android:textSize=\"20sp\" />
收藏 打印