导入依赖
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'io.reactivex.rxjava2:rxjava:2.1.10'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'com.facebook.fresco:fresco:1.9.0'
implementation 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
各个接口
public interface IMainView {
void showShoppCart(ShoppCartBean shoppCartBean);
String uid();
}
public interface IModel {
void getDataShoppCart(Map map);
}
public interface IPresenter {
void showLoginToView(IModel iModel, IMainView iMainView);
void getDataModel(ShoppCartBean shoppCartBean);
}
布局
//activity_main布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".view.MainActivity">
<ExpandableListView
android:id="@+id/expan_able"
android:layout_width="match_parent"
android:groupIndicator="@null"
android:layout_weight="1"
android:layout_height="0dp">
ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/quan_cbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/total"
android:layout_width="0dp"
android:layout_weight="1"
android:text="合计:0"
android:layout_height="wrap_content" />
<TextView
android:background="#ff0000"
android:text="去结算(0)"
android:textStyle="bold"
android:gravity="center"
android:layout_width="100dp"
android:layout_height="40dp" />
LinearLayout>
LinearLayout>
//parent布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/parent_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/parent_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
LinearLayout>
//child布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/child_box"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/child_img"
android:layout_width="90dp"
android:layout_height="90dp" />
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginLeft="6dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/child_title"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/child_price"
android:textColor="#ff0000"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<wby.laowang.newshoppcart.view.AddSubView
android:id="@+id/addsub"
android:layout_marginLeft="15dp"
android:layout_width="110dp"
android:layout_height="30dp">
wby.laowang.newshoppcart.view.AddSubView>
LinearLayout>
LinearLayout>
<TextView
android:id="@+id/child_delete"
android:text="删除"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginRight="7dp"
android:background="#ff0000"
android:layout_width="50dp"
android:layout_height="70dp" />
LinearLayout>
//自定义View加减的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="@drawable/add_shape"
android:layout_height="wrap_content">
<TextView
android:id="@+id/sub"
android:text="-"
android:gravity="center"
android:background="@drawable/add_shape"
android:layout_width="30dp"
android:layout_height="30dp" />
<EditText
android:id="@+id/count"
android:background="@null"
android:layout_width="50dp"
android:gravity="center"
android:layout_height="30dp" />
<TextView
android:id="@+id/add"
android:text="+"
android:gravity="center"
android:background="@drawable/add_shape"
android:layout_width="30dp"
android:layout_height="30dp" />
LinearLayout>
Model层
private boolean parent_flag;
public boolean isParent_flag() {
return parent_flag;
}
public void setParent_flag(boolean parent_flag) {
this.parent_flag = parent_flag;
}
private boolean child_flag;
public boolean isChild_flag() {
return child_flag;
}
public void setChild_flag(boolean child_flag) {
this.child_flag = child_flag;
}
/**
* model的具体实现
*/
public class ModelFusion implements IModel {
private IPresenter iPresenter;
public ModelFusion(IPresenter iPresenter) {
this.iPresenter = iPresenter;
}
@Override
public void getDataShoppCart(Map map) {
RetrofitUtil retrofitUtil = RetrofitUtil.getInstance();
MyService myService = retrofitUtil.createRequest(MyService.class);
Observable observable = myService.getshoppcart(map);
observable.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ShoppCartBean shoppCartBean) {
iPresenter.getDataModel(shoppCartBean);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
Presenter层
/**
* Presenter的操作类
*/
public class PresenterFusion implements IPresenter {
private IMainView iMainView;
@Override
public void showLoginToView(IModel iModel, IMainView iMainView) {
this.iMainView = iMainView;
Map map = new HashMap<>();
map.put("uid",iMainView.uid());
iModel.getDataShoppCart(map);
}
@Override
public void getDataModel(ShoppCartBean shoppCartBean) {
iMainView.showShoppCart(shoppCartBean);
}
}
View层
public class MainActivity extends AppCompatActivity implements IMainView{
@BindView(R.id.expan_able)
ExpandableListView expanAble;
@BindView(R.id.quan_cbox)
CheckBox quanCbox;
@BindView(R.id.total)
TextView total;
private MyExpanAdapter myExpanAdapter;
private ShoppCartBean shopplist;
private String suid = "10756";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initdatas();
}
public void initdatas(){
PresenterFusion presenterFusion = new PresenterFusion();
presenterFusion.showLoginToView(new ModelFusion(presenterFusion),this);
quanCbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean quan_checked = quanCbox.isChecked();
List data = shopplist.getData();
for (int i = 0; i < data.size() ; i++){
data.get(i).setParent_flag(quan_checked);
List list = data.get(i).getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setChild_flag(quan_checked);
}
}
myExpanAdapter.notifyDataSetChanged();
double sum = 0;
for (int p = 0; p < data.size(); p++) {
List plist = shopplist.getData().get(p).getList();
for (int o = 0; o < plist.size(); o++) {
boolean child_flag = plist.get(o).isChild_flag();
if (child_flag) {
double price = plist.get(o).getPrice()*plist.get(o).getNum();
sum += price;
}
}
}
total.setText("合计:"+sum);
}
});
}
@Override
public void showShoppCart(ShoppCartBean shoppCartBean) {
this.shopplist = shoppCartBean;
myExpanAdapter = new MyExpanAdapter(MainActivity.this,shoppCartBean.getData());
expanAble.setAdapter(myExpanAdapter);
int count = expanAble.getCount();
for (int i = 0; i < count; i++) {
expanAble.expandGroup(i);
}
}
@Override
public String uid() {
return suid;
}
}
二级列表适配器
public class MyExpanAdapter extends BaseExpandableListAdapter {
private Context context;
private List slist;
public MyExpanAdapter(Context context, List slist) {
this.context = context;
this.slist = slist;
}
@Override
public int getGroupCount() {
return slist.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return slist.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return slist.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return slist.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 true;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final ParentViewHolder parentViewHolder;
if (convertView == null){
parentViewHolder = new ParentViewHolder();
convertView = View.inflate(context, R.layout.item_parent,null);
parentViewHolder.parent_box = convertView.findViewById(R.id.parent_box);
parentViewHolder.parent_title = convertView.findViewById(R.id.parent_title);
convertView.setTag(parentViewHolder);
}else {
parentViewHolder = (ParentViewHolder) convertView.getTag();
}
parentViewHolder.parent_title.setText(slist.get(groupPosition).getSellerName());
parentViewHolder.parent_box.setChecked(slist.get(groupPosition).isParent_flag());
parentViewHolder.parent_box.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean ischecked = parentViewHolder.parent_box.isChecked();
slist.get(groupPosition).setParent_flag(ischecked);
List list = slist.get(groupPosition).getList();
for (int i = 0; i < list.size(); i++) {
ShoppCartBean.DataBean.ListBean listBean = list.get(i);
listBean.setChild_flag(ischecked);
}
notifyDataSetChanged();
}
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final ChildViewHolder childViewHolder;
if (convertView == null){
childViewHolder = new ChildViewHolder();
convertView = View.inflate(context, R.layout.item_child,null);
childViewHolder.child_box = convertView.findViewById(R.id.child_box);
childViewHolder.child_delete = convertView.findViewById(R.id.child_delete);
childViewHolder.child_img = convertView.findViewById(R.id.child_img);
childViewHolder.child_price = convertView.findViewById(R.id.child_price);
childViewHolder.child_title = convertView.findViewById(R.id.child_title);
childViewHolder.addSubView = convertView.findViewById(R.id.addsub);
convertView.setTag(childViewHolder);
}else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
final List list = slist.get(groupPosition).getList();
childViewHolder.child_title.setText(list.get(childPosition).getTitle());
childViewHolder.child_price.setText("¥: "+list.get(childPosition).getPrice());
childViewHolder.child_box.setChecked(list.get(childPosition).isChild_flag());
String[] imags = list.get(childPosition).getImages().split("\\|");
Uri parse = Uri.parse(imags[0]);
childViewHolder.child_img.setImageURI(parse);
childViewHolder.addSubView.setCount(list.get(childPosition).getNum());
childViewHolder.addSubView.setOnAddClick(new AddSubView.OnAddSubClick() {
@Override
public void onAddSubClick() {
String count = childViewHolder.addSubView.getCount();
list.get(childPosition).setNum(Integer.parseInt(count));
notifyDataSetChanged();
}
});
childViewHolder.child_box.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean child_checked = childViewHolder.child_box.isChecked();
list.get(childPosition).setChild_flag(child_checked);
boolean ch_flag = true;
for (int i = 0; i < list.size(); i++){
boolean child_flag = list.get(i).isChild_flag();
if (child_flag == false){
ch_flag = false;
}
}
slist.get(groupPosition).setParent_flag(ch_flag);
notifyDataSetChanged();
}
});
childViewHolder.child_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
slist.remove(groupPosition);
list.remove(childPosition);
notifyDataSetChanged();
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
static class ParentViewHolder{
CheckBox parent_box;
TextView parent_title;
}
static class ChildViewHolder{
CheckBox child_box;
ImageView child_img;
TextView child_title;
TextView child_price;
TextView child_delete;
AddSubView addSubView;
}
}
自定义加减View
public class AddSubView extends LinearLayout implements View.OnClickListener {
private TextView add;
private TextView sub;
private EditText count;
private OnAddSubClick onAddClick;
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_layout, this);
add = view.findViewById(R.id.add);
sub = view.findViewById(R.id.sub);
count = view.findViewById(R.id.count);
add.setOnClickListener(this);
sub.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.add:
add();
onAddClick.onAddSubClick();
break;
case R.id.sub:
sub();
onAddClick.onAddSubClick();
break;
}
}
public void add(){
String s = count.getText().toString();
int i = Integer.parseInt(s);
i++;
count.setText(i+"");
}
public void sub(){
String s = count.getText().toString();
int i = Integer.parseInt(s);
if (i>=1){
i--;
count.setText(i+"");
}
}
public String getCount(){
return count.getText().toString();
}
public void setCount(int cousize){
count.setText(cousize+"");
}
public interface OnAddSubClick{
void onAddSubClick();
}
public void setOnAddClick(OnAddSubClick onAddClick){
this.onAddClick = onAddClick;
}
}