android:简单的二级购物车

来源:互联网 发布:matlab符号矩阵行列式 编辑:程序博客网 时间:2024/05/22 01:36
**第一步,导入相关依赖,如    compile 'com.squareup.okhttp3:okhttp:3.9.0'    compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'    compile 'com.google.code.gson:gson:2.8.2'    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'    compile 'org.greenrobot:eventbus:3.1.1'    **第二步,添加权限,如    <uses-permission android:name="android.permission.INTERNET">       </uses-permission>    ******第三步,上代码,****net包(Api,HttpUtils,OnNetListener)****Api**public interface Api {    public static final String url = "http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=evaluation";}**HttpUtils**public class HttpUtils {    private static volatile HttpUtils httpUtils;    private final OkHttpClient client;    private HttpUtils() {        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();        logging.setLevel(HttpLoggingInterceptor.Level.BODY);        client = new OkHttpClient.Builder()                .addInterceptor(logging)                .build();    }    public static HttpUtils getHttpUtils() {        if (httpUtils == null) {            synchronized (HttpUtils.class) {                if (httpUtils == null) {                    httpUtils = new HttpUtils();                }            }        }        return httpUtils;    }    /**     * GET请求     *     * @param url     * @param callback     */    public void doGet(String url, Callback callback) {        Request request = new Request.Builder().url(url).build();        client.newCall(request).enqueue(callback);    }}**OnNetListener**public interface OnNetListener<T> {    public void onSuccess(T t);    public void onFailure(Exception e);}**bean包(GoosBean)****自己封装吧,记得可以手动添加属性封装****adapter包(MyAdapter )**public class MyAdapter extends BaseExpandableListAdapter {    private Context context;    private List<GoosBean.DataBean> groupList;    private List<List<GoosBean.DataBean.DatasBean>> chidList;    private  LayoutInflater inflater;    public MyAdapter(Context context, List<List<GoosBean.DataBean.DatasBean>> chidList, List<GoosBean.DataBean> groupList) {        this.context = context;        this.chidList = chidList;        this.groupList = groupList;        inflater = LayoutInflater.from(context);    }    @Override    public int getGroupCount() {        return groupList.size();    }    @Override    public int getChildrenCount(int groupPosition) {        return chidList.get(groupPosition).size();    }    @Override    public Object getGroup(int groupPosition) {        return groupList.get(groupPosition);    }    @Override    public Object getChild(int groupPosition, int childPosition) {        return chidList.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.item_parent_market, null);            holder.cbGroup = (CheckBox) view.findViewById(R.id.cb_parent);            /*标记而已*/            holder.tv_number = (TextView) view.findViewById(R.id.tv_number);            view.setTag(holder);        }else{            view = convertView;            holder = (GroupViewHolder) view.getTag();        }        final GoosBean.DataBean dataBean = groupList.get(groupPosition);        holder.cbGroup.setChecked(dataBean.isChecked());        holder.tv_number.setText(dataBean.getTitle());        /*设置一级列表全选点击事件*/        holder.cbGroup.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                dataBean.setChecked(holder.cbGroup.isChecked());                changeChildCbState(groupPosition, holder.cbGroup.isChecked());                /*jiezhang*/                EventBus.getDefault().post(compute());                /*qx*/                changeAllCbState(isAllGroupCbSelected());                notifyDataSetChanged();            }        });        return view;    }    @Override    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {        View view;        final ChildViewHolder holder;        if(convertView==null){            holder = new ChildViewHolder();            view = inflater.inflate(R.layout.item_chid_market, null);            holder.cbChild = (CheckBox) view.findViewById(R.id.cb_child);            holder.tv_tel = (TextView) view.findViewById(R.id.tv_tel);            holder.tv_content = (TextView) view.findViewById(R.id.tv_content);            holder.tv_time = (TextView) view.findViewById(R.id.tv_time);            holder.tv_price = (TextView) view.findViewById(R.id.tv_pri);            holder.tv_del = (TextView) view.findViewById(R.id.tv_del);            holder.iv_add = (ImageView) view.findViewById(R.id.iv_add);            holder.iv_del = (ImageView) view.findViewById(R.id.iv_del);            holder.add_num_del = (TextView) view.findViewById(R.id.add_num_del);            view.setTag(holder);        }else{            view = convertView;            holder = (ChildViewHolder) view.getTag();        }        final GoosBean.DataBean.DatasBean datasBean = chidList.get(groupPosition).get(childPosition);        holder.cbChild.setChecked(datasBean.isChecked());        holder.tv_tel.setText(datasBean.getType_name());        holder.tv_content.setText(datasBean.getMsg());        holder.tv_time.setText(datasBean.getAdd_time());        holder.tv_price.setText(datasBean.getPrice() + "");        holder.add_num_del.setText(datasBean.getAdd_num_del() + "");/*给二级列表设置点击事件*/        holder.cbChild.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                datasBean.setChecked(holder.cbChild.isChecked());                /*jiezhang*/                PriceAndCountEvent priceAndCountEvent = compute();                EventBus.getDefault().post(priceAndCountEvent);                if(holder.cbChild.isChecked()){                    if(isAllChildCbSelected(groupPosition)){                        changGroupCbState(groupPosition, true);                         /*qx*/                        changeAllCbState(isAllGroupCbSelected());                    }                }else{                    changGroupCbState(groupPosition, false);                     /*qx*/                    changeAllCbState(isAllGroupCbSelected());                }                notifyDataSetChanged();            }        });/*+*/        holder.iv_add.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int add_num_del=datasBean.getAdd_num_del();                holder.add_num_del.setText(++add_num_del+"");                datasBean.setAdd_num_del(add_num_del);                /*jiezhang*/                if (holder.cbChild.isChecked()) {                    PriceAndCountEvent priceAndCountEvent = compute();                    EventBus.getDefault().post(priceAndCountEvent);                }            }        });/*_*/        holder.iv_del.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int add_num_del=datasBean.getAdd_num_del();                if(add_num_del==1){                   return;                }                holder.add_num_del.setText(--add_num_del+"");                datasBean.setAdd_num_del(add_num_del);                  /*jiezhang*/                if (holder.cbChild.isChecked()) {                    PriceAndCountEvent priceAndCountEvent = compute();                    EventBus.getDefault().post(priceAndCountEvent);                }            }        });        return view;    }    @Override    public boolean isChildSelectable(int groupPosition, int childPosition) {        return true;    }    /*点击一级列表全选,二级列表状态改变*/    private void changeChildCbState(int groupPosition, boolean flag) {        List<GoosBean.DataBean.DatasBean> datasBeen = chidList.get(groupPosition);        for (int i = 0; i < datasBeen.size(); i++) {            GoosBean.DataBean.DatasBean datasBean = datasBeen.get(i);            datasBean.setChecked(flag);        }    }     /*为了二级反选,遍历二级,确定状态*/     private boolean isAllChildCbSelected(int groupPosition) {         List<GoosBean.DataBean.DatasBean> datasBeen = chidList.get(groupPosition);         for (int i = 0; i < datasBeen.size(); i++) {             GoosBean.DataBean.DatasBean datasBean = datasBeen.get(i);             if (!datasBean.isChecked()) {                 return false;             }         }         return true;     }    /*为了二级反选,遍历一级,确定状态*/    private void changGroupCbState(int groupPosition, boolean flag) {        GoosBean.DataBean dataBean = groupList.get(groupPosition);        dataBean.setChecked(flag);    }    /*mainactivity-qx*/    public void changeAllListCbState(boolean flag) {        for (int i = 0; i < groupList.size(); i++) {            changGroupCbState(i, flag);            changeChildCbState(i, flag);        }        EventBus.getDefault().post(compute());        notifyDataSetChanged();    }    /*二级fx*/    private boolean isAllGroupCbSelected() {        for (int i = 0; i < groupList.size(); i++) {            GoosBean.DataBean dataBean = groupList.get(i);            if (!dataBean.isChecked()) {                return false;            }        }        return true;    }    /**     * 改变全选的状态     *     * @param flag     */    private void changeAllCbState(boolean flag) {        MessageEvent messageEvent = new MessageEvent();        messageEvent.setChecked(flag);        EventBus.getDefault().post(messageEvent);    }    /*结账*/    private PriceAndCountEvent compute() {        int count = 0;        int price = 0;        for (int i = 0; i < chidList.size(); i++) {            List<GoosBean.DataBean.DatasBean> datasBeen = chidList.get(i);            for (int j = 0; j < datasBeen.size(); j++) {                GoosBean.DataBean.DatasBean datasBean = datasBeen.get(j);                if (datasBean.isChecked()) {                    price += datasBean.getAdd_num_del() * datasBean.getPrice();                    count += datasBean.getAdd_num_del();                }            }        }        PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();        priceAndCountEvent.setCount(count);        priceAndCountEvent.setPrice(price);        return priceAndCountEvent;    }    class GroupViewHolder{        CheckBox cbGroup;        TextView tv_number;    }    class ChildViewHolder{        CheckBox cbChild;        TextView tv_tel;        TextView tv_content;        TextView tv_time;        TextView tv_price;        TextView tv_del;        ImageView iv_del;        ImageView iv_add;        TextView add_num_del;    }}**eventbusevent包(MessageEvent,PriceAndCountEvent)****MessageEvent**public class MessageEvent {    private boolean checked;    public boolean isChecked() {        return checked;    }    public void setChecked(boolean checked) {        this.checked = checked;    }}**PriceAndCountEvent**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;    }}**model包(IMainModel,MainModel)****IMainModel**public interface IMainModel {    public void getGoods(OnNetListener<GoosBean> onNetListener);}**MainModel**public class MainModel implements IMainModel{    private Handler handler = new Handler(Looper.getMainLooper());    @Override    public void getGoods(final OnNetListener<GoosBean> onNetListener) {        HttpUtils.getHttpUtils().doGet(Api.url, new Callback() {            @Override            public void onFailure(Call call, final IOException e) {                handler.post(new Runnable() {                    @Override                    public void run() {                        onNetListener.onFailure(e);                    }                });            }            @Override            public void onResponse(Call call, Response response) throws IOException {                String string=response.body().string();                final GoosBean goosBean = new Gson().fromJson(string, GoosBean.class);                handler.post(new Runnable() {                      @Override                      public void run() {                          onNetListener.onSuccess(goosBean);                      }                  });            }        });    }}**view(IMainActivity,MainActivity)****IMainActivity**public interface IMainActivity {    public void showList(List<GoosBean.DataBean> groupList,List<List<GoosBean.DataBean.DatasBean>> chidList);}**MainActivity**public class MainActivity extends AppCompatActivity implements IMainActivity{    private ExpandableListView mElv;    private CheckBox mCheckbox2;    /**     * 0     */    private TextView mTvPrice;    /**     * 结算(0)     */    private TextView mTvNum;    private LinearLayout mActivityMain;    private MyAdapter myAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        EventBus.getDefault().register(this);        initView();        /*注意调用P层*/        new MainPresenter(this).getGoods();    }    private void initView() {        mElv = (ExpandableListView) findViewById(R.id.elv);        mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2);        mTvPrice = (TextView) findViewById(R.id.tv_price);        mTvNum = (TextView) findViewById(R.id.tv_num);/*qx*/        mCheckbox2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                myAdapter.changeAllListCbState(mCheckbox2.isChecked());            }        });    }    @Override    public void showList(List<GoosBean.DataBean> groupList, List<List<GoosBean.DataBean.DatasBean>> chidList) {         myAdapter = new MyAdapter(this,chidList,groupList);         mElv.setAdapter(myAdapter);        //默认让其全部展开        for (int i = 0; i < groupList.size(); i++) {            mElv.expandGroup(i);        }    }    @Subscribe    public void onMessageEvent(MessageEvent event) {        mCheckbox2.setChecked(event.isChecked());    }    /*结账*/    @Subscribe    public void onMessageEvent(PriceAndCountEvent event) {        mTvNum.setText("结算(" + event.getCount() + ")");        mTvPrice.setText(event.getPrice() + "");    }}**presenter包(MainPresenter)****MainPresenter**public class MainPresenter {    private IMainModel iMainModel;    private IMainActivity iMainActivity;    public MainPresenter(IMainActivity iMainActivity) {        this.iMainActivity = iMainActivity;        iMainModel = new MainModel();    }    public void getGoods(){        iMainModel.getGoods(new OnNetListener<GoosBean>() {            @Override            public void onSuccess(GoosBean goosBean) {                List<GoosBean.DataBean> dataBean = goosBean.getData();                List<List<GoosBean.DataBean.DatasBean>> childList = new ArrayList<List<GoosBean.DataBean.DatasBean>>();                for (int i = 0; i < dataBean.size(); i++) {                    List<GoosBean.DataBean.DatasBean> datas = dataBean.get(i).getDatas();                    childList.add(datas);                }                iMainActivity.showList(dataBean, childList);            }            @Override            public void onFailure(Exception e) {            }        });    }}
原创粉丝点击