二级购物车自定义demo

HttpUtils   net包下的

public class HttpUtils {
    private static volatile HttpUtils httpUtils;
    private final OkHttpClient client;
    private HttpUtils(){
        client=new OkHttpClient.Builder().build();
    }
    public static HttpUtils getHttpUtils(){
        if(httpUtils == null){
            synchronized (HttpUtils.class){
                if(httpUtils == null){
                    httpUtils=new HttpUtils();
                }
            }
        }
        return httpUtils;
    }
    public void doGet(String url, Callback callback){
        Request request=new Request.Builder().url(url).build();
        client.newCall(request).enqueue(callback);
    }
}
// App
public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(configuration);
    }
}
//Api
public class Api {
    public static final String url = "http://120.27.23.105/product/getCarts?uid=100";

}

//OnNetListenter
public interface OnNetListenter<T> {
    public void onSueecss(T t);
    public  void onFailure(Exception e);
}
//model层接口
public interface IShopModel {
    public void getShop(OnNetListenter<ShopBean> onNetListener);
}
//model层继承接口
public class ShopModel implements IShopModel {
    private Handler handler=new Handler(Looper.getMainLooper());
    @Override
    public void getShop(final OnNetListenter<ShopBean> onNetListenter) {
         HttpUtils.getHttpUtils().doGet(Api.url, new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                      onNetListenter.onFailure(e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                 String string=response.body().string();
                final ShopBean shopBean=new Gson().fromJson(string,ShopBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                      onNetListenter.onSueecss(shopBean);
                    }
                });
            }
        });
    }
}
//presenter层
public class ShopPresenter {
    private IShopModel iShopModel;
    private ShopActivity shopActivity;
    public ShopPresenter(ShopActivity shopActivity){
        iShopModel=new ShopModel();
        this.shopActivity=shopActivity;
    }
    public void getShop(){
        iShopModel.getShop(new OnNetListenter<ShopBean>() {
            @Override
            public void onSueecss(ShopBean shopBean) {
                List<ShopBean.DataBean> dataBean = shopBean.getData();
                List<List<ShopBean.DataBean.ListBean>> childList=
                        new ArrayList<List<ShopBean.DataBean.ListBean>>();
                   for(int i=0;i<dataBean.size();i++){
                       List<ShopBean.DataBean.ListBean> listBeen= dataBean.get(i).getList();
                       childList.add(listBeen);
                   }
                shopActivity.showList(dataBean,childList);
            }

            @Override
            public void onFailure(Exception e) {
                 e.printStackTrace();
            }
        });
    }
}
view层接口
public interface ShopActivity {
    public void showList(List<ShopBean.DataBean> groupList,List<List<ShopBean.DataBean.ListBean>> childList);
}
view层继承接口

public class MainActivity extends AppCompatActivity implements ShopActivity {

    private ExpandableListView mElv;
    private CheckBox mCheckbox2;
    private TextView mPriceTv;
    private TextView mNumTv;
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        //注册EventBus
        EventBus.getDefault().register(this);
        initView();
        new ShopPresenter(this).getShop();
        mCheckbox2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.changeAllListCbState(mCheckbox2.isChecked());
            }
        });

    }

    private void initView() {
        mElv = (ExpandableListView) findViewById(R.id.elv);
        mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2);
        mPriceTv = (TextView) findViewById(R.id.tv_price);
        mNumTv = (TextView) findViewById(R.id.tv_num);
    }

    @Subscribe
    public void onMessageEvent(MessageEvent event){
        mCheckbox2.setChecked(event.isChecked());
    }
    @Subscribe
    public void onMessageEvent(PriceAndCountEvent event){
        mNumTv.setText("结算("+event.getCount()+")");
        mPriceTv.setText(event.getPrice() + "");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showList(List<ShopBean.DataBean> groupList, List<List<ShopBean.DataBean.ListBean>> childList) {
        adapter=new MyAdapter(this,groupList,childList);
        mElv.setAdapter(adapter);
        mElv.setGroupIndicator(null);
        //默认让其全部展开
        for(int i=0;i<groupList.size();i++){
            mElv.expandGroup(i);
        }
    }
}
 //自定义view页面
 
public class MyView extends LinearLayout {
    private ImageView mDelIv;
    private TextView mNumTv;
    private ImageView mAddIv;

    public MyView(Context context) {
        super(context, null);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.myview, this);
        initView(view);
    }

    private void initView(@NonNull final View itemView) {
        mDelIv = (ImageView) itemView.findViewById(R.id.iv_del);
        mNumTv = (TextView) itemView.findViewById(tv_num);
        mAddIv = (ImageView) itemView.findViewById(R.id.iv_add);
    }
    public void setAddClickListener(OnClickListener onClickListener){
        mAddIv.setOnClickListener(onClickListener);
    }
    public void setDelClickListener(OnClickListener onClickListener){
        mDelIv.setOnClickListener(onClickListener);
    }
    public void setNum(String num) {
        mNumTv.setText(num);
    }
    public int getNum() {
        String num = mNumTv.getText().toString();
        return Integer.parseInt(num);
    }
}
//MyAdapter页面
public class MyAdapter extends BaseExpandableListAdapter {
    private List<ShopBean.DataBean> groupList;
    private List<List<ShopBean.DataBean.ListBean>> chlidList;
    private Context context;
    private final LayoutInflater inflater;

    public MyAdapter(Context context, List<ShopBean.DataBean> groupList, List<List<ShopBean.DataBean.ListBean>> chlidList) {
        this.groupList = groupList;
        this.context = context;
        this.chlidList = chlidList;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return chlidList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return chlidList.get(groupPosition).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 isExpanded, View convertView, ViewGroup parent) {
       View view;
        final GroupViewHolder holder;
        if(convertView == null){
            holder=new GroupViewHolder();
            view=inflater.inflate(R.layout.fatheritem,null);
           holder.cbGroup=view.findViewById(R.id.cb_parent);
            holder.tv_number=view.findViewById(R.id.tv_number);
            holder.tv_sign=view.findViewById(R.id.tv_sign);
          view.setTag(holder);
        }else{
            view=convertView;
            holder= (GroupViewHolder) view.getTag();

        }
        final ShopBean.DataBean dataBean = groupList.get(groupPosition);
        holder.cbGroup.setChecked(dataBean.isChecked());
        holder.tv_number.setText(dataBean.getSellerid());
        holder.tv_sign.setText(dataBean.getSellerName());
        holder.cbGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果点击一级列表的选中框
                dataBean.setChecked(holder.cbGroup.isChecked());
                //changeChildCbState改变二级列表checkbox状态
                changeChildCbState(groupPosition,holder.cbGroup.isChecked());
                EventBus.getDefault().post(compute());
               // changeAllCbState改变全选的状态
                // ,isAllGroupCbSelected()一级列表是否全部选中
                changeAllCbState(isAllGroupCbSelected());
                notifyDataSetChanged();
            }


        });
        return view;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view;
        final ChildViewHolder holder2;
        if(convertView == null){
            holder2=new ChildViewHolder();
            view=inflater.inflate(R.layout.childitem,null);
            holder2.cbChild=view.findViewById(R.id.cb_child);
            holder2.tv_tel=view.findViewById(R.id.tv_tel);
            holder2.tv_time=view.findViewById(R.id.tv_time);
            holder2.tv_price = view.findViewById(R.id.tv_pri);
            holder2.tv_del = view.findViewById(R.id.tv_del);
            holder2.iv=view.findViewById(R.id.iv);
            holder2.myView=view.findViewById(R.id.mv);
            view.setTag(holder2);
        }else {
            view=convertView;
            holder2= (ChildViewHolder) view.getTag();
        }
        final ShopBean.DataBean.ListBean listBean = chlidList.get(groupPosition).get(childPosition);
        holder2.cbChild.setChecked(listBean.isChecked());
        holder2.tv_tel.setText(listBean.getTitle());
        holder2.tv_time.setText(listBean.getCreatetime());
        holder2.tv_price.setText(listBean.getPrice()+"");
        holder2.myView.setNum(listBean.getNum()+"");
        //拆分图片url
        String[] split=listBean.getImages().split("\\|");
        ImageLoader.getInstance().displayImage(split[0] ,holder2.iv);
         holder2.cbChild.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //设置该条目对象里的checked属性值
                   listBean.isChecked(holder2.cbChild.isChecked());
                    PriceAndCountEvent priceAndCountEvent=compute();
                 EventBus.getDefault().post(priceAndCountEvent);
                 if(holder2.cbChild.isChecked()){
                     //当前checkbox是选中状态
                    if(isAllChildCbSelected(groupPosition)){
                        changGroupCbState(groupPosition,true);
                        changeAllCbState(isAllGroupCbSelected());
                    }else {
                        changGroupCbState(groupPosition,false);
                        changeAllCbState(isAllGroupCbSelected());;
                    }
                     notifyDataSetChanged();
                 }
             }
         });
          //加
        holder2.myView.setAddClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = holder2.myView.getNum();
                num++;
                listBean.setNum(num);
                if (holder2.cbChild.isChecked()) {
                    EventBus.getDefault().post(compute());
                }
                notifyDataSetChanged();
            }
        });
        //减
        holder2.myView.setDelClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = holder2.myView.getNum();
              if(num == 1){
                return;
              }
                num--;
                listBean.setNum(num);
                if (holder2.cbChild.isChecked()) {
                    EventBus.getDefault().post(compute());
                }
                notifyDataSetChanged();
            }
        });
        holder2.tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<ShopBean.DataBean.ListBean> myBeans = chlidList.get(groupPosition);
                ShopBean.DataBean.ListBean remove = myBeans.remove(childPosition);
                  if(myBeans.size() ==0){
                      chlidList.remove(groupPosition);
                      groupList.remove(groupPosition);
                  }
                EventBus.getDefault().post(compute());
                notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
    class GroupViewHolder{
        CheckBox cbGroup;
        TextView tv_number;
        TextView tv_sign;
    }
    class ChildViewHolder {
        CheckBox cbChild;
        ImageView iv;
        TextView tv_tel;
        TextView tv_time;
        TextView tv_price;
        TextView tv_del;
        private MyView myView;
    }
    //计算价格
    private PriceAndCountEvent compute(){
      int price=0;
        int count=0;
        for(int i=0;i<chlidList.size();i++){
            List<ShopBean.DataBean.ListBean> beanList = chlidList.get(i);
            for(int j=0;j<beanList.size();j++ ){
                ShopBean.DataBean.ListBean listBean = beanList.get(j);
               if(listBean.isChecked()){
                   price+=listBean.getPrice()*listBean.getNum();
                   count+=listBean.getNum();

               }
            }
        }
        PriceAndCountEvent priceAndCountEvent=new PriceAndCountEvent();
        priceAndCountEvent.setPrice(price);
        priceAndCountEvent.setCount(count);
        return priceAndCountEvent;
    }
    private boolean  isAllChildCbSelected( int groupPosition){
        List<ShopBean.DataBean.ListBean> been = chlidList.get(groupPosition);
        for(int i=0;i<been.size();i++){
            ShopBean.DataBean.ListBean bean = been.get(i);
            if(!bean.isChecked()){
                return  false;
            }
        }

        return  true;
    }
    /**
     * 判断一级列表是否全部选中
     *
     * @return
     */
    private boolean isAllGroupCbSelected(){
        for(int i=0;i<groupList.size();i++){
            ShopBean.DataBean dataBea = groupList.get(i);
            if(! dataBea.isChecked()){
                return false;
            }
        }
        return  true;
    }
    //改变一级列表
    private void changGroupCbState(int groupPosition,boolean flag){
        ShopBean.DataBean dataBeans = groupList.get(groupPosition);
        dataBeans.setChecked(flag);
    }
    //改变二级列表
    private void changeChildCbState(int groupPosition,boolean flag){
        List<ShopBean.DataBean.ListBean> lists = chlidList.get(groupPosition);
        for(int i=0;i<lists.size();i++){
            ShopBean.DataBean.ListBean wolists = lists.get(i);
            wolists.setChecked(flag);

        }
    }
    //改变全选的状态值
    private void changeAllCbState(boolean flag){
        MessageEvent messageEvent=new MessageEvent();
        messageEvent.setChecked(flag);
        EventBus.getDefault().post(messageEvent);
    }
    /**
     * 设置全选、反选
     *
     * @param flag
     */
    public void changeAllListCbState(boolean flag) {
        for (int i = 0; i < groupList.size(); i++) {
            changGroupCbState(i, flag);
            changeChildCbState(i, flag);
        }
        EventBus.getDefault().post(compute());
        notifyDataSetChanged();
    }
}
//eventbusevent包
public class MessageEvent {
    private boolean checked;

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}
public class PriceAndCountEvent {
    private int price;
    private int count;

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
08-10
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值