compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'
compile 'org.greenrobot:eventbus:3.1.1'
购物车界面
<?xml version="1.0" encoding="utf-8"?>
<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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:background="#F7F7F7">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:background="@mipmap/back"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="购物车"/>
<TextView
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:text="编辑"/>
</RelativeLayout>
<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="60dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/checkAll"
android:layout_marginTop="16dp"
android:text="全选"/>
<RelativeLayout
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:paddingTop="6dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="合计:" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#DD6839"
android:text="¥" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不含运费" />
</LinearLayout>
<TextView
android:id="@+id/pay"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center"
android:background="#FC7903"
android:text="去支付"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="90dp"
android:visibility="gone"
android:layout_alignParentRight="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:background="#FC7903"
android:text="分享宝贝"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:background="#FC7903"
android:text="分享宝贝"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:background="#FC7903"
android:text="删除"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
一级
<?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="40dp"
android:orientation="horizontal"
android:padding="10dp">
<CheckBox
android:id="@+id/group_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/group_sj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商家"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/group_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="编辑"/>
</RelativeLayout>
</LinearLayout>
二级
<?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="120dp"
android:orientation="horizontal"
android:padding="10dp">
<CheckBox
android:id="@+id/child_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/child_sdv"
android:layout_width="100dp"
android:layout_height="100dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="14dp"
android:id="@+id/child_show"
android:orientation="vertical">
<TextView
android:id="@+id/child_tv1"
android:layout_width="match_parent"
android:layout_height="18dp"
android:text="tv1"
android:layout_marginTop="8dp"/>
<TextView
android:id="@+id/child_tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="tv2"
android:layout_marginTop="8dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/child_tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#DD6839"
android:text="tv3"
android:layout_marginTop="8dp" />
<TextView
android:id="@+id/child_tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="X"
android:layout_marginTop="8dp" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/child_hide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:visibility="gone">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="horizontal">
<TextView
android:id="@+id/child_jian"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#bac2ec"
android:gravity="center"
android:text="-" />
<TextView
android:id="@+id/child_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="18sp"
android:text="1" />
<TextView
android:id="@+id/child_jia"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#bac2ec"
android:gravity="center"
android:text="+" />
</LinearLayout>
<Button
android:id="@+id/child_del"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="#f70d24"
android:text="删除"
android:textColor="#ffffff" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
回调接口
public interface OnNetListener<T> {
public void onSuccess(T t);
public void onFailure(Exception e);
}
封装网络请求
public class RetrofitHelper {
private static OkHttpClient client;
private static ServerApi serverApi;
static {
getClient();
}
public static OkHttpClient getClient() {
if (client == null){
synchronized (RetrofitHelper.class){
if (client == null){
client = new OkHttpClient();
}
}
}
return client;
}
public static ServerApi getServerApi() {
if (serverApi == null){
synchronized (ServerApi.class){
if (serverApi == null){
serverApi = onCreate(ServerApi.class,Api.HOST);
}
}
}
return serverApi;
}
public static <T> T onCreate(Class<T> tClass, String url){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(tClass);
}
}
EventBus--全选、反选框
public class MessageEvent {
private boolean checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
}
EventBus--价钱、数量
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;
}
}
购物车界面逻辑
public class MainActivity extends AppCompatActivity implements IMainView {
private SelectPresenter selectPresenter;
/**
* 编辑
*/
private TextView edit;
private ExpandableListView elv;
private CheckBox checkAll;
/**
* ¥
*/
private TextView price;
/**
* 去支付
*/
private TextView pay;
private ElvAdapter adapter;
private RelativeLayout show;
private RelativeLayout gone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
selectPresenter = new SelectPresenter(this);
selectPresenter.setSelect();
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override
public void showSelect(List<SelectBean.DataBean> data, List<List<SelectBean.DataBean.ListBean>> child) {
adapter = new ElvAdapter(MainActivity.this, data, child);
elv.setAdapter(adapter);
elv.setGroupIndicator(null);
//默认二级列表展开
for (int i = 0; i < data.size(); i++) {
elv.expandGroup(i);
}
}
private void initView() {
edit = findViewById(R.id.edit);
elv = findViewById(R.id.elv);
checkAll = findViewById(R.id.checkAll);
price = findViewById(R.id.price);
pay = findViewById(R.id.pay);
show = findViewById(R.id.show);
gone = findViewById(R.id.gone);
checkAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter.changeAllListCbState(checkAll.isChecked());
}
});
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String edi = edit.getText().toString().trim();
if (edi.equals("编辑")){
edit.setText("完成");
show.setVisibility(View.GONE);
gone.setVisibility(View.VISIBLE);
return;
}
if (edi.equals("完成")){
edit.setText("编辑");
show.setVisibility(View.VISIBLE);
gone.setVisibility(View.GONE);
}
}
});
}
@Subscribe
public void onMessageEvent(MessageEvent event) {
checkAll.setChecked(event.isChecked());
}
@Subscribe
public void onMessageEvent(PriceAndCountEvent event) {
pay.setText("结算(" + event.getCount() + ")");
price.setText("¥" + event.getPrice());
}
}
适配器
public class ElvAdapter extends BaseExpandableListAdapter {
private Context context;
private List<SelectBean.DataBean> group;
private List<List<SelectBean.DataBean.ListBean>> child;
private AlertDialog show;
private boolean flag;
public ElvAdapter(Context context, List<SelectBean.DataBean> group, List<List<SelectBean.DataBean.ListBean>> child) {
this.context = context;
this.group = group;
this.child = child;
}
@Override
public int getGroupCount() {
if (group == null){
return 0;
}
return group.size();
}
@Override
public int getChildrenCount(int i) {
if (child == null){
return 0;
}
return child.get(i).size();
}
@Override
public Object getGroup(int i) {
return group.get(i);
}
@Override
public Object getChild(int i, int i1) {
return child.get(i).get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
final GroupViewHolder holder1;
if (view == null){
holder1 = new GroupViewHolder();
view = View.inflate(context,R.layout.group,null);
holder1.cb = view.findViewById(R.id.group_cb);
holder1.tv = view.findViewById(R.id.group_sj);
holder1.edit = view.findViewById(R.id.group_edit);
view.setTag(holder1);
} else {
holder1 = (GroupViewHolder) view.getTag();
}
final SelectBean.DataBean dataBean = group.get(i);
holder1.cb.setChecked(dataBean.isChecked());
holder1.tv.setText(dataBean.getSellerName());
//给一级CheckBox设置点击事件
holder1.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataBean.setChecked(holder1.cb.isChecked());
changeChildCbState(i,holder1.cb.isChecked());
EventBus.getDefault().post(computer());
changeAllCbState(isAllGroupCbSelected());
notifyDataSetChanged();
}
});
holder1.edit.setText(dataBean.getEdit());
notifyDataSetChanged();
holder1.edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<SelectBean.DataBean.ListBean> list = dataBean.getList();
if (flag == false){
flag = true;
for (int j = 0; j < list.size(); j++) {
list.get(j).setShow(false);
}
notifyDataSetChanged();
} else {
flag = false;
for (int j = 0; j < list.size(); j++) {
list.get(j).setShow(true);
}
notifyDataSetChanged();
}
notifyDataSetChanged();
}
});
return view;
}
@Override
public View getChildView(final int i, int i1, boolean b, View view, ViewGroup viewGroup) {
final ChildViewHolder holder;
if (view == null){
holder = new ChildViewHolder();
view = View.inflate(context,R.layout.child,null);
holder.cb = view.findViewById(R.id.child_cb);
holder.sdv = view.findViewById(R.id.child_sdv);
holder.show = view.findViewById(R.id.child_show);
holder.tv1 = view.findViewById(R.id.child_tv1);
holder.tv2 = view.findViewById(R.id.child_tv2);
holder.tv3 = view.findViewById(R.id.child_tv3);
holder.tv4 = view.findViewById(R.id.child_tv4);
holder.hide = view.findViewById(R.id.child_hide);
holder.jian = view.findViewById(R.id.child_jian);
holder.num = view.findViewById(R.id.child_num);
holder.jia = view.findViewById(R.id.child_jia);
holder.del = view.findViewById(R.id.child_del);
view.setTag(holder);
} else {
holder = (ChildViewHolder) view.getTag();
}
final SelectBean.DataBean.ListBean list = child.get(i).get(i1);
String[] img = list.getImages().split("\\|");
holder.cb.setChecked(list.isCheck());
holder.sdv.setImageURI(img[0]);
final int pid = list.getPid();
holder.sdv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, DeliteActivity.class);
intent.putExtra("pid",pid+"");
intent.putExtra("img",list.getImages()+"");
intent.putExtra("title",list.getTitle()+"");
intent.putExtra("price",list.getPrice()+"");
context.startActivity(intent);
}
});
holder.tv1.setText(list.getTitle());
holder.tv2.setText(list.getCreatetime());
holder.tv3.setText("¥"+list.getPrice());
holder.tv4.setText("X"+list.getNum());
holder.num.setText(list.getNum()+"");
//给二级CheckBox设置点击事件
holder.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置该条目对象里面的CheckBox属性值
list.setCheck(holder.cb.isChecked());
EventBus.getDefault().post(computer());
if (holder.cb.isChecked()){
//当前CheckBox是选中状态
if (isAllChildCbSelected(i)){
changeGroupCbState(i,true);
changeAllCbState(isAllGroupCbSelected());
}
} else {
//取消选中
changeGroupCbState(i,false);
changeAllCbState(isAllGroupCbSelected());
}
notifyDataSetChanged();
}
});
if (list.getShow() == false){
holder.hide.setVisibility(View.VISIBLE);
holder.show.setVisibility(View.GONE);
} else {
holder.hide.setVisibility(View.GONE);
holder.show.setVisibility(View.VISIBLE);
}
notifyDataSetChanged();
//数量减
holder.jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = list.getNum();
if (num==1){
return;
}
holder.num.setText(--num+"");
list.setNum(num);
if (holder.cb.isChecked()){
EventBus.getDefault().post(computer());
}
}
});
//数量加
holder.jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = list.getNum();
holder.num.setText(++num+"");
list.setNum(num);
if (holder.cb.isChecked()){
EventBus.getDefault().post(computer());
}
}
});
final String pid01 = pid+"";
final int selected = list.getSelected();
final String sellerid = list.getSellerid()+"";
final String num = list.getNum()+"";
holder.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
class GroupViewHolder{
CheckBox cb;
TextView tv;
TextView edit;
}
class ChildViewHolder{
LinearLayout show;
CheckBox cb;
SimpleDraweeView sdv;
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
RelativeLayout hide;
TextView jian;
TextView num;
TextView jia;
Button del;
}
//改变全选CheckBox状态
private void changeAllCbState(boolean flag){
MessageEvent messageEvent = new MessageEvent();
messageEvent.setChecked(flag);
EventBus.getDefault().post(messageEvent);
}
//改变二级列表CheckBox状态
private void changeChildCbState(int groupPosition,boolean flag){
List<SelectBean.DataBean.ListBean> datasBeen = child.get(groupPosition);
for (int i = 0; i < datasBeen.size(); i++) {
SelectBean.DataBean.ListBean datasBean = datasBeen.get(i);
datasBean.setCheck(flag);
}
}
//改变一级列表CheckBox状态
private void changeGroupCbState(int groupPosition,boolean flag){
SelectBean.DataBean dataBean = group.get(groupPosition);
dataBean.setChecked(flag);
}
//判断一级列表是否全部选中
public boolean isAllGroupCbSelected(){
for (int i = 0; i < group.size(); i++) {
SelectBean.DataBean dataBean = group.get(i);
if (!dataBean.isChecked()){
return false;
}
}
return true;
}
//判断二级列表是否全部选中
public boolean isAllChildCbSelected(int groupPosition){
List<SelectBean.DataBean.ListBean> datasBeen = child.get(groupPosition);
for (int i = 0; i < datasBeen.size(); i++) {
SelectBean.DataBean.ListBean datasBean = datasBeen.get(i);
if (!datasBean.isCheck()){
return false;
}
}
return true;
}
//计算列表中选中的钱和数量
private PriceAndCountEvent computer(){
int count = 0;
int price = 0;
for (int i = 0; i < child.size(); i++) {
List<SelectBean.DataBean.ListBean> datasBeen = child.get(i);
for (int j = 0; j < datasBeen.size(); j++) {
SelectBean.DataBean.ListBean datasBean = datasBeen.get(j);
if (datasBean.isCheck()){
count+=datasBean.getNum();
Float pric = Float.valueOf(datasBean.getPrice());
price+=datasBean.getNum()*pric;
}
}
}
PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
priceAndCountEvent.setCount(count);
priceAndCountEvent.setPrice(price);
return priceAndCountEvent;
}
//设置全选、反选
public void changeAllListCbState(boolean flag){
for (int i = 0; i < group.size(); i++) {
changeGroupCbState(i,flag);
changeChildCbState(i,flag);
}
EventBus.getDefault().post(computer());
notifyDataSetChanged();
}
}