<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:orientation=
"vertical"
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.MainActivity">
<TextView
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:gravity=
"center"
android:text=
"购物车"
android:textSize=
"25sp" />
<View
android:layout_width=
"match_parent"
android:layout_height=
"0.75dp"
android:background=
"#999999" />
<ExpandableListView
android:id=
"@+id/expan_list_view"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:groupIndicator=
"@null"></ExpandableListView>
<LinearLayout
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:gravity=
"center"
android:orientation=
"horizontal">
<CheckBox
android:id=
"@+id/xuan"
style=
"@style/Widget.AppCompat.CompoundButton.RadioButton"
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/sum"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_marginLeft=
"10dp"
android:layout_weight=
"1"
android:text=
"合计:¥0"
android:textSize=
"25sp" />
<Button
android:id=
"@+id/toJiSuan"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:background=
"#F00"
android:text=
"去结算" />
</LinearLayout>
</LinearLayout>
styles.xml文件-------------------------------------------------
<resources>
<!-- Base application theme. -->
<style name=
"AppTheme" parent=
"Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name=
"colorPrimary">@color/colorPrimary</item>
<item name=
"colorPrimaryDark">@color/colorPrimaryDark</item>
<item name=
"colorAccent">@color/colorAccent</item>
</style>
</resources>
购物车加减号的布局-----------------------------------------------------
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"100dp"
android:layout_height=
"30dp"
android:orientation=
"horizontal">
<TextView
android:id=
"@+id/sub"
android:layout_width=
"wrap_content"
android:layout_height=
"match_parent"
android:layout_weight=
"1"
android:background=
"@drawable/add_sub_shape"
android:gravity=
"center"
android:text=
"-"
android:textSize=
"25sp" />
<EditText
android:id=
"@+id/count"
android:layout_width=
"wrap_content"
android:layout_height=
"match_parent"
android:layout_weight=
"1"
android:background=
"@drawable/add_sub_shape"
android:focusable=
"false"
android:gravity=
"center"
android:text=
"0" />
<TextView
android:id=
"@+id/add"
android:layout_width=
"wrap_content"
android:layout_height=
"match_parent"
android:layout_weight=
"1"
android:background=
"@drawable/add_sub_shape"
android:gravity=
"center"
android:text=
"+"
android:textSize=
"25sp" />
</LinearLayout>
购物车商品的布局------------------------------------------------------
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:gravity=
"center"
android:orientation=
"horizontal">
<CheckBox
style=
"@style/Widget.AppCompat.CompoundButton.RadioButton"
android:id=
"@+id/child_cb"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content" />
<ImageView
android:id=
"@+id/child_icon"
android:layout_width=
"50dp"
android:layout_height=
"50dp"
android:src=
"@mipmap/ic_launcher" />
<LinearLayout
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:orientation=
"vertical">
<TextView
android:id=
"@+id/child_title"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:text=
"商品名称"
android:textSize=
"20sp" />
<LinearLayout
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:gravity=
"center"
android:orientation=
"horizontal">
<TextView
android:id=
"@+id/child_price"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"价格:0"
android:textColor=
"@android:color/holo_red_light" />
<com.example.administrator.myapplication.view.AddSubView
android:id=
"@+id/add_sub_view"
android:layout_width=
"100dp"
android:layout_height=
"25dp"
android:layout_marginLeft=
"40dp"></com.example.administrator.myapplication.view.AddSubView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
购物车商品的布局--------------------------------------------------------<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/parent_cb"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content" />
<TextView
android:id=
"@+id/parent_title"
android:text=
"商家"
android:singleLine=
"true"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content" />
</LinearLayout>
AddSubView--------------------------------------------------------
public class AddSubView extends LinearLayout implements View.OnClickListener {
private TextView sub;
private TextView add;
private EditText count;
private AddClickListener addClickListener;
private SubClickListener subClickListener;
private static final String TAG =
"AddSubView------------";
public AddSubView(Context context) {
this(context,
null);
}
public AddSubView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,
0);
}
public AddSubView(Context context, @Nullable AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.add_sub_layout,
this);
sub = view.findViewById(R.id.sub);
add = view.findViewById(R.id.add);
count = view.findViewById(R.id.count);
sub.setOnClickListener(
this);
add.setOnClickListener(
this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sub:
String c = count.getText().toString();
int i = Integer.parseInt(c);
if (i <=
0) {
return;
}
count.setText(--i +
"");
subClickListener.onSubClick(v,i);
Log.d(TAG,
"减-----" + i);
break;
case R.id.add:
String c1 = count.getText().toString();
int i1 = Integer.parseInt(c1);
count.setText(++i1 +
"");
addClickListener.onAddClick(v,i1);
Log.d(TAG,
"onClick() returned: " +
"加===");
break;
}
}
public int getCount() {
return Integer.parseInt(count.getText().toString().trim());
}
public void setCount(
int s) {
count.setText(s +
"");
}
public interface AddClickListener {
void onAddClick(View view,
int count);
}
public interface SubClickListener {
void onSubClick(View view,
int count);
}
public void setOnAddClickListener(AddClickListener addClickListener) {
this.addClickListener = addClickListener;
}
public void setOnSubClickListener(SubClickListener subClickListener) {
this.subClickListener = subClickListener;
}
}
SumUtils------------------------------------------------------------
public class SumUtils {
public static String
sum(List<GoBean.DataBean> list) {
double sum =
0;
for (
int i =
0; i < list.size(); i++) {
List<GoBean.ChildBean> list1 = list.
get(i).getList();
for (
int j =
0; j < list1.size(); j++) {
GoBean.ChildBean childBean = list1.
get(j);
if (childBean.isChildIsSelected()) {
sum += childBean.getPrice()*childBean.getNum();
}
}
}
return Double.toString(sum);
}
}
MainActivity---------------------------------------------------------
public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener {
private static final String TAG =
"MainActivity";
private ExpandableListView expan_list_view;
private CheckBox xuan;
private TextView sum;
private Button toJiSuan;
List<GoBean.DataBean> list;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IPresentermpl iPresentermpl =
new IPresentermpl();
iPresentermpl.showCartList(
new ICartModelmpl(),
this);
initView();
}
private void initView() {
expan_list_view = findViewById(R.id.expan_list_view);
xuan = findViewById(R.id.xuan);
sum = findViewById(R.id.sum);
toJiSuan = findViewById(R.id.toJiSuan);
xuan.setOnClickListener(
this);
toJiSuan.setOnClickListener(
this);
}
@Override
public void showCartList(List<GoBean.DataBean> list) {
this.list=list;
Log.d(TAG,
"showCartList:+++++++++++++++++++++++++++++++++++++++++++++++++ " + list.size());
myAdapter =
new MyAdapter(MainActivity.
this, list,
this);
expan_list_view.setAdapter(myAdapter);
int groupCount = expan_list_view.getCount();
for (
int i =
0; i < groupCount; i++) {
expan_list_view.expandGroup(i);
}
}
@Override
public void showCount(String count) {
sum.setText(
"总价是:" + count);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.xuan:
boolean
checked = xuan.isChecked();
for (
int i =
0; i < list.size(); i++) {
GoBean.DataBean dataBean = list.
get(i);
dataBean.setParentIsSelected(
checked);
List<GoBean.ChildBean> list3 = dataBean.getList();
for (
int j =
0; j < list3.size(); j++) {
list3.
get(j).setChildIsSelected(
checked);
}
}
myAdapter.notifyDataSetChanged();
String count = SumUtils.sum(list);
sum.setText(
"总价是:" + count);
break;
}
}
}
ExpandableListView适配器-----------------------------------------
public class MyAdapter extends BaseExpandableListAdapter{
private Context context;
private List<GoBean.DataBean> list;
private IView iView;
public MyAdapter(Context context, List<GoBean.DataBean> list,IView iView) {
this.context = context;
this.list = list;
this.iView=iView;
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(
int groupPosition) {
return list.
get(groupPosition).getList().size();
}
@Override
public Object
getGroup(
int groupPosition) {
return list.
get(groupPosition);
}
@Override
public Object
getChild(
int groupPosition,
int childPosition) {
return list.
get(groupPosition).getList().
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) {
ParentViewHolder parentViewHolder;
if (convertView ==
null) {
convertView = View.inflate(context, R.layout.item_parent,
null);
CheckBox parent_cb = convertView.findViewById(R.id.parent_cb);
TextView parent_title = convertView.findViewById(R.id.parent_title);
parentViewHolder =
new ParentViewHolder(parent_cb, parent_title);
convertView.setTag(parentViewHolder);
}
else {
parentViewHolder = (ParentViewHolder) convertView.getTag();
}
parentViewHolder.getParent_cb().setChecked(list.
get(groupPosition).isParentIsSelected());
parentViewHolder.getParent_title().setText(list.
get(groupPosition).getSellerName());
parentViewHolder.getParent_cb().setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean selected = list.
get(groupPosition).isParentIsSelected();
selected = !selected;
list.
get(groupPosition).setParentIsSelected(selected);
List<GoBean.ChildBean> list1 = MyAdapter.
this.list.
get(groupPosition).getList();
for (
int i =
0; i < list1.size(); i++) {
GoBean.ChildBean childBean = list1.
get(i);
childBean.setChildIsSelected(selected);
}
notifyDataSetChanged();
String sum = SumUtils.sum(list);
iView.showCount(sum);
}
});
return convertView;
}
@Override
public View
getChildView(final
int groupPosition, final
int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
MyChildViewHolder myChildViewHolder;
if (convertView ==
null) {
convertView = View.inflate(context, R.layout.child_layout,
null);
CheckBox childCb = convertView.findViewById(R.id.child_cb);
TextView childTitle = convertView.findViewById(R.id.child_title);
ImageView childIcon = convertView.findViewById(R.id.child_icon);
TextView childPrice = convertView.findViewById(R.id.child_price);
AddSubView addSubView = convertView.findViewById(R.id.add_sub_view);
myChildViewHolder =
new MyChildViewHolder(childCb, childTitle, childPrice, addSubView, childIcon);
convertView.setTag(myChildViewHolder);
}
else {
myChildViewHolder = (MyChildViewHolder) convertView.getTag();
}
myChildViewHolder.getChildCb().setChecked(list.
get(groupPosition).getList().
get(childPosition).isChildIsSelected());
myChildViewHolder.getChildTitle().setText(list.
get(groupPosition).getList().
get(childPosition).getTitle());
myChildViewHolder.getChildPrice().setText(list.
get(groupPosition).getList().
get(childPosition).getPrice() +
"");
myChildViewHolder.getAddSubView().setCount(list.
get(groupPosition).getList().
get(childPosition).getNum());
myChildViewHolder.getAddSubView().setOnAddClickListener(
new AddSubView.AddClickListener() {
@Override
public void onAddClick(View view,
int count) {
list.
get(groupPosition).getList().
get(childPosition).setNum(count);
boolean selected = list.
get(groupPosition).getList().
get(childPosition).isChildIsSelected();
if (selected){
String sum = SumUtils.sum(list);
iView.showCount(sum);
}
}
});
myChildViewHolder.getAddSubView().setOnSubClickListener(
new AddSubView.SubClickListener() {
@Override
public void onSubClick(View view,
int count) {
list.
get(groupPosition).getList().
get(childPosition).setNum(count);
boolean selected = list.
get(groupPosition).getList().
get(childPosition).isChildIsSelected();
if (selected){
String sum = SumUtils.sum(list);
iView.showCount(sum);
}
}
});
String images = list.
get(groupPosition).getList().
get(childPosition).getImages();
String icon_url = images.split(
"\\|")[
0];
Glide.with(context).load(icon_url).
into(myChildViewHolder.getChildIcon());
myChildViewHolder.getChildCb().setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean selected = list.
get(groupPosition).getList().
get(childPosition).isChildIsSelected();
selected = !selected;
list.
get(groupPosition).getList().
get(childPosition).setChildIsSelected(selected);
List<GoBean.ChildBean> list2 = MyAdapter.
this.list.
get(groupPosition).getList();
boolean flag =
true;
for (
int j =
0; j < list2.size(); j++) {
if (!list2.
get(j).isChildIsSelected()) {
flag =
false;
}
}
list.
get(groupPosition).setParentIsSelected(flag);
notifyDataSetChanged();
String sum = SumUtils.sum(list);
iView.showCount(sum);
}
});
return convertView;
}
@Override
public boolean
isChildSelectable(
int groupPosition,
int childPosition) {
return false;
}
class ParentViewHolder {
private CheckBox parent_cb;
private TextView parent_title;
public ParentViewHolder(CheckBox parent_cb, TextView parent_title) {
this.parent_cb = parent_cb;
this.parent_title = parent_title;
}
public CheckBox
getParent_cb() {
return parent_cb;
}
public void setParent_cb(CheckBox parent_cb) {
this.parent_cb = parent_cb;
}
public TextView
getParent_title() {
return parent_title;
}
public void setParent_title(TextView parent_title) {
this.parent_title = parent_title;
}
}
class MyChildViewHolder {
private CheckBox childCb;
private TextView childTitle;
private TextView childPrice;
private AddSubView addSubView;
private ImageView childIcon;
public MyChildViewHolder(CheckBox childCb, TextView childTitle, TextView childPrice, AddSubView addSubView, ImageView childIcon) {
this.childCb = childCb;
this.childTitle = childTitle;
this.childPrice = childPrice;
this.addSubView = addSubView;
this.childIcon = childIcon;
}
public CheckBox
getChildCb() {
return childCb;
}
public void setChildCb(CheckBox childCb) {
this.childCb = childCb;
}
public TextView
getChildTitle() {
return childTitle;
}
public void setChildTitle(TextView childTitle) {
this.childTitle = childTitle;
}
public TextView
getChildPrice() {
return childPrice;
}
public void setChildPrice(TextView childPrice) {
this.childPrice = childPrice;
}
public AddSubView
getAddSubView() {
return addSubView;
}
public void setAddSubView(AddSubView addSubView) {
this.addSubView = addSubView;
}
public ImageView
getChildIcon() {
return childIcon;
}
public void setChildIcon(ImageView childIcon) {
this.childIcon = childIcon;
}
}
}
Presenter层---------------------------------------------------------
public class IPresentermpl implements IPresenter{
private static final String TAG =
"IPresentermpl";
@Override
public void showCartList(ICartModel iCartModel, final IView iView) {
Map<String,String> map=
new HashMap<>();
map.put(
"uid",
"71");
iCartModel.getCartList(Httpfig.url, map,
new GetCartListListener() {
@Override
public void getCartSuccess(String json) {
Log.d(TAG,
"getCartSuccess: "+json);
Gson gson=
new Gson();
GoBean goBean = gson.fromJson(json, GoBean.class);
List<GoBean.DataBean> data = goBean.getData();
iView.showCartList(data);
}
@Override
public void getCartError(String error) {
Log.d(TAG,
"getCartError: 失败+++++++++++++++++++++++++++++++++++++");
}
});
}
}
Model层-------------------------------------------------------------
public class ICartModelmpl implements ICartModel {
private static final String TAG =
"ICartModelmpl";
@Override
public void getCartList(String url, Map<String, String> map, final GetCartListListener getCartListListener) {
OKHttpUtils okHttpUtils=
new OKHttpUtils();
okHttpUtils.okPost(url,map);
okHttpUtils.setOnLoginListener(
new OKHttpUtils.LoginListener() {
@Override
public void loginSuccess(String json) {
Log.d(TAG,
"loginSuccess: "+json);
getCartListListener.getCartSuccess(json);
}
@Override
public void loginError(String error) {
Log.d(TAG,
"loginSuccess:+++++++++++++++++++++++++++++失败 "+error);
getCartListListener.getCartError(error);
}
});
}
}