使用的是rxjava观察者模式
主布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".view.activity.ShopActivity"> <ScrollView android:layout_above="@+id/linear_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!--购物车的二级列表--> <com.example.desktop.monthshoppingcart.view.costom.MyExpandableListView android:id="@+id/expanable_listview" android:layout_width="match_parent" android:layout_height="match_parent"> </com.example.desktop.monthshoppingcart.view.costom.MyExpandableListView> </LinearLayout> </ScrollView> <LinearLayout android:id="@+id/linear_layout" android:layout_alignParentBottom="true" android:gravity="center_vertical" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <CheckBox android:layout_marginLeft="10dp" android:id="@+id/check_all" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选"/> <TextView android:id="@+id/text_total" android:text="合计:¥0.00" android:layout_weight="2" android:layout_marginLeft="8dp" android:layout_width="0dp" android:layout_height="wrap_content" /> <TextView android:text="去结算(0)" android:background="#ff0000" android:textColor="#ffffff" android:gravity="center" android:id="@+id/text_buy" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> </LinearLayout> </RelativeLayout>
一级布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:gravity="center_vertical" android:padding="10dp" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/check_group" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_marginLeft="10dp" android:text="标题" android:id="@+id/text_group" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
二级布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:padding="10dp" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:layout_centerVertical="true" android:id="@+id/check_child" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/image_good" android:layout_centerVertical="true" android:layout_toRightOf="@+id/check_child" android:layout_marginLeft="10dp" android:layout_width="80dp" android:layout_height="80dp" /> <TextView android:id="@+id/text_title" android:layout_toRightOf="@+id/image_good" android:layout_marginLeft="10dp" android:layout_alignTop="@+id/image_good" android:maxLines="2" android:minLines="2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_price" android:layout_toRightOf="@+id/image_good" android:layout_marginLeft="10dp" android:layout_alignBottom="@+id/image_good" android:text="¥99.99" android:textColor="#ff0000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_alignParentRight="true" android:layout_alignBottom="@+id/image_good" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/text_jian" android:text="一" android:padding="5dp" android:background="@drawable/bian_kuang_line" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:gravity="center" android:id="@+id/text_num" android:paddingLeft="10dp" android:paddingRight="10dp" android:background="@drawable/bian_kuang_line" android:layout_width="wrap_content" android:layout_height="match_parent" /> <TextView android:id="@+id/text_add" android:text="十" android:padding="5dp" android:background="@drawable/bian_kuang_line" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout>
边框的样式
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#fff" ></solid> <stroke android:color="#000" android:width="1dp"/> </shape>
在bean类中需要加入一个布尔类型的值
public static class DataBean { private boolean groupIsCkeck; private String sellerName; private String sellerid; public boolean isGroupIsCkeck() { return groupIsCkeck; } public void setGroupIsCkeck(boolean groupIsCkeck) { this.groupIsCkeck = groupIsCkeck; } private List<ListBean> list;
定义一个接口
public interface IPosition { void getGroup(int group, Boolean b); void getChild(int group, int child, Boolean b); void getNum(int group, int child, Boolean b, int value); }
activity中
public class ShopActivity extends AppCompatActivity implements Shop_face, View.OnClickListener { @BindView(R.id.expanable_listview) MyExpandableListView expanableListview; @BindView(R.id.check_all) CheckBox checkAll; @BindView(R.id.text_total) TextView textTotal; @BindView(R.id.text_buy) TextView textBuy; @BindView(R.id.linear_layout) LinearLayout linearLayout; private ShopPresenter shopPresenter; private int uid; private List<ShopBean.DataBean> data; private List<ShopBean.DataBean.ListBean> childlist=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shop); ButterKnife.bind(this); expanableListview.setGroupIndicator(null); shopPresenter = new ShopPresenter(this); shopPresenter.setData(ApiUrl.getCart_url, 11657); checkAll.setOnClickListener(this); } @Override public void setVSuccess(ShopBean shopBean) { Log.i("-----", shopBean.getCode() + ""); // if ("0".equals(shopBean.getCode())) { data = shopBean.getData(); checkAll.setChecked(childIsChick()); //改变组的状态 for (int j = 0; j < data.size(); j++) { data.get(j).setGroupIsCkeck(childIsAllCheck(data.get(j).getList())); } MyExpandableAdapter adapter = new MyExpandableAdapter(data, ShopActivity.this); expanableListview.setAdapter(adapter); for (int i = 0; i < shopBean.getData().size(); i++) { expanableListview.expandGroup(i); } adapter.setLinstner(new IPosition() { @Override public void getGroup(int group, Boolean b) { Log.d("MainActivity", "map:======" + b); changeStateByGroup(data.get(group).getList(), b); changePriceAndNum(); } @Override public void getChild(int group, int child, Boolean b) { changeStateByChild(data.get(group).getList().get(child), b); changePriceAndNum(); } @Override public void getNum(int group, int child, Boolean b, int value) { changeStateByNum(data.get(group).getList().get(child), b,value); changePriceAndNum(); } }); changePriceAndNum(); /* } else { Toast.makeText(ShopActivity.this, shopBean.getMsg(), Toast.LENGTH_SHORT).show(); }*/ } private void changeStateByNum(ShopBean.DataBean.ListBean list, Boolean b, int value) { Map<String, String> map = new HashMap<>(); map.put("uid", "11657"); map.put("sellerid", String.valueOf(list.getSellerid())); map.put("pid", String.valueOf(list.getPid())); map.put("num", String.valueOf(value)); map.put("selected", String.valueOf(b ? 1 : 0)); RetrofitUtil.getApiService(ApiUrl.updateCart_url).gengCart(map) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Action1<String>() { @Override public void call(String s) { Log.d("MainActivity", "map:======"); shopPresenter.setData(ApiUrl.getCart_url, 11657); } }); } private void changeStateByChild(ShopBean.DataBean.ListBean list, Boolean b) { Map<String, String> map = new HashMap<>(); map.put("uid", "11657"); map.put("sellerid", String.valueOf(list.getSellerid())); map.put("pid", String.valueOf(list.getPid())); map.put("num", String.valueOf(list.getNum())); map.put("selected", String.valueOf(b ? 1 : 0)); RetrofitUtil.getApiService(ApiUrl.updateCart_url).gengCart(map) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Action1<String>() { @Override public void call(String s) { Log.d("MainActivity", "map:======"); shopPresenter.setData(ApiUrl.getCart_url, 11657); } }); } private boolean childIsAllCheck(List<ShopBean.DataBean.ListBean> list) { for (int j = 0; j < list.size(); j++) { if (list.get(j).getSelected() == 0) { return false; } } return true; } private boolean childIsChick() { for (int i = 0; i < data.size(); i++) { List<ShopBean.DataBean.ListBean> list = data.get(i).getList(); for (int j = 0; j < list.size(); j++) { if (list.get(j).getSelected() == 0) { return false; } } } return true; } private void changePriceAndNum() { int num = 0; int sum = 0; for (int i = 0; i < data.size(); i++) { List<ShopBean.DataBean.ListBean> list = data.get(i).getList(); for (int j = 0; j < list.size(); j++) { if (list.get(j).getSelected() == 1) { num += list.get(j).getNum(); sum += num * Double.valueOf(list.get(j).getBargainPrice()); } } } textBuy.setText("去结算" + num); textTotal.setText("" + sum); } private void changeStateByGroup(List<ShopBean.DataBean.ListBean> list, Boolean b) { Observable[] observables = new Observable[list.size()]; for (int j = 0; j < list.size(); j++) { Map<String, String> map = new HashMap<>(); map.put("uid", "11657"); map.put("sellerid", String.valueOf(list.get(j).getSellerid())); map.put("pid", String.valueOf(list.get(j).getPid())); map.put("num", String.valueOf(list.get(j).getNum())); map.put("selected", String.valueOf(b ? 1 : 0)); observables[j]= RetrofitUtil.getApiService(ApiUrl.updateCart_url).gengCart(map); Log.d("MainActivity", "map:" + map); } Observable.merge(observables) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Subscriber() { @Override public void onCompleted() { Log.d("MainActivity", "map:======"); shopPresenter.setData(ApiUrl.getCart_url, 11657); } @Override public void onError(Throwable e) { Log.d("MainActivity", "map:+++" + e); } @Override public void onNext(Object o) { } }); } @Override public void setVError(String s) { } @Override protected void onDestroy() { super.onDestroy(); //取消注册事件 EventBus.getDefault().unregister(this); } @Subscribe(threadMode = ThreadMode.POSTING, sticky = true) public void onMoonEvent(MessageEvent messageEvent) { uid = messageEvent.getMessage(); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.check_all: childlist.clear(); for (int i=0;i<data.size();i++){ List<ShopBean.DataBean.ListBean> list= data.get(i).getList(); for (int j=0;j<list.size();j++){ childlist.add(list.get(j)); } } changAllState(); changePriceAndNum(); break; } } private void changAllState() { List<Observable> observables=new ArrayList<>(); for (int j=0;j<childlist.size();j++){ Map<String,String> map=new HashMap<>(); map.put("uid","11657"); map.put("sellerid",String.valueOf(childlist.get(j).getSellerid())); map.put("pid",String.valueOf(childlist.get(j).getPid())); map.put("num",String.valueOf(childlist.get(j).getNum())); map.put("selected",String.valueOf(checkAll.isChecked()?1:0)); Observable<String> observable = RetrofitUtil.getApiService(ApiUrl.updateCart_url).gengCart(map); observables.add(observable); Log.d("MainActivity", "map:" + map); } Observable.merge(observables.toArray(new Observable[childlist.size()])) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Action1() { @Override public void call(Object o) { Log.d("MainActivity", "map:======"); shopPresenter.setData(ApiUrl.getCart_url, 11657); } }); } }
适配器:
public class MyExpandableAdapter extends BaseExpandableListAdapter{ List<ShopBean.DataBean> data; Context con; private IPosition iPosition; public MyExpandableAdapter(List<ShopBean.DataBean> data, Context con) { this.data = data; this.con = con; } @Override public int getGroupCount() { return data.size(); } @Override public int getChildrenCount(int i) { return data.get(i).getList().size(); } @Override public Object getGroup(int i) { return data.get(i); } @Override public Object getChild(int i, int i1) { return data.get(i).getList().get(i1); } @Override public long getGroupId(int i) { return i; } @Override public long getChildId(int i, int i1) { return i1; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) { final GroupHolder gh; if (view==null){ view=View.inflate(con, R.layout.group_item,null); gh=new GroupHolder(); gh.check_group=(CheckBox) view.findViewById(R.id.check_group); gh.text_group=(TextView) view.findViewById(R.id.text_group); view.setTag(gh); }else { gh= (GroupHolder) view.getTag(); } gh.check_group.setChecked(data.get(i).isGroupIsCkeck()); gh.text_group.setText(data.get(i).getSellerName()); gh.check_group.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { iPosition.getGroup(i,gh.check_group.isChecked()); } }); return view; } @Override public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) { final ChildHolder ch; if (view==null){ view=View.inflate(con, R.layout.child_item,null); ch=new ChildHolder(); ch.check_child=(CheckBox) view.findViewById(R.id.check_child); ch.text_title=(TextView) view.findViewById(R.id.text_title); ch.text_price=(TextView) view.findViewById(R.id.text_price); ch.text_add=(TextView) view.findViewById(R.id.text_add); ch.text_jian=(TextView) view.findViewById(R.id.text_jian); ch.text_num=(TextView) view.findViewById(R.id.text_num); ch.draweeView=(SimpleDraweeView) view.findViewById(R.id.image_good); view.setTag(ch); }else { ch= (ChildHolder) view.getTag(); } final ShopBean.DataBean.ListBean listBean = data.get(i).getList().get(i1); ch.check_child.setChecked(listBean.getSelected()==1?true:false); ch.text_num.setText(""+listBean.getNum()); ch.text_title.setText(listBean.getTitle()); ch.text_price.setText(""+listBean.getBargainPrice()); ch.draweeView.setImageURI(listBean.getImages().split("\\|")[0]); ch.check_child.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { iPosition.getChild(i,i1,ch.check_child.isChecked()); } }); ch.text_jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int a= listBean.getNum() - 1; iPosition.getNum(i,i1,ch.check_child.isChecked(),a); } }); ch.text_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int a= listBean.getNum() +1; iPosition.getNum(i,i1,ch.check_child.isChecked(),a); } }); return view; } @Override public boolean isChildSelectable(int i, int i1) { return true; } public void setLinstner(IPosition iPosition) { this.iPosition=iPosition; } private class GroupHolder{ CheckBox check_group; TextView text_group; } private class ChildHolder{ CheckBox check_child; SimpleDraweeView draweeView; TextView text_title; TextView text_price; TextView text_jian; TextView text_num; TextView text_add; } }
--------------------------------P层和M层--------------
P层:
public class ShopPresenter implements Shop_inter{ private Shop_face shop_face; private final ShopModel shopModel; public ShopPresenter(Shop_face shop_face) { this.shop_face=shop_face; shopModel = new ShopModel(this); } public void setData(String getCart_url, int uid) { shopModel.setUserData(getCart_url,uid); } @Override public void setSuccess(ShopBean shopBean) { shop_face.setVSuccess(shopBean); } @Override public void setError(String s) { } }
M层:
public class ShopModel { private Shop_inter shop_inter; public ShopModel(Shop_inter shop_inter) { this.shop_inter=shop_inter; } public void setUserData(String getCart_url, final int uid) { RetrofitUtil.getApiService(getCart_url).getCart(uid+"") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<String>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Log.d("ShopModel", "e:" + e); } @Override public void onNext(String s) { Log.d("ShopModel", s); ShopBean shopBean = new Gson().fromJson(s, ShopBean.class); shop_inter.setSuccess(shopBean); } }); } }