二级购物车自定义demo

来源:互联网 发布:sql查询结果导出excel 编辑:程序博客网 时间:2024/05/18 03:58

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;    }}



原创粉丝点击