先添加权限:
<uses-permission android:name="android.permission.INTERNET" />
添加第三方需要用到的依赖:
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'org.greenrobot:eventbus:3.1.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
//上布局文件:
//主布局文件
<?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="vertical"
tools:context="mvpframework.bwie.com.a1509agoodcart.view.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#990000ff"
android:gravity="center"
android:text="购物车"
android:textColor="#ff3660"
android:textSize="25sp" />
<ExpandableListView
android:id="@+id/elv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/checkbox2"
android:gravity="center_vertical"
android:text="全选"
android:textSize="20sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="合计 :" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:paddingRight="10dp"
android:text="0"
android:textColor="@android:color/holo_red_light" />
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:padding="10dp"
android:text="结算(0)"
android:textColor="@android:color/white" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
//activity_other
<?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="vertical"
tools:context="mvpframework.bwie.com.a1509agoodcart.view.OtherActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#990000ff"
android:gravity="center"
android:text="购物车"
android:textColor="#ff3660"
android:textSize="25sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/checkbox2"
android:gravity="center_vertical"
android:text="全选"
android:textSize="20sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="合计 :" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:paddingRight="10dp"
android:text="0"
android:textColor="@android:color/holo_red_light" />
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:padding="10dp"
android:text="结算(0)"
android:textColor="@android:color/white" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
//child
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:focusable="false" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_tel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="iphone6" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="什么手机" />
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="2016-12-10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tv_pri"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥3000.00" />
<mvpframework.bwie.com.a1509agoodcart.MyView
android:id="@+id/mv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/tv_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除" />
</LinearLayout>
//item_child_market
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:focusable="false" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_tel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="iphone6" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="什么手机" />
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="2016-12-10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tv_pri"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥3000.00" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_del"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/shopcart_minus_grey" />
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@drawable/shopcart_add_btn"
android:paddingBottom="2dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="2dp"
android:text="1" />
<ImageView
android:id="@+id/iv_add"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:src="@drawable/shopcart_add_red" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/tv_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除" />
</LinearLayout>
//item_parent_market
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:focusable="false" />
<TextView
android:id="@+id/tv_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="标记" />
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="12345678" />
</LinearLayout>
//myview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_del"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/shopcart_minus_grey" />
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@drawable/shopcart_add_btn"
android:paddingBottom="2dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="2dp"
android:text="1" />
<ImageView
android:id="@+id/iv_add"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:src="@drawable/shopcart_add_red" />
</LinearLayout>
//开始上的代码:net包:
package mvpframework.bwie.com.a1509agoodcart.net;
/**
* Created by peng on 2017/11/16.
*/
public interface Api {
public static final String url = "http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=evaluation";
}
package mvpframework.bwie.com.a1509agoodcart.net;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
/**
* Created by peng on 2017/11/16.
*/
public class HttpUtils {
private static volatile HttpUtils httpUtils;
private final OkHttpClient client;
private HttpUtils() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
client = new OkHttpClient.Builder()
.addInterceptor(logging)
.build();
}
public static HttpUtils getHttpUtils() {
if (httpUtils == null) {
synchronized (HttpUtils.class) {
if (httpUtils == null) {
httpUtils = new HttpUtils();
}
}
}
return httpUtils;
}
/**
* GET请求
*
* @param url
* @param callback
*/
public void doGet(String url, Callback callback) {
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(callback);
}
}
package mvpframework.bwie.com.a1509agoodcart.net;
/**
* Created by peng on 2017/11/16.
*/
public interface OnNetListener<T> {
public void onSuccess(T t);
public void onFailure(Exception e);
}
//接下来是model层:
package mvpframework.bwie.com.a1509agoodcart.mode;
import android.os.Handler;
import android.os.Looper;
import com.google.gson.Gson;
import java.io.IOException;
import mvpframework.bwie.com.a1509agoodcart.bean.GoosBean;
import mvpframework.bwie.com.a1509agoodcart.net.Api;
import mvpframework.bwie.com.a1509agoodcart.net.HttpUtils;
import mvpframework.bwie.com.a1509agoodcart.net.OnNetListener;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by peng on 2017/11/16.
*/
public class MainModel implements IMainModel {
private Handler handler = new Handler(Looper.getMainLooper());
public void getGoods(final OnNetListener<GoosBean> onNetListener) {
HttpUtils.getHttpUtils().doGet(Api.url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
final GoosBean goosBean = new Gson().fromJson(string, GoosBean.class);
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.onSuccess(goosBean);
}
});
}
});
}
}
package mvpframework.bwie.com.a1509agoodcart.mode;
import mvpframework.bwie.com.a1509agoodcart.bean.GoosBean;
import mvpframework.bwie.com.a1509agoodcart.net.OnNetListener;
/**
* Created by peng on 2017/11/16.
*/
public interface IMainModel {
public void getGoods(OnNetListener<GoosBean> onNetListener);
}
//Prensenter层:
package mvpframework.bwie.com.a1509agoodcart.presenter;
import java.util.ArrayList;
import java.util.List;
import mvpframework.bwie.com.a1509agoodcart.bean.GoosBean;
import mvpframework.bwie.com.a1509agoodcart.mode.IMainModel;
import mvpframework.bwie.com.a1509agoodcart.mode.MainModel;
import mvpframework.bwie.com.a1509agoodcart.net.OnNetListener;
import mvpframework.bwie.com.a1509agoodcart.view.IMainActivity;
/**
* Created by peng on 2017/11/16.
*/
public class MainPresenter {
private final IMainModel imainModel;
private final IMainActivity iMainActivity;
public MainPresenter(IMainActivity iMainActivity) {
this.iMainActivity = iMainActivity;
imainModel = new MainModel();
}
public void getGoods2() {
imainModel.getGoods(new OnNetListener<GoosBean>() {
@Override
public void onSuccess(GoosBean goosBean) {
List<GoosBean.DataBean.DatasBean> list = new ArrayList<GoosBean.DataBean.DatasBean>();
List<GoosBean.DataBean> data = goosBean.getData();
for (int i = 0; i < data.size(); i++) {
List<GoosBean.DataBean.DatasBean> datas = data.get(i).getDatas();
list.addAll(datas);
}
iMainActivity.showList(list);
}
@Override
public void onFailure(Exception e) {
}
});
}
public void getGoods() {
imainModel.getGoods(new OnNetListener<GoosBean>() {
@Override
public void onSuccess(GoosBean goosBean) {
//List<GoosBean.DataBean> groupList, List<List<GoosBean.DataBean.DatasBean>> childList
List<GoosBean.DataBean> dataBean = goosBean.getData();
List<List<GoosBean.DataBean.DatasBean>> childList = new ArrayList<List<GoosBean.DataBean.DatasBean>>();
for (int i = 0; i < dataBean.size(); i++) {
List<GoosBean.DataBean.DatasBean> datas = dataBean.get(i).getDatas();
childList.add(datas);
}
iMainActivity.showList(dataBean, childList);
}
@Override
public void onFailure(Exception e) {
}
});
}
}
//view层:
package mvpframework.bwie.com.a1509agoodcart.view;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.List;
import mvpframework.bwie.com.a1509agoodcart.R;
import mvpframework.bwie.com.a1509agoodcart.adapter.RvAdapter;
import mvpframework.bwie.com.a1509agoodcart.bean.GoosBean;
import mvpframework.bwie.com.a1509agoodcart.eventbusevent.MessageEvent;
import mvpframework.bwie.com.a1509agoodcart.eventbusevent.PriceAndCountEvent;
import mvpframework.bwie.com.a1509agoodcart.presenter.MainPresenter;
public class OtherActivity extends AppCompatActivity implements IMainActivity {
private RecyclerView mRv;
private CheckBox mCheckbox2;
/**
* 0
*/
private TextView mTvPrice;
/**
* 结算(0)
*/
private TextView mTvNum;
private MainPresenter mainPresenter;
private RvAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
EventBus.getDefault().register(this);
mainPresenter = new MainPresenter(this);
initView();
mRv.setLayoutManager(new LinearLayoutManager(this));
//先请求接口数据
mainPresenter.getGoods2();
mCheckbox2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAdapter.allSelect(mCheckbox2.isChecked());
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe
public void onMessageEvent(MessageEvent event) {
mCheckbox2.setChecked(event.isChecked());
}
@Subscribe
public void onMessageEvent(PriceAndCountEvent event) {
mTvNum.setText("结算(" + event.getCount() + ")");
mTvPrice.setText(event.getPrice() + "");
}
private void initView() {
mRv = (RecyclerView) findViewById(R.id.rv);
mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2);
mTvPrice = (TextView) findViewById(R.id.tv_price);
mTvNum = (TextView) findViewById(R.id.tv_num);
}
//是给MainActivity用的方法,此处不必管它
@Override
public void showList(List<GoosBean.DataBean> groupList, List<List<GoosBean.DataBean.DatasBean>> childList) {
}
@Override
public void showList(List<GoosBean.DataBean.DatasBean> list) {
myAdapter = new RvAdapter(this, list);
mRv.setAdapter(myAdapter);
}
}
package mvpframework.bwie.com.a1509agoodcart.view;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.List;
import mvpframework.bwie.com.a1509agoodcart.R;
import mvpframework.bwie.com.a1509agoodcart.adapter.MyAdapter;
import mvpframework.bwie.com.a1509agoodcart.bean.GoosBean;
import mvpframework.bwie.com.a1509agoodcart.eventbusevent.MessageEvent;
import mvpframework.bwie.com.a1509agoodcart.eventbusevent.PriceAndCountEvent;
import mvpframework.bwie.com.a1509agoodcart.presenter.MainPresenter;
public class MainActivity extends AppCompatActivity implements IMainActivity {
private ExpandableListView mElv;
private CheckBox mCheckbox2;
/**
* 0
*/
private TextView mTvPrice;
/**
* 结算(0)
*/
private TextView mTvNum;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
initView();
new MainPresenter(this).getGoods();
mCheckbox2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.changeAllListCbState(mCheckbox2.isChecked());
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
private void initView() {
mElv = (ExpandableListView) findViewById(R.id.elv);
mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2);
mTvPrice = (TextView) findViewById(R.id.tv_price);
mTvNum = (TextView) findViewById(R.id.tv_num);
}
@Override
public void showList(List<GoosBean.DataBean> groupList, List<List<GoosBean.DataBean.DatasBean>> childList) {
adapter = new MyAdapter(this, groupList, childList);
mElv.setAdapter(adapter);
mElv.setGroupIndicator(null);
//默认让其全部展开
for (int i = 0; i < groupList.size(); i++) {
mElv.expandGroup(i);
}
}
@Override
public void showList(List<GoosBean.DataBean.DatasBean> list) {
}
@Subscribe
public void onMessageEvent(MessageEvent event) {
mCheckbox2.setChecked(event.isChecked());
}
@Subscribe
public void onMessageEvent(PriceAndCountEvent event) {
mTvNum.setText("结算(" + event.getCount() + ")");
mTvPrice.setText(event.getPrice() + "");
}
}
package mvpframework.bwie.com.a1509agoodcart.view;
import java.util.List;
import mvpframework.bwie.com.a1509agoodcart.bean.GoosBean;
/**
* Created by peng on 2017/11/16.
*/
public interface IMainActivity {
public void showList(List<GoosBean.DataBean> groupList, List<List<GoosBean.DataBean.DatasBean>> childList);
public void showList(List<GoosBean.DataBean.DatasBean> list);
}
//适配器:
package mvpframework.bwie.com.a1509agoodcart.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import java.util.List;
import mvpframework.bwie.com.a1509agoodcart.MyView;
import mvpframework.bwie.com.a1509agoodcart.R;
import mvpframework.bwie.com.a1509agoodcart.bean.GoosBean;
import mvpframework.bwie.com.a1509agoodcart.eventbusevent.MessageEvent;
import mvpframework.bwie.com.a1509agoodcart.eventbusevent.PriceAndCountEvent;
/**
* Created by peng on 2017/11/21.
*/
public class RvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<GoosBean.DataBean.DatasBean> list;
public RvAdapter(Context context, List<GoosBean.DataBean.DatasBean> list) {
this.context = context;
this.list = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.child, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
final GoosBean.DataBean.DatasBean datasBean = list.get(position);
final MyViewHolder myViewHolder = (MyViewHolder) holder;
myViewHolder.cbChild.setChecked(datasBean.isChecked());
myViewHolder.tv_tel.setText(datasBean.getType_name());
myViewHolder.tv_content.setText(datasBean.getMsg());
myViewHolder.tv_time.setText(datasBean.getAdd_time());
myViewHolder.tv_price.setText(datasBean.getPrice() + "");
myViewHolder.myView.setNum(datasBean.getNum() + "");
//checkbox点击事件
myViewHolder.cbChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datasBean.setChecked(myViewHolder.cbChild.isChecked());
PriceAndCountEvent compute = compute();
EventBus.getDefault().post(compute);
if (myViewHolder.cbChild.isChecked()) {
if (isAllCbSelected()) {
//改变"全选"状态
changeAllCbState(true);
}
} else {
changeAllCbState(false);
}
notifyDataSetChanged();
}
});
myViewHolder.myView.setAddClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num = myViewHolder.myView.getNum();
num++;
datasBean.setNum(num);
if (myViewHolder.cbChild.isChecked()) {
EventBus.getDefault().post(compute());
}
notifyDataSetChanged();
}
});
myViewHolder.myView.setDelClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num = myViewHolder.myView.getNum();
if (num == 1) {
return;
}
num--;
datasBean.setNum(num);
if (myViewHolder.cbChild.isChecked()) {
EventBus.getDefault().post(compute());
}
notifyDataSetChanged();
}
});
myViewHolder.tv_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);
EventBus.getDefault().post(compute());
notifyDataSetChanged();
}
});
}
/**
* 改变全选的状态
*
* @param flag
*/
private void changeAllCbState(boolean flag) {
MessageEvent messageEvent = new MessageEvent();
messageEvent.setChecked(flag);
EventBus.getDefault().post(messageEvent);
}
private boolean isAllCbSelected() {
for (int i = 0; i < list.size(); i++) {
GoosBean.DataBean.DatasBean datasBean = list.get(i);
if (!datasBean.isChecked()) {
return false;
}
}
return true;
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private final CheckBox cbChild;
private final TextView tv_tel;
private final TextView tv_content;
private final TextView tv_time;
private final TextView tv_price;
private MyView myView;
private final TextView tv_del;
public MyViewHolder(View itemView) {
super(itemView);
cbChild = itemView.findViewById(R.id.cb_child);
tv_tel = itemView.findViewById(R.id.tv_tel);
tv_content = itemView.findViewById(R.id.tv_content);
tv_time = itemView.findViewById(R.id.tv_time);
tv_price = itemView.findViewById(R.id.tv_pri);
tv_del = itemView.findViewById(R.id.tv_del);
myView = itemView.findViewById(R.id.mv);
}
}
private PriceAndCountEvent compute() {
int price = 0;
int count = 0;
for (int i = 0; i < list.size(); i++) {
GoosBean.DataBean.DatasBean datasBean = list.get(i);
if (datasBean.isChecked()) {
price += datasBean.getPrice() * datasBean.getNum();
count += datasBean.getNum();
}
}
PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
priceAndCountEvent.setPrice(price);
priceAndCountEvent.setCount(count);
return priceAndCountEvent;
}
public void allSelect(boolean flag) {
for (int i = 0; i < list.size(); i++) {
GoosBean.DataBean.DatasBean datasBean = list.get(i);
datasBean.setChecked(flag);
}
EventBus.getDefault().post(compute());
notifyDataSetChanged();
}
}
package mvpframework.bwie.com.a1509agoodcart.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 org.greenrobot.eventbus.EventBus;
import java.util.List;
import mvpframework.bwie.com.a1509agoodcart.R;
import mvpframework.bwie.com.a1509agoodcart.bean.GoosBean;
import mvpframework.bwie.com.a1509agoodcart.eventbusevent.MessageEvent;
import mvpframework.bwie.com.a1509agoodcart.eventbusevent.PriceAndCountEvent;
/**
* Created by peng on 2017/11/16.
*/
public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
private List<GoosBean.DataBean> groupList;
private List<List<GoosBean.DataBean.DatasBean>> childList;
private final LayoutInflater inflater;
public MyAdapter(Context context, List<GoosBean.DataBean> groupList, List<List<GoosBean.DataBean.DatasBean>> childList) {
this.context = context;
this.groupList = groupList;
this.childList = childList;
inflater = LayoutInflater.from(context);
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childList.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groupList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childList.get(groupPosition).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) {
View view;
final GroupViewHolder holder;
if (convertView == null) {
holder = new GroupViewHolder();
view = inflater.inflate(R.layout.item_parent_market, null);
holder.cbGroup = view.findViewById(R.id.cb_parent);
holder.tv_number = view.findViewById(R.id.tv_number);
view.setTag(holder);
} else {
view = convertView;
holder = (GroupViewHolder) view.getTag();
}
final GoosBean.DataBean dataBean = groupList.get(groupPosition);
holder.cbGroup.setChecked(dataBean.isChecked());
holder.tv_number.setText(dataBean.getTitle());
//一级checkbox
holder.cbGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataBean.setChecked(holder.cbGroup.isChecked());
changeChildCbState(groupPosition, holder.cbGroup.isChecked());
EventBus.getDefault().post(compute());
changeAllCbState(isAllGroupCbSelected());
notifyDataSetChanged();
}
});
return view;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view;
final ChildViewHolder holder;
if (convertView == null) {
holder = new ChildViewHolder();
view = inflater.inflate(R.layout.item_child_market, null);
holder.cbChild = view.findViewById(R.id.cb_child);
holder.tv_tel = view.findViewById(R.id.tv_tel);
holder.tv_content = view.findViewById(R.id.tv_content);
holder.tv_time = view.findViewById(R.id.tv_time);
holder.tv_price = view.findViewById(R.id.tv_pri);
holder.tv_del = view.findViewById(R.id.tv_del);
holder.iv_add = view.findViewById(R.id.iv_add);
holder.iv_del = view.findViewById(R.id.iv_del);
holder.tv_num = view.findViewById(R.id.tv_num);
view.setTag(holder);
} else {
view = convertView;
holder = (ChildViewHolder) view.getTag();
}
final GoosBean.DataBean.DatasBean datasBean = childList.get(groupPosition).get(childPosition);
holder.cbChild.setChecked(datasBean.isChecked());
holder.tv_tel.setText(datasBean.getType_name());
holder.tv_content.setText(datasBean.getMsg());
holder.tv_time.setText(datasBean.getAdd_time());
holder.tv_price.setText(datasBean.getPrice() + "");
holder.tv_num.setText(datasBean.getNum() + "");
//二级checkbox
holder.cbChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置该条目对象里的checked属性值
datasBean.setChecked(holder.cbChild.isChecked());
PriceAndCountEvent priceAndCountEvent = compute();
EventBus.getDefault().post(priceAndCountEvent);
if (holder.cbChild.isChecked()) {
//当前checkbox是选中状态
if (isAllChildCbSelected(groupPosition)) {
changGroupCbState(groupPosition, true);
changeAllCbState(isAllGroupCbSelected());
}
} else {
changGroupCbState(groupPosition, false);
changeAllCbState(isAllGroupCbSelected());
}
notifyDataSetChanged();
}
});
//加号
holder.iv_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num = datasBean.getNum();
holder.tv_num.setText(++num + "");
datasBean.setNum(num);
if (holder.cbChild.isChecked()) {
PriceAndCountEvent priceAndCountEvent = compute();
EventBus.getDefault().post(priceAndCountEvent);
}
}
});
//减号
holder.iv_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num = datasBean.getNum();
if (num == 1) {
return;
}
holder.tv_num.setText(--num + "");
datasBean.setNum(num);
if (holder.cbChild.isChecked()) {
PriceAndCountEvent priceAndCountEvent = compute();
EventBus.getDefault().post(priceAndCountEvent);
}
}
});
//删除
holder.tv_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<GoosBean.DataBean.DatasBean> datasBeen = childList.get(groupPosition);
GoosBean.DataBean.DatasBean remove = datasBeen.remove(childPosition);
if (datasBeen.size() == 0) {
childList.remove(groupPosition);
groupList.remove(groupPosition);
}
EventBus.getDefault().post(compute());
notifyDataSetChanged();
}
});
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
class GroupViewHolder {
CheckBox cbGroup;
TextView tv_number;
}
class ChildViewHolder {
CheckBox cbChild;
TextView tv_tel;
TextView tv_content;
TextView tv_time;
TextView tv_price;
TextView tv_del;
ImageView iv_del;
ImageView iv_add;
TextView tv_num;
}
/**
* 改变全选的状态
*
* @param flag
*/
private void changeAllCbState(boolean flag) {
MessageEvent messageEvent = new MessageEvent();
messageEvent.setChecked(flag);
EventBus.getDefault().post(messageEvent);
}
/**
* 改变一级列表checkbox状态
*
* @param groupPosition
*/
private void changGroupCbState(int groupPosition, boolean flag) {
GoosBean.DataBean dataBean = groupList.get(groupPosition);
dataBean.setChecked(flag);
}
/**
* 改变二级列表checkbox状态
*
* @param groupPosition
* @param flag
*/
private void changeChildCbState(int groupPosition, boolean flag) {
List<GoosBean.DataBean.DatasBean> datasBeen = childList.get(groupPosition);
for (int i = 0; i < datasBeen.size(); i++) {
GoosBean.DataBean.DatasBean datasBean = datasBeen.get(i);
datasBean.setChecked(flag);
}
}
/**
* 判断一级列表是否全部选中
*
* @return
*/
private boolean isAllGroupCbSelected() {
for (int i = 0; i < groupList.size(); i++) {
GoosBean.DataBean dataBean = groupList.get(i);
if (!dataBean.isChecked()) {
return false;
}
}
return true;
}
/**
* 判断二级列表是否全部选中
*
* @param groupPosition
* @return
*/
private boolean isAllChildCbSelected(int groupPosition) {
List<GoosBean.DataBean.DatasBean> datasBeen = childList.get(groupPosition);
for (int i = 0; i < datasBeen.size(); i++) {
GoosBean.DataBean.DatasBean datasBean = datasBeen.get(i);
if (!datasBean.isChecked()) {
return false;
}
}
return true;
}
/**
* 计算列表中,选中的钱和数量
*/
private PriceAndCountEvent compute() {
int count = 0;
int price = 0;
for (int i = 0; i < childList.size(); i++) {
List<GoosBean.DataBean.DatasBean> datasBeen = childList.get(i);
for (int j = 0; j < datasBeen.size(); j++) {
GoosBean.DataBean.DatasBean datasBean = datasBeen.get(j);
if (datasBean.isChecked()) {
price += datasBean.getNum() * datasBean.getPrice();
count += datasBean.getNum();
}
}
}
PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
priceAndCountEvent.setCount(count);
priceAndCountEvent.setPrice(price);
return priceAndCountEvent;
}
/**
* 设置全选、反选
*
* @param flag
*/
public void changeAllListCbState(boolean flag) {
for (int i = 0; i < groupList.size(); i++) {
changGroupCbState(i, flag);
changeChildCbState(i, flag);
}
EventBus.getDefault().post(compute());
notifyDataSetChanged();
}
}
//eventbusevent包
package mvpframework.bwie.com.a1509agoodcart.eventbusevent;
/**
* Created by peng on 2017/11/17.
*/
public class PriceAndCountEvent {
private int price;
private int count;
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
package mvpframework.bwie.com.a1509agoodcart.eventbusevent;
/**
* Created by peng on 2017/11/17.
*/
public class MessageEvent {
private boolean checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
}
//: MyView
package mvpframework.bwie.com.a1509agoodcart;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by peng on 2017/11/21.
*/
public class MyView extends LinearLayout {
private ImageView iv_add;
private ImageView iv_del;
private TextView tv_num;
public MyView(Context context) {
this(context, null);
}
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(context).inflate(R.layout.myview, this);
iv_add = findViewById(R.id.iv_add);
iv_del = findViewById(R.id.iv_del);
tv_num = findViewById(R.id.tv_num);
}
public void setAddClickListener(OnClickListener onClickListener) {
iv_add.setOnClickListener(onClickListener);
}
public void setDelClickListener(OnClickListener onClickListener) {
iv_del.setOnClickListener(onClickListener);
}
public void setNum(String num) {
tv_num.setText(num);
}
public int getNum() {
String num = tv_num.getText().toString();
return Integer.parseInt(num);
}
}