//M层
public class ShoppingModel {
private static final String TAG = "ShoppingModel";
public void login(final ModelView modelView){
String url="https://www.zhaoapi.cn/product/getCarts?uid=71";
OkhtttpUtils.getInstance().doGet(url, new OkhtttpUtils.OkCallback() {
@Override
public void onFailure(Exception e) {
}
@Override
public void onResponse(String json) {
Log.d(TAG, "onResponse: 111111111"+json);
ShoppingBean shoppingBean = new Gson().fromJson(json, ShoppingBean.class);
Log.d(TAG, "onResponse: 222222222"+shoppingBean);
String code = shoppingBean.getCode();
if ("0".equalsIgnoreCase(code)){
if (modelView!=null){
modelView.onSuccess(shoppingBean);
}
}else {
if (modelView!=null){
modelView.onFailure("失败");
}
}
}
});
}
public interface ModelView{
void onSuccess(ShoppingBean shoppingBean);
void onFailure(String error);
}
}
//P层
public class ShoppingPresenter extends BasePresenter<ShoppingView>{
private ShoppingModel shoppingModel;
public ShoppingPresenter(ShoppingView view) {
super(view);
}
@Override
protected void initModel() {
shoppingModel = new ShoppingModel();
}
public void login(){
shoppingModel.login(new ShoppingModel.ModelView() {
@Override
public void onSuccess(ShoppingBean shoppingBean) {
if (view!=null){
view.onShoppingSuccess(shoppingBean);
}
}
@Override
public void onFailure(String error) {
if (view!=null){
view.onShoppingFailure(error);
}
}
});
}
}
//V层
public class ShoppingActivity extends BaseFragment<ShoppingPresenter> implements ShoppingView {
private List<ShoppingBean.DataBean> list=new ArrayList<>();
private ShoppingExpandAdapter shoppingExpandAdapter;
private ExpandableListView expandableListView;
private TextView textView;
private Button button;
private CheckBox box;
@Override
protected void initListener() {
}
@Override
protected void initDatas() {
presenter.login();
}
@Override
protected void initViews() {
expandableListView = getView().findViewById(R.id.shopping_expand);
box = getView().findViewById(R.id.shopping_box);
button = getView().findViewById(R.id.shopping_button);
textView = getView().findViewById(R.id.shopping_price);
box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
boolean allProductsSelected = shoppingExpandAdapter.isAllProductsSelected();
shoppingExpandAdapter.changeAllProductsStatus(!allProductsSelected);
shoppingExpandAdapter.notifyDataSetChanged();
refreshSelectedAndTotalPriceAndTotalNumber();
}
});
}
@Override
protected ShoppingPresenter providePresenter() {
return new ShoppingPresenter(this);
}
@Override
protected int provideLayoutId() {
return R.layout.activity_shopping;
}
@Override
public void onShoppingSuccess(final ShoppingBean shoppingBean) {
List<ShoppingBean.DataBean> data = shoppingBean.getData();
list.addAll(data);
shoppingExpandAdapter = new ShoppingExpandAdapter(list);
shoppingExpandAdapter.setOnCartListChangeListener(new ShoppingExpandAdapter.onCartListChangeListener() {
@Override
public void onSellerCheckedChange(int groupPosition) {
boolean parentAllSelected = shoppingExpandAdapter.isParentAllSelected(groupPosition);
shoppingExpandAdapter.changeCurrentSellerAllProductsStatus(groupPosition,!parentAllSelected);
shoppingExpandAdapter.notifyDataSetChanged();
refreshSelectedAndTotalPriceAndTotalNumber();
}
@Override
public void onProductCheckedChange(int groupPosition, int childPosition) {
shoppingExpandAdapter.changeCurrentProductStatus(groupPosition,childPosition);
shoppingExpandAdapter.notifyDataSetChanged();
refreshSelectedAndTotalPriceAndTotalNumber();
}
@Override
public void onProductNumberChange(int groupPosition, int childPosition, int number) {
shoppingExpandAdapter.changeCurrentProductNumber(groupPosition,childPosition,number);
shoppingExpandAdapter.notifyDataSetChanged();
refreshSelectedAndTotalPriceAndTotalNumber();
}
});
expandableListView.setAdapter(shoppingExpandAdapter);
for (int i = 0; i < list.size(); i++) {
expandableListView.expandGroup(i);
}
refreshSelectedAndTotalPriceAndTotalNumber();
}
private void refreshSelectedAndTotalPriceAndTotalNumber() {
boolean allProductsSelected = shoppingExpandAdapter.isAllProductsSelected();
box.setChecked(allProductsSelected);
float v = shoppingExpandAdapter.totalPrice();
textView.setText("合计:¥"+v);
int i = shoppingExpandAdapter.totalNum();
button.setText("结算("+i+")");
}
@Override
public void onShoppingFailure(String error) {
}
@Override
public Context context() {
return getActivity();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
//加减器
public class MyAddremove extends LinearLayout {
@BindView(R.id.sub_tv)
TextView subTv;
@BindView(R.id.number_tv)
TextView numberTv;
@BindView(R.id.sadd_tv)
TextView saddTv;
private int number = 1;
public MyAddremove(Context context) {
this(context, null);
}
public MyAddremove(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
View view = inflate(context, R.layout.shopping_add_remove, this);
ButterKnife.bind(view);
}
@OnClick({R.id.sub_tv, R.id.sadd_tv})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.sub_tv:
if (number>1){
--number;
numberTv.setText(number+"");
if (onNumberChangeListener!=null){
onNumberChangeListener.onNumberChange(number);
}
}else {
Toast.makeText(getContext(),"不能再少了",Toast.LENGTH_SHORT).show();
}
break;
case R.id.sadd_tv:
++number;
numberTv.setText(number+"");
if (onNumberChangeListener!=null){
onNumberChangeListener.onNumberChange(number);
}
break;
}
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
numberTv.setText(number+"");
}
OnNumberChangeListener onNumberChangeListener;
public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
this.onNumberChangeListener = onNumberChangeListener;
}
public interface OnNumberChangeListener {
void onNumberChange(int num);
}
}
//适配器
public class ShoppingExpandAdapter extends BaseExpandableListAdapter {
private List<ShoppingBean.DataBean> list;
public ShoppingExpandAdapter(List<ShoppingBean.DataBean> list) {
this.list = list;
}
@Override
public int getGroupCount() {
return list == null ? 0 : list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getList() == null ? 0 : list.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ShoppingBean.DataBean dataBean = list.get(groupPosition);
ParentViewHolder parentViewHolder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.shopping_expand_parent, null);
parentViewHolder = new ParentViewHolder(convertView);
convertView.setTag(parentViewHolder);
} else {
parentViewHolder = (ParentViewHolder) convertView.getTag();
}
parentViewHolder.parentName.setText(list.get(groupPosition).getSellerName());
boolean parentSelected = isParentAllSelected(groupPosition);
parentViewHolder.parentBox.setChecked(parentSelected);
parentViewHolder.parentBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onCartListChangeListener!=null){
onCartListChangeListener.onSellerCheckedChange(groupPosition);
}
}
});
return convertView;
}
//当前商家所有商品是否被选中
public boolean isParentAllSelected(int groupPosition){
ShoppingBean.DataBean dataBean = list.get(groupPosition);
List<ShoppingBean.DataBean.ListBean> beanList = dataBean.getList();
for (ShoppingBean.DataBean.ListBean listBean:beanList) {
if (listBean.getSelected()==0){
return false;
}
}
return true;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ShoppingBean.DataBean.ListBean listBean = this.list.get(groupPosition).getList().get(childPosition);
ChildViewHolder childViewHolder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.shopping_child, null);
childViewHolder=new ChildViewHolder(convertView);
convertView.setTag(childViewHolder);
}else {
childViewHolder=(ChildViewHolder)convertView.getTag();
}
childViewHolder.childText.setText(listBean.getTitle());
childViewHolder.childPrice.setText(listBean.getBargainPrice()+"");
String[] split = listBean.getImages().split("\\|");
ImageLoader.getInstance().displayImage(split[0],childViewHolder.childImage, MyApp.getOptions());
childViewHolder.childBox.setChecked(listBean.getSelected()==1);
childViewHolder.childBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onCartListChangeListener!=null){
onCartListChangeListener.onProductCheckedChange(groupPosition,childPosition);
}
}
});
childViewHolder.chidAddRemove.setNumber(listBean.getNum());
childViewHolder.chidAddRemove.setOnNumberChangeListener(new MyAddremove.OnNumberChangeListener() {
@Override
public void onNumberChange(int num) {
if (onCartListChangeListener!=null){
onCartListChangeListener.onProductNumberChange(groupPosition,childPosition,num);
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
static class ParentViewHolder {
@BindView(R.id.parent_box)
CheckBox parentBox;
@BindView(R.id.parent_name)
TextView parentName;
ParentViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
static class ChildViewHolder {
@BindView(R.id.child_box)
CheckBox childBox;
@BindView(R.id.child_image)
ImageView childImage;
@BindView(R.id.child_text)
TextView childText;
@BindView(R.id.child_price)
TextView childPrice;
@BindView(R.id.chid_add_remove)
MyAddremove chidAddRemove;
ChildViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
onCartListChangeListener onCartListChangeListener;
public void setOnCartListChangeListener(onCartListChangeListener onCartListChangeListener){
this.onCartListChangeListener=onCartListChangeListener;
}
public interface onCartListChangeListener{
void onSellerCheckedChange(int groupPosition);
void onProductCheckedChange(int groupPosition,int childPosition);
void onProductNumberChange(int groupPosition,int childPosition,int number);
}
//所有商品是否被选中
public boolean isAllProductsSelected(){
for (int i = 0; i < list.size(); i++) {
ShoppingBean.DataBean dataBean = list.get(i);
List<ShoppingBean.DataBean.ListBean> beanList = dataBean.getList();
for (int j = 0; j < beanList.size(); j++) {
if (beanList.get(j).getSelected()==0){
return false;
}
}
}
return true;
}
//计算总价
public float totalPrice(){
float total=0;
for (int i = 0; i < list.size(); i++) {
List<ShoppingBean.DataBean.ListBean> beanList = this.list.get(i).getList();
for (int j = 0; j < beanList.size(); j++) {
if (beanList.get(j).getSelected()==1) {
float bargainPrice = beanList.get(j).getBargainPrice();
int num = beanList.get(j).getNum();
total += num * bargainPrice;
}
}
}
return total;
}
//计算总数
public int totalNum(){
int number=0;
for (int i = 0; i < list.size(); i++) {
List<ShoppingBean.DataBean.ListBean> beanList = this.list.get(i).getList();
for (int j = 0; j < beanList.size(); j++) {
if (beanList.get(j).getSelected()==1) {
int num = beanList.get(j).getNum();
number+=num;
}
}
}
return number;
}
//当商家得checkbox被点击得时候调用,设置当前商家得所有商品得状态
public void changeCurrentSellerAllProductsStatus(int groupPosition,boolean isSelected){
List<ShoppingBean.DataBean.ListBean> beanList = this.list.get(groupPosition).getList();
for (int i = 0; i < beanList.size(); i++) {
ShoppingBean.DataBean.ListBean listBean = beanList.get(i);
listBean.setSelected(isSelected?1:0);
}
}
//当商品得checkbox被点击得时候调用,改变当前商品状态
public void changeCurrentProductStatus(int groupPosition, int childPosition){
ShoppingBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition);
listBean.setSelected(listBean.getSelected()==0?1:0);
}
//当加减器被点击得时候调用,改变当前商品得数量
public void changeCurrentProductNumber(int groupPosition, int childPosition, int number){
ShoppingBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition);
listBean.setNum(number);
}
//设置所有商品得状态
public void changeAllProductsStatus(boolean selected){
for (int i = 0; i < list.size(); i++) {
List<ShoppingBean.DataBean.ListBean> listBean = this.list.get(i).getList();
for (int j = 0; j < listBean.size(); j++) {
listBean.get(j).setSelected(selected?1:0);
}
}
}
}
//接口
public interface ShoppingView extends IView{
void onShoppingSuccess(ShoppingBean shoppingBean);
void onShoppingFailure(String error);
}
//主布局
<ExpandableListView
android:id="@+id/shopping_expand"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"></ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/shopping_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"/>
<TextView
android:id="@+id/shopping_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="合计:¥0.00"
android:layout_marginLeft="20dp"
android:layout_weight="1"/>
<Button
android:id="@+id/shopping_button"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="结算(0)"
android:layout_marginRight="10dp"/>
</LinearLayout>
//加减器布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:layout_height="30dp"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:background="#99000000">
<TextView
android:id="@+id/sub_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"
android:textSize="16dp"
android:background="#fff"
android:gravity="center"
android:layout_margin="2dp"/>
<TextView
android:id="@+id/number_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:textSize="16dp"
android:background="#fff"
android:gravity="center"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"/>
<TextView
android:id="@+id/sadd_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"
android:textSize="16dp"
android:background="#fff"
android:gravity="center"
android:layout_margin="2dp"/>
</LinearLayout>
//组
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:padding="@dimen/space_padding">
<CheckBox
android:id="@+id/parent_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/parent_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商品"/>
</LinearLayout>
//子
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/child_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"/>
<ImageView
android:id="@+id/child_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginLeft="10dp">
<TextView
android:id="@+id/child_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="商品"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/child_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥0.0"/>
<com.bwie.zhoujianhang.yue_moni.mvp.shopping.view.MyAddremove
android:id="@+id/chid_add_remove"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_marginRight="10dp"></com.bwie.zhoujianhang.yue_moni.mvp.shopping.view.MyAddremove>
</RelativeLayout>
</LinearLayout>
</LinearLayout>