请求网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
依赖
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:okhttp:3.7.0'
implementation 'com.squareup.okio:okio:1.12.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
okhttp(gson解析,Model层)
package com.example.day20.model;
import android.os.Handler;
import android.os.Message;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class HttpUtile {
private final OkHttpClient okhttp;
public HttpUtile(){
okhttp = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.build();
};
public static HttpUtile getInHttpUtile(){
return HttpUtiles.httpUtile;
}
private static class HttpUtiles{
private static HttpUtile httpUtile=new HttpUtile();
}
private CallBack callBacks;
public void getData(String path, final Class<NewBean> newBeanClass, final CallBack<NewBean> callBack) {
this.callBacks=callBack;
Request request = new Request.Builder().get().url(path).build();
Call call = okhttp.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callBacks.onFailure("错误");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Gson gson=new Gson();
NewBean newBean = gson.fromJson(string, newBeanClass);
Message message=new Message();
message.obj=newBean;
handler.sendMessage(message);
}
});
}
public interface CallBack<D>{
void onResponse(D d);
void onFailure(String s);
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
NewBean obj=(NewBean) msg.obj;
callBacks.onResponse(obj);
}
};
}
Basepresenter(P层)
package com.example.day20.presenter;
import com.example.day20.view.interfaces.IBaseView;
public class BasePresenter<T extends IBaseView> {
private T mview;
public T getMview() {
return mview;
}
public void setMview(T mview) {
this.mview = mview;
}
public void datch(){
mview=null;
}
}
MainPresenter(P层)
package com.example.day20.presenter;
import com.example.day20.model.HttpUtile;
import com.example.day20.model.NewBean;
import com.example.day20.view.interfaces.IMainView;
public class MainPresenter extends BasePresenter<IMainView>{
public void setData(String path) {
HttpUtile.getInHttpUtile().getData(path,NewBean.class, new HttpUtile.CallBack<NewBean>() {
@Override
public void onResponse(NewBean newBean) {
getMview().onSuccess(newBean);
}
@Override
public void onFailure(String s) {
}
});
}
}
MainActivity(view层)
package com.example.day20.view.activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import com.example.day20.R;
import com.example.day20.model.NewBean;
import com.example.day20.presenter.MainPresenter;
import com.example.day20.view.adapter.MyAdapter;
import com.example.day20.view.interfaces.IMainView;
public class MainActivity extends BaseActivity implements IMainView<NewBean> {
private String path="http://www.wanandroid.com/tools/mockapi/6523/restaurant-list";
private MainPresenter mainPresenter;
private ExpandableListView expandableListView;
private MyAdapter myAdapter;
private CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void initData() {
mainPresenter = new MainPresenter();
mainPresenter.setMview(this);
mainPresenter.setData(path);
}
@Override
protected void initView() {
expandableListView = findViewById(R.id.view);
checkBox = findViewById(R.id.checkboxall);
}
@Override
protected int Layout() {
return R.layout.activity_main;
}
@Override
public void onSuccess(NewBean newBean) {
myAdapter = new MyAdapter(this);
myAdapter.setData(newBean);
expandableListView.setAdapter(myAdapter);
myAdapter.setView(checkBox);
}
@Override
public void onEorr(String s) {
}
}
BaseActivity(view层)
package com.example.day20.view.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(Layout());
initView();
initData();
}
protected abstract void initData();
protected abstract void initView();
protected abstract int Layout();
}
Adapter(view层)
package com.example.day20.view.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.day20.R;
import com.example.day20.model.NewBean;
public class MyAdapter extends BaseExpandableListAdapter {
private Context mContext;
private NewBean mnewBean;
public MyAdapter(Context context){
this.mContext = context;
}
public void setData(NewBean newBean) {
this.mnewBean=newBean;
}
@Override
public int getGroupCount() {
if(mnewBean==null){
return 0;
}
return mnewBean.getData().size();
}
@Override
public int getChildrenCount(int groupPosition) {
if(mnewBean==null){
return 0;
}
return mnewBean.getData().get(groupPosition).getSpus().size();
}
private CheckBox mcheckbox;
public void setView(final CheckBox checkBox) {
this.mcheckbox=checkBox;
mcheckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBoxall=(CheckBox) v;
selectAll(checkBoxall.isChecked());
}
});
}
@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 true;
}
//父类
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
parentViewHolder parentViewHolder;
if(convertView==null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_group, null);
parentViewHolder=new parentViewHolder();
parentViewHolder.shopName=convertView.findViewById(R.id.shopName);
parentViewHolder.checkBox=convertView.findViewById(R.id.checkbox_parent);
convertView.setTag(parentViewHolder);
}else{
parentViewHolder =(parentViewHolder) convertView.getTag();
}
parentViewHolder.checkBox.setChecked(mnewBean.getData().get(groupPosition).isCheck());
parentViewHolder.shopName.setText(mnewBean.getData().get(groupPosition).getName());
parentViewHolder.checkBox.setTag(groupPosition);
parentViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox=(CheckBox) v;
boolean checked = checkBox.isChecked();
int groupPosition = Integer.parseInt(checkBox.getTag().toString());
mnewBean.getData().get(groupPosition).setCheck(checked);
selectGroup(groupPosition,checked);
boolean selectAllGroup = isSelectAllGroup();
mcheckbox.setChecked(selectAllGroup);
notifyDataSetChanged();
}
});
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
childViewHolder childViewHolder;
if(convertView==null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_child, null);
childViewHolder=new childViewHolder();
childViewHolder.goodsName=convertView.findViewById(R.id.goodsName);
childViewHolder.checkBox=convertView.findViewById(R.id.checkbox_child);
childViewHolder.image=convertView.findViewById(R.id.child_image);
convertView.setTag(childViewHolder);
}else{
childViewHolder =(childViewHolder) convertView.getTag();
}
childViewHolder.checkBox.setChecked(mnewBean.getData().get(groupPosition).getSpus().get(childPosition).isCheck());
childViewHolder.goodsName.setText(mnewBean.getData().get(groupPosition).getSpus().get(childPosition).getName());
Glide.with(mContext).load(mnewBean.getData().get(groupPosition).getSpus().get(childPosition).getPic_url()).into(childViewHolder.image);
childViewHolder.checkBox.setTag(groupPosition+"#"+childPosition);
childViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox=(CheckBox) v;
String tag =(String) v.getTag();
int groupPosition = Integer.parseInt(tag.split("#")[0]);
int childPosition = Integer.parseInt(tag.split("#")[1]);
NewBean.DataBean dataBean = mnewBean.getData().get(groupPosition);
NewBean.DataBean.SpusBean spusBean = dataBean.getSpus().get(childPosition);
spusBean.setCheck(checkBox.isChecked());
boolean selectGroup = isSelectGroup(groupPosition);
dataBean.setCheck(selectGroup);
notifyDataSetChanged();
}
});
return convertView;
}
private boolean isSelectGroup(int groupPosition){
for (int i = 0; i < mnewBean.getData().get(groupPosition).getSpus().size(); i++) {
NewBean.DataBean.SpusBean spusBean = mnewBean.getData().get(groupPosition).getSpus().get(i);
boolean check = spusBean.isCheck();
if (!check){
return false;
}
}
return true;
}
//判断是否全选 父类全选的话 全选被选中
private boolean isSelectAllGroup(){
for (int i = 0; i < mnewBean.getData().size(); i++) {
NewBean.DataBean dataBean = mnewBean.getData().get(i);
boolean check = dataBean.isCheck();
if (!check) {
return false;
}
}
return true;
}
//选中父类 子类也被选中
public void selectGroup(int groupPosition,boolean isChecked){
for (int i = 0; i < mnewBean.getData().get(groupPosition).getSpus().size(); i++) {
NewBean.DataBean.SpusBean listBean = mnewBean.getData().get(groupPosition).getSpus().get(i);
listBean.setCheck(isChecked);
}
}
//判断是不是全选
private void selectAll(boolean checked) {
for (int i = 0; i <mnewBean.getData().size() ; i++) {
NewBean.DataBean dataBean = mnewBean.getData().get(i);
dataBean.setCheck(checked);
for (int j = 0; j <dataBean.getSpus().size() ; j++) {
NewBean.DataBean.SpusBean spusBean = dataBean.getSpus().get(j);
spusBean.setCheck(checked);
}
}
notifyDataSetChanged();
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class parentViewHolder{
TextView shopName;
CheckBox checkBox;
}
class childViewHolder{
TextView goodsName;
CheckBox checkBox;
ImageView image;
}
}
主xml
<?xml version="1.0" encoding="utf-8"?>
<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.MainActivity">
<ExpandableListView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
<LinearLayout
android:id="@+id/linear_bottom"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:background="#999999"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkboxall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<TextView
android:id="@+id/priceall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="¥100" />
</LinearLayout>
</RelativeLayout>
父类的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<CheckBox
android:id="@+id/checkbox_parent"
android:layout_width="wrap_content"
android:focusable="false"
android:clickable="true"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/shopName"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
子类的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkbox_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"/>
<ImageView
android:id="@+id/child_image"
android:layout_width="60dp"
android:layout_height="60dp"/>
<TextView
android:id="@+id/goodsName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="6666"/>
</LinearLayout>