运用网络请求,请求到数据
下面开始展示数据
activity—main的布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/activity_cart"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:orientation="vertical"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="bwie.com.xiangmoyisanzoumoli1118.View.CartActivity">
- <ExpandableListView
- android:id="@+id/elv"
- android:layout_weight="1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <LinearLayout
- android:layout_weight="9"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <CheckBox
- android:id="@+id/quanxuan"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:text="全选"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <LinearLayout
- android:layout_marginLeft="50dp"
- android:orientation="vertical"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:id="@+id/zongjia"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="总价:0.0"
- />
- <TextView
- android:text="共0件商品"
- android:id="@+id/tv_count"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <TextView
- android:text="去结算"
- android:background="#FFD82020"
- android:layout_marginLeft="100dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
- </LinearLayout>
MainActivity: 运用到EventBus
- package bwie.com.xiangmoyisanzoumoli1118.View;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.CheckBox;
- import android.widget.ExpandableListView;
- import android.widget.TextView;
- import org.greenrobot.eventbus.EventBus;
- import org.greenrobot.eventbus.Subscribe;
- import java.util.ArrayList;
- import java.util.List;
- import bwie.com.xiangmoyisanzoumoli1118.Adapter.MyExpandableAdapter;
- import bwie.com.xiangmoyisanzoumoli1118.R;
- import bwie.com.xiangmoyisanzoumoli1118.View.IActivity.IGoodsActivity;
- import bwie.com.xiangmoyisanzoumoli1118.bean.CountAndPrice;
- import bwie.com.xiangmoyisanzoumoli1118.bean.GoodsBean;
- import bwie.com.xiangmoyisanzoumoli1118.bean.MessgeEvent;
- import bwie.com.xiangmoyisanzoumoli1118.persenter.GoodsPersenter;
- public class CartActivity extends AppCompatActivity implements IGoodsActivity {
- private ExpandableListView mElv;
- private List<List<GoodsBean.DataBean.ListBean>> list = new ArrayList<>();
- private CheckBox mQuanxuan;
- /**
- * 总价:0.0
- */
- private TextView mZongjia;
- /**
- * 共0件商品
- */
- private TextView mTvCount;
- private int count=0;
- private int price=0;
- private MyExpandableAdapter myExpandableAdapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_cart);
- EventBus.getDefault().register(this);
- //调用GoodsPersenter中的方法
- new GoodsPersenter(this).getGoods();
- initView();
- mQuanxuan.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- myExpandableAdapter.qx(mQuanxuan.isChecked());
- }
- });
- }
- @Override
- public void chuanZhi(List<GoodsBean.DataBean> data) {
- //遍历一级得到二级的集合
- for (int i = 0; i < data.size(); i++) {
- List<GoodsBean.DataBean.ListBean> list1 = data.get(i).getList();
- list.add(list1);
- }
- myExpandableAdapter = new MyExpandableAdapter(data, this, list);
- mElv.setAdapter(myExpandableAdapter);
- //没有
- mElv.setGroupIndicator(null);
- //默认展开
- for (int i = 0; i < data.size(); i++) {
- mElv.expandGroup(i);
- }
- }
- private void initView() {
- mElv = (ExpandableListView) findViewById(R.id.elv);
- mQuanxuan = (CheckBox) findViewById(R.id.quanxuan);
- mZongjia = (TextView) findViewById(R.id.zongjia);
- mTvCount = (TextView) findViewById(R.id.tv_count);
- }
- //接收传过来的值
- @Subscribe
- public void onPriceAndCount(CountAndPrice cp){
- mZongjia.setText("共"+cp.getCount()+"件商品");
- mTvCount.setText("总计:"+cp.getPrice());
- }
- //接收传过来的值
- @Subscribe
- public void onPriceAndCount(MessgeEvent event){
- //改变全选的状态
- mQuanxuan.setChecked(event.isCheck());
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- EventBus.getDefault().unregister(this);
- }
- }
写二级列表的适配器
- package bwie.com.xiangmoyisanzoumoli1118.Adapter;
- import android.content.Context;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.CheckBox;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.nostra13.universalimageloader.core.ImageLoader;
- import org.greenrobot.eventbus.EventBus;
- import java.util.List;
- import bwie.com.xiangmoyisanzoumoli1118.R;
- import bwie.com.xiangmoyisanzoumoli1118.bean.CountAndPrice;
- import bwie.com.xiangmoyisanzoumoli1118.bean.GoodsBean;
- import bwie.com.xiangmoyisanzoumoli1118.bean.MessgeEvent;
- public class MyExpandableAdapter extends BaseExpandableListAdapter {
- private List<GoodsBean.DataBean> groupdata;
- private Context context;
- private List<List<GoodsBean.DataBean.ListBean>> childlist;
- public MyExpandableAdapter(List<GoodsBean.DataBean> groupdata, Context context, List<List<GoodsBean.DataBean.ListBean>> childlist) {
- this.groupdata = groupdata;
- this.context = context;
- this.childlist = childlist;
- }
- @Override
- public int getGroupCount() {
- return groupdata.size();
- }
- @Override
- public int getChildrenCount(int groupPosition) {
- return childlist.get(groupPosition).size();
- }
- @Override
- public Object getGroup(int groupPosition) {
- return groupdata.get(groupPosition);
- }
- @Override
- public Object getChild(int groupPosition, int childPosition) {
- return null;
- }
- @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) {
- final GroupViewHolder holder;
- if (convertView == null) {
- convertView = View.inflate(context, R.layout.groupitem, null);
- holder = new GroupViewHolder();
- holder.tv_group = convertView.findViewById(R.id.tv_group);
- holder.group_cb = convertView.findViewById(R.id.gx_group);
- convertView.setTag(holder);
- } else {
- holder = (GroupViewHolder) convertView.getTag();
- }
- final GoodsBean.DataBean dataBean = groupdata.get(groupPosition);
- String sellerName = dataBean.getSellerName();
- holder.group_cb.setChecked(dataBean.isCheck());
- holder.tv_group.setText(sellerName);
- //点击一级checkbox的状态
- holder.group_cb.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- dataBean.setCheck(holder.group_cb.isChecked());
- //改变所有孩子的状态
- changeChildState(groupPosition,holder.group_cb.isChecked());
- EventBus.getDefault().post(jS());
- //通过判断一级的checkbox判断全选的状态
- changeMianQXstatus(checkGroupAll());
- notifyDataSetChanged();
- }
- });
- return convertView;
- }
- @Override
- public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
- final ChildViewHolder holder;
- if (convertView == null) {
- convertView = View.inflate(context, R.layout.childitem, null);
- holder = new ChildViewHolder();
- holder.tv_title=convertView.findViewById(R.id.title);
- holder.price=convertView.findViewById(R.id.price); //价格
- holder.child_cb=convertView.findViewById(R.id.gouxuan_child); //子勾选
- holder.add=convertView.findViewById(R.id.jia); //加加
- holder.del=convertView.findViewById(R.id.del); //删除
- holder.jian=convertView.findViewById(R.id.jian); //减
- holder.title_img=convertView.findViewById(R.id.title_img);
- holder.tv_num=convertView.findViewById(R.id.tv_num); //数量
- holder.yanse=convertView.findViewById(R.id.yanse);
- convertView.setTag(holder);
- } else {
- holder = (ChildViewHolder) convertView.getTag();
- }
- final GoodsBean.DataBean.ListBean listBean = childlist.get(groupPosition).get(childPosition);
- int price = (int) listBean.getPrice();
- holder.child_cb.setChecked(listBean.isCheck());
- holder.price.setText("¥:"+price+"");
- String title = listBean.getTitle();
- holder.tv_title.setText(title);
- String subhead = listBean.getSubhead();
- String substring = subhead.substring(1, 10)+".....";
- holder.yanse.setText(substring);
- String images = listBean.getImages();
- String[] split = images.split("\\|");
- String s = split[1];
- ImageLoader.getInstance().displayImage(s,holder.title_img);
- listBean.setCou(1);
- //给子条目的checkBox点击事件
- holder.child_cb.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- listBean.setCheck(holder.child_cb.isChecked());
- EventBus.getDefault().post(jS());
- //如果二级的checkBox选中,就让一级的checkBox勾选
- if(holder.child_cb.isChecked()) {
- if (ischeckAllchild(groupPosition)) {
- //改变一级checkBox的状态
- changeGroupstatus(groupPosition,true);
- //通过判断一级的CheckBox的状态来改变全选的状态
- changeMianQXstatus(checkGroupAll());
- }
- }else{
- changeGroupstatus(groupPosition,false);
- changeMianQXstatus(checkGroupAll());
- }
- notifyDataSetChanged();
- }
- });
- //点击加加的功能
- holder.add.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- int cou = listBean.getCou();
- holder.tv_num.setText(++cou+"");
- listBean.setCou(cou);
- if(holder.child_cb.isChecked()){
- EventBus.getDefault().post(jS());
- }
- }
- });
- //点击减减的功能
- holder.jian.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- int cou = listBean.getCou();
- if(cou==1) { return;}
- holder.tv_num.setText(--cou+"");
- listBean.setCou(cou);
- if (holder.child_cb.isChecked()) {
- EventBus.getDefault().post(jS());
- }
- }
- });
- //点击删除
- holder.del.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- List<GoodsBean.DataBean.ListBean> listBeen = childlist.get(groupPosition);
- GoodsBean.DataBean.ListBean remove = listBeen.remove(childPosition);
- if(listBeen.size()==0){
- childlist.remove(childPosition);
- groupdata.remove(groupPosition);
- }
- EventBus.getDefault().post(jS());
- notifyDataSetChanged();
- }
- });
- return convertView;
- }
- @Override
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- return true;
- }
- class GroupViewHolder {
- CheckBox group_cb;
- TextView tv_group;
- }
- class ChildViewHolder {
- CheckBox child_cb;
- TextView tv_title,price,yanse,tv_num;
- ImageView title_img,del,add,jian;
- }
- /**
- * 判断全部的二级是CheckBox否选中
- *
- */
- private boolean ischeckAllchild(int g){
- List<GoodsBean.DataBean.ListBean> listBeen = childlist.get(g);
- for (int i = 0; i <listBeen.size() ; i++) {
- GoodsBean.DataBean.ListBean listBean = listBeen.get(i);
- if(!listBean.isCheck()){
- return false;
- }
- }
- return true;
- }
- /**
- * 计算数量和价格
- * 通过遍历一级的得到二级的CheckBox,如果是选择的选中状态的话,就统计它们的总计的数量和价格
- */
- private CountAndPrice jS(){
- int cou=0;
- int jiage=0;
- for (int i = 0; i <groupdata.size() ; i++) {
- for (int j = 0; j <childlist.get(i).size() ; j++) {
- GoodsBean.DataBean.ListBean listBean = childlist.get(i).get(j);
- if(listBean.isCheck()){
- cou+=listBean.getCou();
- jiage+=(int)listBean.getPrice()*listBean.getCou();
- }
- }
- }
- CountAndPrice countAndPrice = new CountAndPrice();
- countAndPrice.setCount(cou);
- countAndPrice.setPrice(jiage);
- return countAndPrice;
- }
- /**
- * 改变一级列表的状态
- */
- private void changeGroupstatus(int groupPosition,boolean flag ){
- GoodsBean.DataBean dataBean = groupdata.get(groupPosition);
- dataBean.setCheck(flag);
- }
- /**
- * 改变全选的状态
- *
- */
- private void changeMianQXstatus(boolean flag){
- MessgeEvent messgeEvent = new MessgeEvent();
- messgeEvent.setCheck(flag);
- EventBus.getDefault().post(messgeEvent);
- }
- /**
- * 判断一级的是否全部选中
- */
- private boolean checkGroupAll(){
- for (int i = 0; i < groupdata.size(); i++) {
- if(!groupdata.get(i).isCheck()){
- return false;
- }
- }
- return true;
- }
- /**
- * 改变所有孩子的状态
- */
- private void changeChildState(int grouPistion,boolean flag){
- List<GoodsBean.DataBean.ListBean> listBeen = childlist.get(grouPistion);
- for (int i = 0; i < listBeen.size(); i++) {
- listBeen.get(i).setCheck(flag);
- }
- }
- /**
- * 改变全选的
- */
- public void qx(boolean flag){
- for (int i = 0; i < groupdata.size(); i++) {
- changeGroupstatus(i, flag);
- changeChildState(i, flag);
- }
- EventBus.getDefault().post(jS());
- notifyDataSetChanged();
- }
- }
一级列表的布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <CheckBox
- android:id="@+id/gx_group"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/tv_group"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
二级列表的布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:id="@+id/title"
- android:layout_marginLeft="20dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <CheckBox
- android:layout_marginLeft="20dp"
- android:gravity="center_vertical"
- android:id="@+id/gouxuan_child"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <ImageView
- android:gravity="center_vertical"
- android:id="@+id/title_img"
- android:layout_width="50dp"
- android:layout_height="50dp" />
- <LinearLayout
- android:orientation="vertical"
- android:layout_marginLeft="30dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:id="@+id/price"
- android:layout_marginLeft="10dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/yanse"
- android:layout_marginLeft="10dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <LinearLayout
- android:layout_marginLeft="100dp"
- android:orientation="horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <ImageView
- android:id="@+id/jian"
- android:layout_width="20dp"
- android:layout_height="20dp"
- android:src="@drawable/shopcart_minus_grey" />
- <TextView
- android:id="@+id/tv_num"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dp"
- android:background="@drawable/shopcart_add_btn"
- android:paddingBottom="2dp"
- android:paddingLeft="20dp"
- android:paddingRight="20dp"
- android:paddingTop="2dp"
- android:text="1" />
- <ImageView
- android:id="@+id/jia"
- android:layout_width="20dp"
- android:layout_height="20dp"
- android:layout_marginLeft="5dp"
- android:src="@drawable/shopcart_add_red" />
- </LinearLayout>
- </LinearLayout>
- <ImageView
- android:id="@+id/del"
- android:layout_marginLeft="20dp"
- android:background="@drawable/a"
- android:layout_width="32dp"
- android:layout_height="29dp" />
- </LinearLayout>
- </LinearLayout>
画椭圆
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <corners android:radius="200dp"></corners>
- <stroke android:color="@color/colorPrimaryDark" android:width="1dp"></stroke>
- </shape>