首先先导入依赖
implementation ‘com.android.support:recyclerview-v7:28.0.0’
implementation ‘com.squareup.okhttp3:okhttp:3.12.0’
implementation ‘com.google.code.gson:gson:2.8.5’
implementation ‘com.github.bumptech.glide:glide:4.8.0’
别忘了网络权限
配置MVP框架
//IView
package com.example.gouwuche.view;
public interface iView {
void success( T success);
void error( T error);
}
//okHTTPUtils工具类
package com.example.gouwuche.utils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class Myutils {
static class getdata{
public static final Myutils utils=new Myutils();
}
public static Myutils data(){
return getdata.utils;
}
public static void Asyncget(String nurl, Callback callback){
OkHttpClient okHttpClient=new OkHttpClient();
Request request=new Request.Builder().url(nurl).build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
}
//回调的接口 Callback
package com.example.gouwuche.callback;
public interface MyCallBack {
void success(T success);
void error(T error);
}
//model接口
package com.example.gouwuche.model;
import com.example.gouwuche.callback.MyCallBack;
public interface model {
void setRequest(String url, MyCallBack callBack);
}
//model接口的实现类
package com.example.gouwuche.model;
import android.os.Handler;
import android.os.Message;
import com.example.gouwuche.bean.result;
import com.example.gouwuche.callback.MyCallBack;
import com.example.gouwuche.utils.Myutils;
import com.google.gson.Gson;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class modelimpl implements model {
private MyCallBack callBack;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==100)
{
result result= (com.example.gouwuche.bean.result) msg.obj;
callBack.success(result);
}
}
};
@Override
public void setRequest(String url, MyCallBack callBack) {
this.callBack=callBack;
Myutils.Asyncget(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String s = response.body().string();
Gson gson=new Gson();
result result = gson.fromJson(s, result.class);
Message message=new Message();
message.what=100;
message.obj=result;
handler.sendMessage(message);
}
});
}
}
//presenter接口
package com.example.gouwuche.presenter;
public interface presenter {
void setData(String url);
}
//实现presenter接口
package com.example.gouwuche.presenter;
import com.example.gouwuche.callback.MyCallBack;
import com.example.gouwuche.model.modelimpl;
import com.example.gouwuche.view.iView;
public class presenterimpl implements presenter{
private modelimpl modelimpl;
private iView iView;
public presenterimpl(com.example.gouwuche.view.iView iView) {
this.iView = iView;
modelimpl=new modelimpl();
}
@Override
public void setData(String url) {
modelimpl.setRequest(url, new MyCallBack() {
@Override
public void success(Object success) {
iView.success(success);
}
@Override
public void error(Object error) {
iView.error(error);
}
});
}
}
//mainActivity的xml
<ExpandableListView
android:id="@+id/expand"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ExpandableListView>
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:background="#ccc"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="全选" />
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="总数量" />
<TextView
android:id="@+id/js"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="去结算" />
</LinearLayout>
//mainActivity类
package com.example.gouwuche;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.gouwuche.adapter.MyAdapter;
import com.example.gouwuche.bean.result;
import com.example.gouwuche.presenter.presenterimpl;
import com.example.gouwuche.view.iView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements iView {
private String murl = "http://www.wanandroid.com/tools/mockapi/6523/restaurant-list";
private ExpandableListView expand;
private CheckBox cb;
private TextView count;
private TextView js;
private List<result.DataBean> data = new ArrayList<>();
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
presenterimpl presenterimpl = new presenterimpl(this);
presenterimpl.setData(murl);
adapter = new MyAdapter(MainActivity.this, data);
expand.setAdapter(adapter);
expand.setGroupIndicator(null);
adapter.setcallback(new MyAdapter.adaptercallback() {
@Override
//group
public void setGroupCheck(int groupPosition) {
boolean b = adapter.childxz(groupPosition);
adapter.childAllCheck(groupPosition, !b);
adapter.notifyDataSetChanged();
sxBottom();
}
@Override
//child
public void setChildCheck(int groupPosition, int childPosition) {
boolean b = adapter.childXZ(groupPosition, childPosition);
adapter.childFz(groupPosition,childPosition,!b);
adapter.notifyDataSetChanged();
sxBottom();
}
@Override
//给商品赋值 刷新适配器
public void setNumber(int groupPosition, int childPosition, int number) {
adapter.setShangPingNumber(groupPosition,childPosition,number);
adapter.notifyDataSetChanged();
sxBottom();
}
});
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean b = adapter.qbXz();
adapter.qdxz(!b);
adapter.notifyDataSetChanged();
sxBottom();
}
});
}
//底部刷新
private void sxBottom() {
boolean b = adapter.qbXz();
cb.setChecked(b);
float price = adapter.jiSuanPrice();
js.setText("总价是:"+price);
int number = adapter.jiSuanNumber();
count.setText("去结算("+number+")");
}
private void initview() {
expand = findViewById(R.id.expand);
cb = findViewById(R.id.cb);
count = findViewById(R.id.count);
js = findViewById(R.id.js);
}
@Override
public void success(Object success) {
result result = (com.example.gouwuche.bean.result) success;
data.addAll(result.getData());
}
@Override
public void error(Object error) {
}
}
//adapter适配器
package com.example.gouwuche.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.gouwuche.R;
import com.example.gouwuche.bean.result;
import com.example.gouwuche.myView.MyView;
import java.util.List;
public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
private List<result.DataBean> data;
public MyAdapter(Context context, List<result.DataBean> data) {
this.context = context;
this.data = data;
}
@Override
public int getGroupCount() {
return data.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return data.get(groupPosition).getSpus().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) {
groupHolder holder1 = null;
if (convertView == null) {
holder1 = new groupHolder();
convertView = View.inflate(context, R.layout.gourpitem, null);
holder1.cb = convertView.findViewById(R.id.cb);
holder1.tv = convertView.findViewById(R.id.tv);
convertView.setTag(holder1);
} else {
holder1 = (groupHolder) convertView.getTag();
}
holder1.tv.setText(data.get(groupPosition).getName());
//判断child是否全部被选中 如果全部被选中就给group选中
boolean childxz = childxz(groupPosition);
holder1.cb.setChecked(childxz);
holder1.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adaptercallback != null) {
adaptercallback.setGroupCheck(groupPosition);
}
}
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
childHolder holder2 = null;
if (convertView == null) {
holder2 = new childHolder();
convertView = View.inflate(context, R.layout.childitem, null);
holder2.img = convertView.findViewById(R.id.img);
holder2.cb1 = convertView.findViewById(R.id.cb1);
holder2.title = convertView.findViewById(R.id.title);
holder2.price = convertView.findViewById(R.id.price);
holder2.jj_view=convertView.findViewById(R.id.jj_view);
convertView.setTag(holder2);
} else {
holder2 = (childHolder) convertView.getTag();
}
Glide.with(context).load(data.get(groupPosition).getSpus().get(childPosition).getPic_url()).into(holder2.img);
holder2.title.setText(data.get(groupPosition).getSpus().get(childPosition).getName());
holder2.price.setText("¥:" + data.get(groupPosition).getSpus().get(childPosition).getSkus().get(0).getPrice());
holder2.jj_view.cs(data.get(groupPosition).getSpus().get(childPosition).getPraise_num());
holder2.jj_view.setOnChange(new MyView.OnCountChange() {
@Override
public void setCount(int count) {
if (adaptercallback != null) {
adaptercallback.setNumber(groupPosition,childPosition,count);
}
}
});
//给他赋值
holder2.cb1.setChecked(data.get(groupPosition).getSpus().get(childPosition).isChildChecked());
holder2.cb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adaptercallback!=null){
adaptercallback.setChildCheck(groupPosition,childPosition);
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class groupHolder {
private CheckBox cb;
private TextView tv;
}
class childHolder {
private CheckBox cb1;
private ImageView img;
private TextView title;
private TextView price;
private MyView jj_view;
}
//看child是不是都选中了 如果都选中了 就返回true
public boolean childxz(int groupPosition) {
result.DataBean dataBean = data.get(groupPosition);
List<result.DataBean.SpusBean> spus = dataBean.getSpus();
for (int j = 0; j < spus.size(); j++) {
boolean b = spus.get(j).isChildChecked();
if (!b) {
return false;
}
}
return true;
}
//如果返回的true给他选中
public void childAllCheck(int groupPosition, boolean b) {
result.DataBean dataBean = data.get(groupPosition);
List<result.DataBean.SpusBean> spus = dataBean.getSpus();
for (int i = 0; i < spus.size(); i++) {
result.DataBean.SpusBean spusBean = spus.get(i);
spusBean.setChildChecked(b);
}
}
//这个方法用来判断所有的商品是否被选中(包括group和child)
public boolean qbXz() {
for (int i = 0; i < data.size(); i++) {
result.DataBean dataBean = data.get(i);
List<result.DataBean.SpusBean> spus = dataBean.getSpus();
for (int j = 0; j < spus.size(); j++) {
if (!spus.get(j).isChildChecked()) {
return false;
}
}
}
return true;
}
//全都给他选中
public void qdxz(boolean b){
for (int i = 0; i < data.size(); i++) {
result.DataBean dataBean = data.get(i);
List<result.DataBean.SpusBean> spus = dataBean.getSpus();
for (int j = 0; j < spus.size(); j++) {
spus.get(j).setChildChecked(b);
}
}
}
//现在要写判断child是否被选中
public boolean childXZ(int groupPosition,int childPosition){
result.DataBean.SpusBean spusBean = data.get(groupPosition).getSpus().get(childPosition);
if (spusBean.isChildChecked()){
return true;
}
return false;
}
//下边紧接着给他写赋值的操作
public void childFz(int groupPosition,int childPosition,boolean b){
result.DataBean.SpusBean spusBean = data.get(groupPosition).getSpus().get(childPosition);
spusBean.setChildChecked(b);
}
//计算选中商品的价格
public float jiSuanPrice(){
float allPrice = 0;
for (int i = 0; i < data.size(); i++) {
result.DataBean dataBean = data.get(i);
List<result.DataBean.SpusBean> spus = dataBean.getSpus();
for (int j = 0; j < spus.size(); j++) {
if (spus.get(j).isChildChecked()) {
allPrice+=spus.get(j).getPraise_num()*Float.parseFloat(spus.get(j).getSkus().get(0).getPrice());
}
}
}
return allPrice;
}
//计算数量
public int jiSuanNumber(){
int allGood = 0;
for (int i = 0; i < data.size(); i++) {
result.DataBean dataBean = data.get(i);
List<result.DataBean.SpusBean> spus = dataBean.getSpus();
for (int j = 0; j < spus.size(); j++) {
if (spus.get(j).isChildChecked()) {
allGood+=spus.get(j).getPraise_num();
}
}
}
return allGood;
}
//给商品数量进行赋值
public void setShangPingNumber(int groupPosition, int childPosition, int number) {
result.DataBean.SpusBean spusBean = data.get(groupPosition).getSpus().get(childPosition);
spusBean.setPraise_num(number);
}
public interface adaptercallback {
void setGroupCheck(int groupPosition);
void setChildCheck(int groupPosition, int childPosition);
void setNumber(int groupPosition, int childPosition, int number);
}
private adaptercallback adaptercallback;
public void setcallback(adaptercallback adaptercallback) {
this.adaptercallback = adaptercallback;
}
}
//自定义View的xml
<?xml version="1.0" encoding="utf-8"?><ImageView
android:id="@+id/jian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/aaa"/>
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_margin="5dp"
android:text="12"
/>
<ImageView
android:id="@+id/jia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bb"/>
//自定义view
package com.example.gouwuche.myView;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.gouwuche.R;
public class MyView extends LinearLayout implements View.OnClickListener {
private ImageView jia;
private ImageView jian;
private TextView count;
private int mcount;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.jiajian, this);
initViews();
}
private void initViews() {
jia = findViewById(R.id.jia);
jian = findViewById(R.id.jian);
count = findViewById(R.id.count);
jia.setOnClickListener(this);
jian.setOnClickListener(this);
}
public void cs(int number){
this.mcount=number;
count.setText(number+"");
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.jia:
mcount++;
count.setText(mcount+"");
if(mCountChange!=null)
{
mCountChange.setCount(mcount);
}
break;
case R.id.jian:
if(mcount>0)
{
mcount--;
count.setText(mcount+"");
if(mCountChange!=null)
{
mCountChange.setCount(mcount);
}
}
break;
}
}
public interface OnCountChange {
void setCount(int count);
}
private OnCountChange mCountChange;
public void setOnChange(OnCountChange countChange) {
this.mCountChange = countChange;
}
}
//父布局
<?xml version="1.0" encoding="utf-8"?><CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="背后看日本更好听不了"/>
//子布局
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="部分金额烤肉拌饭而分"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥:123"
android:textColor="#f00"/>
<com.example.gouwuche.myView.MyView
android:id="@+id/jj_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="30dp"
android:gravity="center_vertical"></com.example.gouwuche.myView.MyView>
</LinearLayout>
</LinearLayout>