//依賴
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.facebook.fresco:fresco:1.5.0'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
//權限
<uses-permission android:name="android.permission.INTERNET" />
//購物車的M層
//接口IShopModel
package model;
import java.util.Map;
import presenter.IShopPresenter;
public interface IShopModel {
void getCarInfo(String path, Map<String,String> map, IShopPresenter iShopPresenter);
}
//ShopModel
package model;
import com.google.gson.Gson;
import java.util.Map;
import bean.ShoppCarBean;
import presenter.IShopPresenter;
import utils.OkHttpUtils;
import utils.OnFinishListener;
public class ShopModel implements IShopModel{
private OkHttpUtils okHttpUtils;
@Override
public void getCarInfo(String path, Map<String, String> map, final IShopPresenter iShopPresenter) {
//建立對象
okHttpUtils = OkHttpUtils.getInData();
okHttpUtils.doGet(path, map, new OnFinishListener() {
@Override
public void onFailed(String msg) {
iShopPresenter.onFailed(msg);
}
@Override
public void onSuccess(Object obj) {
Gson gson=new Gson();
ShoppCarBean shoppCarBean = gson.fromJson(obj.toString(), ShoppCarBean.class);
iShopPresenter.onSuccess(shoppCarBean.getData());
}
});
}
}
//購物車的P層
//IShopPresenter
package presenter;
import java.util.List;
import java.util.Map;
import bean.ShoppCarBean;
public interface IShopPresenter {
void onSuccess(List<ShoppCarBean.DataBean> data);
void onFailed(String msg);
void onDestory();
void getCarInfo(String path, Map<String,String> map);
}
//ShopPresenter
package presenter;
import java.util.List;
import java.util.Map;
import bean.ShoppCarBean;
import model.IShopModel;
import model.ShopModel;
import view.activity.IMainView;
public class ShopPresenter implements IShopPresenter {
private IMainView iMainView;
private IShopModel iShopModel;
public ShopPresenter(IMainView iMainView) {
this.iMainView = iMainView;
iShopModel=new ShopModel();
}
@Override
public void getCarInfo(String path, Map<String, String> map) {
iShopModel.getCarInfo(path,map,this);
}
@Override
public void onSuccess(List<ShoppCarBean.DataBean> data) {
if(iMainView!=null){
iMainView.onSuccess(data);
}
}
@Override
public void onFailed(String msg) {
if(iMainView!=null){
iMainView.onFailed(msg);
}
}
@Override
public void onDestory() {
if(iMainView!=null){
iMainView=null;
}
}
}
//購物車的V層
//IMainView
package view.activity;
import java.util.List;
import bean.ShoppCarBean;
public interface IMainView {
void onSuccess(List<ShoppCarBean.DataBean> data);
void onFailed(String msg);
}
//MainActivity
package view.activity;
import android.content.Intent;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
import bawei.com.gouwuche_demo2.R;
import bean.ShoppCarBean;
import presenter.IShopPresenter;
import presenter.ShopPresenter;
import view.adapter.MyExpandAdapter;
public class MainActivity extends AppCompatActivity implements IMainView, MyExpandAdapter.addJian, MyExpandAdapter.isChecked {
private ExpandableListView expandList;
private IShopPresenter iShopPresenter;
private Map<String, String> map;
private MyExpandAdapter adapter;
private List<ShoppCarBean.DataBean> list;
private CheckBox btnCheckAll;
private TextView tvTotalPrice;
private TextView btnEditor;//編輯
//預設是false
private boolean flag;
private int count = 0;
private double price = 0.00;
private TextView btnAmount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//隐藏标題(actionbar)
getSupportActionBar().hide();
//找控件的方法
initView();
expandList.setAdapter(adapter);
//加減
adapter.setAddJian(this);
//判斷商家和商品是否選中狀态
adapter.setIsChecked(this);
//全選點選監聽事件
btnCheckAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isCheckedAll(((CheckBox) view).isChecked());
setTvTotalPrice();
}
});
}
private void initView() {
expandList = (ExpandableListView) findViewById(R.id.expandList);
btnCheckAll = (CheckBox) findViewById(R.id.btnCheckAll);
tvTotalPrice = (TextView) findViewById(R.id.tvTotalPrice);
btnAmount = (TextView) findViewById(R.id.btnAmount);
btnAmount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
//編輯按鈕
btnEditor = (TextView) findViewById(R.id.btnEditor);
//編輯
btnEditor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!flag) {
flag = true;
btnEditor.setText("完成");
adapter.showDeleteButton(flag);
} else {
flag = false;
btnEditor.setText("編輯");
adapter.showDeleteButton(flag);
}
}
});
//建立P層對象
iShopPresenter = new ShopPresenter(this);
//建立map集合
map = new HashMap<>();
//存放uid
map.put("uid", "4582");
//調用接口中的請求網絡的方法
iShopPresenter.getCarInfo("https://www.zhaoapi.cn/product/getCarts", map);
//建立擴充卡
adapter = new MyExpandAdapter(this);
}
//成功
@Override
public void onSuccess(List<ShoppCarBean.DataBean> data) {
this.list = data;
adapter.setList(list);
//調用展開的方法
zhankai();
}
//展開的方法(預設展開)
private void zhankai() {
for (int i = 0; i < adapter.getGroupCount(); i++) {
expandList.expandGroup(i);
}
}
//失敗
@Override
public void onFailed(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
//商品數量加的方法
@Override
public void add(int groupPosition, int childPosition) {
ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition);
int num = listBean.getNum();
num++;
listBean.setNum(num);
//重新整理擴充卡
adapter.notifyDataSetChanged();
//調用計算總價的方法
setTvTotalPrice();
}
//商品數量減的方法
@Override
public void jian(int groupPosition, int childPosition) {
ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition);
int num = listBean.getNum();
if (num == 1) {
return;
}
num--;
listBean.setNum(num);
//重新整理擴充卡
adapter.notifyDataSetChanged();
//調用計算總價的方法
setTvTotalPrice();
}
//判斷商家是否選中的方法
@Override
public void IsCheckedGroup(int groupPosition, boolean isChecked) {
//商家javabean
ShoppCarBean.DataBean dataBean = list.get(groupPosition);
dataBean.setGroupChoosed(isChecked);
//周遊商家裡面的商品,将其置為選中狀态
for (ShoppCarBean.DataBean.ListBean bean : dataBean.getList()) {
bean.setChildChoosed(isChecked);
}
//底部結算那個checkbox狀态(全選)
if (isCheckAll()) {
btnCheckAll.setChecked(true);
} else {
btnCheckAll.setChecked(false);
}
//重新整理擴充卡
adapter.notifyDataSetChanged();
//調用計算總價的方法
setTvTotalPrice();
}
//商品的checkbox
@Override
public void IsCheckedChild(int groupPosition, int childPosition, boolean isChecked) {
ShoppCarBean.DataBean dataBean = list.get(groupPosition);
ShoppCarBean.DataBean.ListBean listBean = dataBean.getList().get(childPosition);
//你點選商家的商品條目将其選中狀态記錄
listBean.setChildChoosed(isChecked);
//檢測商家裡面的每一個商品是否被選中,如果被選中,傳回boolean
boolean result = isGoodsCheckAll(groupPosition);
if (result) {
dataBean.setGroupChoosed(result);
} else {
dataBean.setGroupChoosed(result);
}
//底部結算那個checkbox狀态(全選)
if (isCheckAll()) {
btnCheckAll.setChecked(true);
} else {
btnCheckAll.setChecked(false);
}
//重新整理擴充卡
adapter.notifyDataSetChanged();
//計算總價
setTvTotalPrice();
}
//檢測某個商家的商品是否都選中,如果都選中的話,商家CheckBox應該是選中狀态
private boolean isGoodsCheckAll(int groupPosition) {
ShoppCarBean.DataBean dataBean = list.get(groupPosition);
//周遊某一個商家的每個商品是否被選中
for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()) {
if (!listBean.isChildChoosed()) {
return false;
}
}
return true;
}
//全選與反選
private void isCheckedAll(boolean isChecked) {
for (ShoppCarBean.DataBean dataBean : list) {
dataBean.setGroupChoosed(isChecked);
for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()) {
listBean.setChildChoosed(isChecked);
}
}
//重新整理擴充卡
adapter.notifyDataSetChanged();
setTvTotalPrice();
}
//購物車商品是否全部選中
private boolean isCheckAll() {
for (ShoppCarBean.DataBean dataBean : list) {
if (!dataBean.isGroupChoosed()) {
return false;
}
}
return true;
}
//計算你所選中的商品總價
private void setTvTotalPrice() {
//初始化值
count = 0;
price = 0.00;
for (ShoppCarBean.DataBean dataBean : list) {
for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()) {
if (listBean.isChildChoosed()) {
count++;
price += listBean.getNum() * listBean.getPrice();
}
}
}
//設定文本資訊、合計、結算的商品數量
tvTotalPrice.setText("合計:¥" + price);
btnAmount.setText("結算(" + count + ")");
}
}
//MyExpandAdapter擴充卡
package view.adapter;
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.List;
import bawei.com.gouwuche_demo2.R;
import bean.ShoppCarBean;
public class MyExpandAdapter extends BaseExpandableListAdapter{
private List<ShoppCarBean.DataBean> list;
private Context context;
private CheckBox ck_group_choosed;
//商品加減的一個接口變量
private addJian addJian;
private isChecked isChecked;
private Button btn_commodity_delete;
//接收是否處于編輯狀态的一個boolean值
private boolean isEditor;
//商家以及商品是否被選中的一個監聽
public void setIsChecked(MyExpandAdapter.isChecked isChecked) {
this.isChecked = isChecked;
}
//設定商品的加減監聽
public void setAddJian(MyExpandAdapter.addJian addJian) {
this.addJian = addJian;
}
public MyExpandAdapter(Context context) {
this.context = context;
}
//設定資料
public void setList(List<ShoppCarBean.DataBean> data){
this.list=data;
}
//是否顯示删除按鈕
public void showDeleteButton(boolean isEditor){
this.isEditor=isEditor;
//重新整理擴充卡
notifyDataSetChanged();
}
@Override
public int getGroupCount() {
return list!=null?list.size():0;
}
@Override
public int getChildrenCount(int i) {
return list!=null&&list.get(i).getList()!=null?list.get(i).getList().size():0;
}
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.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 b, View convertView, ViewGroup parent) {
if(convertView==null){
convertView= LayoutInflater.from(context).inflate(R.layout.layout_group_item,parent,false);
}
//找控件
ck_group_choosed = convertView.findViewById(R.id.ck_group_choosed);
//指派
ck_group_choosed.setText(list.get(groupPosition).getSellerName());
//設定商家checkbox複選框的狀态
ck_group_choosed.setChecked(list.get(groupPosition).isGroupChoosed());
ck_group_choosed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isChecked.IsCheckedGroup(groupPosition,((CheckBox)view).isChecked());
}
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean b, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=LayoutInflater.from(context).inflate(R.layout.layout_child_item,parent,false);
}
//商品選擇
CheckBox ck_child_choose = convertView.findViewById(R.id.ck_child_choose);
//商品圖檔
ImageView iv_show_pic = convertView.findViewById(R.id.iv_show_pic);
//商品主标題
TextView tv_commodity_name = convertView.findViewById(R.id.tv_commodity_name);
//商品副标題
TextView tv_commodity_attr = convertView.findViewById(R.id.tv_commodity_attr);
//商品價格
TextView tv_commodity_price = convertView.findViewById(R.id.tv_commodity_price);
//商品數量
TextView tv_commodity_num = convertView.findViewById(R.id.tv_commodity_num);
//商品減
TextView iv_sub = convertView.findViewById(R.id.iv_sub);
//商品加減中的數量變化
final TextView tv_commodity_show_num = convertView.findViewById(R.id.tv_commodity_show_num);
//商品加
TextView iv_add = convertView.findViewById(R.id.iv_add);
//删除按鈕
btn_commodity_delete = convertView.findViewById(R.id.btn_commodity_delete);
//設定文本資訊
tv_commodity_name.setText(list.get(groupPosition).getList().get(childPosition).getTitle());
tv_commodity_attr.setText(list.get(groupPosition).getList().get(childPosition).getSubhead());
tv_commodity_price.setText("¥"+list.get(groupPosition).getList().get(childPosition).getPrice());
tv_commodity_num.setText("x"+list.get(groupPosition).getList().get(childPosition).getNum());
tv_commodity_show_num.setText(list.get(groupPosition).getList().get(childPosition).getNum()+"");
//處理商品的選中狀态
ck_child_choose.setChecked(list.get(groupPosition).getList().get(childPosition).isChildChoosed());
//商品複選框是否被選中
ck_child_choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isChecked.IsCheckedChild(groupPosition,childPosition,((CheckBox)view).isChecked());
}
});
//分割圖檔位址
String images = list.get(groupPosition).getList().get(childPosition).getImages();
String[] urls = images.split("\\|");
//加載商品圖檔
Glide.with(context)
.load(urls[0])
.crossFade()
.into(iv_show_pic);
//商品加
iv_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(context,"加",Toast.LENGTH_SHORT).show();
addJian.add(groupPosition,childPosition);
}
});
//減
iv_sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addJian.jian(groupPosition,childPosition);
}
});
//控制删除按鈕的隐藏與顯示
if(isEditor){
btn_commodity_delete.setVisibility(View.VISIBLE);
btn_commodity_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
list.remove(groupPosition);
notifyDataSetChanged();
}
});
}else{
btn_commodity_delete.setVisibility(View.GONE);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
//加減
public interface addJian{
//商品添加操作
void add(int groupPosition, int childPosition);
//商品減少操作
void jian(int groupPosition, int childPosition);
}
public interface isChecked{
//商家的條目的複選框監聽
void IsCheckedGroup(int groupPosition,boolean isChecked);
//商品的條目的複選框監聽
void IsCheckedChild(int groupPosition,int childPosition,boolean isChecked);
}
}
//購物車資料
package bean;
import java.util.List;
public class ShoppCarBean {
/**
* msg : 請求成功
* code : 0
* data : [{"list":[{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清雙攝,就是清晰!2000+1600萬高清攝像頭,6GB大記憶體+高通骁龍835處理器,性能怪獸!","title":"一加手機5 (A5000) 6GB+64GB 月岩灰 全網通 雙卡雙待 移動聯通電信4G手機"},{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":2,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G顯存】全高清窄邊框 8G記憶體 256固态硬碟 支援指紋識别 預裝WIN10系統","title":"小米(MI)Air 13.3英寸全金屬輕薄筆記本(i5-7200U 8G 256G PCle SSD MX150 2G獨顯 FHD 指紋識别 Win10)銀\r\n"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":6666,"createtime":"2017-10-10T16:01:31","detailUrl":"https://item.m.jd.com/product/5089273.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8284/363/1326459580/71585/6d3e8013/59b857f2N6ca75622.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9346/182/1406837243/282106/68af5b54/59b8480aNe8af7f5c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8434/54/1359766007/56140/579509d9/59b85801Nfea207db.jpg!q70.jpg","num":1,"pid":46,"price":234,"pscid":39,"selected":0,"sellerid":2,"subhead":"【iPhone新品上市】新一代iPhone,讓智能看起來更不一樣","title":"Apple iPhone 8 Plus (A1864) 64GB 金色 移動聯通電信4G手機"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","num":1,"pid":58,"price":6399,"pscid":40,"selected":0,"sellerid":2,"subhead":"更新4G大顯存!Nvme協定Pcie SSD,速度快人一步】GTX1050Ti就選拯救者!專業遊戲鍵盤&新模具全新設計!","title":"聯想(Lenovo)拯救者R720 15.6英寸遊戲筆記本電腦(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS 黑)"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":21,"price":699,"pscid":1,"selected":0,"sellerid":14,"subhead":"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下","title":"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g"}],"sellerName":"商家14","sellerid":"14"}]
*/
private String msg;
private String code;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* list : [{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清雙攝,就是清晰!2000+1600萬高清攝像頭,6GB大記憶體+高通骁龍835處理器,性能怪獸!","title":"一加手機5 (A5000) 6GB+64GB 月岩灰 全網通 雙卡雙待 移動聯通電信4G手機"},{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":2,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G顯存】全高清窄邊框 8G記憶體 256固态硬碟 支援指紋識别 預裝WIN10系統","title":"小米(MI)Air 13.3英寸全金屬輕薄筆記本(i5-7200U 8G 256G PCle SSD MX150 2G獨顯 FHD 指紋識别 Win10)銀\r\n"}]
* sellerName : 商家1
* sellerid : 1
*/
private String sellerName;
private String sellerid;
private List<ListBean> list;
//商家是否被選中 組條目是否被選中
private boolean isGroupChoosed;
public boolean isGroupChoosed() {
return isGroupChoosed;
}
public void setGroupChoosed(boolean groupChoosed) {
isGroupChoosed = groupChoosed;
}
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public String getSellerid() {
return sellerid;
}
public void setSellerid(String sellerid) {
this.sellerid = sellerid;
}
public List<ListBean> getList() {
return list;
}
public void setList(List<ListBean> list) {
this.list = list;
}
public static class ListBean {
/**
* bargainPrice : 99
* createtime : 2017-10-14T21:38:26
* detailUrl : https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends
* images : https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg
* num : 1
* pid : 45
* price : 2999
* pscid : 39
* selected : 0
* sellerid : 1
* subhead : 高清雙攝,就是清晰!2000+1600萬高清攝像頭,6GB大記憶體+高通骁龍835處理器,性能怪獸!
* title : 一加手機5 (A5000) 6GB+64GB 月岩灰 全網通 雙卡雙待 移動聯通電信4G手機
*/
//private int bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private int pid;
private int price;
private int pscid;
private int selected;
private int sellerid;
private String subhead;
private String title;
//子條目商品是否被選中的一個字段狀态
private boolean isChildChoosed;
public boolean isChildChoosed() {
return isChildChoosed;
}
public void setChildChoosed(boolean childChoosed) {
isChildChoosed = childChoosed;
}
/*public int getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(int bargainPrice) {
this.bargainPrice = bargainPrice;
}*/
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
}
//訂單M層(建立,展示)
(IAddModel,AddModel,IOrderModel,OrderModel)
package model;
import net.OnNetListener;
import java.util.Map;
import bean.AddCarBean;
public interface IAddModel {
public void doCar(String url, Map<String, String> map, OnNetListener<AddCarBean> onNetListener);
}
//AddModel
package model;
import android.os.Handler;
import android.os.Looper;
import com.google.gson.Gson;
import net.HttpUtils;
import net.OnNetListener;
import java.io.IOException;
import java.util.Map;
import bean.AddCarBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class AddModel implements IAddModel {
private Handler handler = new Handler(Looper.getMainLooper());
@Override
public void doCar(String url, Map<String, String> map, final OnNetListener<AddCarBean> onNetListener) {
HttpUtils.getHttpUtils().doPost(url, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
onNetListener.OnFailed(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
final AddCarBean addCarBean = new Gson().fromJson(string, AddCarBean.class);
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.OnSuccess(addCarBean);
}
});
}
});
}
}
//IOrderModel
package model;
import net.OnNetListener;
import bean.OrderBean;
public interface IOrderModel {
public void getOrder(String status, String page, OnNetListener<OrderBean> onNetListener);
}
//OrderModel
package model;
import android.os.Handler;
import android.os.Looper;
import com.google.gson.Gson;
import net.Api;
import net.HttpUtils;
import net.OnNetListener;
import java.io.IOException;
import java.util.HashMap;
import bean.OrderBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class OrderModel implements IOrderModel {
private Handler handler = new Handler(Looper.getMainLooper());
@Override
public void getOrder(String status, String page, final OnNetListener<OrderBean> onNetListener) {
HashMap<String, String> map = new HashMap<>();
map.put("uid","71");
map.put("status",status);
map.put("page",page);
HttpUtils.getHttpUtils().doPost(Api.getOrders, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
onNetListener.OnFailed(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
final OrderBean orderBean = new Gson().fromJson(string, OrderBean.class);
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.OnSuccess(orderBean);
}
});
}
});
}
}
//訂單P層
(AddPresenter,OrderPresenter)
//AddP
package presenter;
import net.Api;
import net.OnNetListener;
import java.util.HashMap;
import bean.AddCarBean;
import model.AddModel;
import view.activity.IMainActivity;
public class AddPresenter {
private IMainActivity iMainActivity;
private final AddModel addModel;
public AddPresenter(IMainActivity iMainActivity) {
this.iMainActivity = iMainActivity;
addModel = new AddModel();
}
public void addCar(int uid ){
HashMap<String, String> map = new HashMap<>();
map.put("uid","71");
addModel.doCar(Api.createOrder, map, new OnNetListener<AddCarBean>() {
@Override
public void OnSuccess(AddCarBean addCarBean) {
iMainActivity.addCar(addCarBean);
}
@Override
public void OnFailed(Exception e) {
}
});
}
}
//OrderP
package presenter;
import net.OnNetListener;
import bean.OrderBean;
import model.OrderModel;
import view.activity.ISecondActivity;
public class OrderPresenter {
private ISecondActivity iSecondActivity;
private final OrderModel orderModel;
public OrderPresenter(ISecondActivity iSecondActivity) {
this.iSecondActivity = iSecondActivity;
orderModel = new OrderModel();
}
public void getOrder(final boolean isRefresh, String status, String page){
orderModel.getOrder(status, page, new OnNetListener<OrderBean>() {
@Override
public void OnSuccess(OrderBean orderBean) {
iSecondActivity.showData(isRefresh,orderBean.getData());
}
@Override
public void OnFailed(Exception e) {
}
});
}
}
//訂單V層
//IMainActivity
package view.activity;
import bean.AddCarBean;
public interface IMainActivity {
public void show(String str);
public void addCar(AddCarBean addCarBean);
}
//ISecondActivity
package view.activity;
import java.util.List;
import bean.OrderBean;
public interface ISecondActivity {
public void showData(boolean isRefresh, List<OrderBean.DataBean> list);
}
//SecondActivity
package view.activity;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;
import bawei.com.gouwuche_demo2.R;
import bean.OrderBean;
import presenter.OrderPresenter;
import view.adapter.MyAdapter;
public class SecondActivity extends AppCompatActivity implements ISecondActivity, View.OnClickListener{
private ImageView mIvPop;
/**
* 待支付
*/
private RadioButton mRb1;
/**
* 已支付
*/
private RadioButton mRb2;
/**
* 已取消
*/
private RadioButton mRb3;
private RadioGroup mRg;
private XRecyclerView mXrv;
private int page = 1;
private List<OrderBean.DataBean> list = new ArrayList<>();
private MyAdapter myAdapter;
private OrderPresenter orderPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mIvPop = (ImageView) findViewById(R.id.iv_pop);
mRb1 = (RadioButton) findViewById(R.id.rb1);
mRb2 = (RadioButton) findViewById(R.id.rb2);
mRb3 = (RadioButton) findViewById(R.id.rb3);
mRg = (RadioGroup) findViewById(R.id.rg);
mXrv = (XRecyclerView) findViewById(R.id.xrv);
initView();
mXrv.setLayoutManager(new LinearLayoutManager(this));
myAdapter = new MyAdapter(this, list);
mXrv.setAdapter(myAdapter);
orderPresenter = new OrderPresenter(this);
orderPresenter.getOrder(true, "1", page + "");
mXrv.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
page = 1;
orderPresenter.getOrder(true, "1", page + "");
}
@Override
public void onLoadMore() {
//加載更多
page++;
orderPresenter.getOrder(false, "1", page + "");
}
});
}
private void initView() {
mIvPop.setOnClickListener(this);
mRb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
orderPresenter.getOrder(true, "0", "1");
}
});
mRb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
orderPresenter.getOrder(true, "1", "1");
}
});
mRb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
orderPresenter.getOrder(true, "2", "1");
}
});
}
@Override
public void showData(boolean isRefresh, List<OrderBean.DataBean> newlist) {
if (isRefresh) {
list.clear();
list.addAll(newlist);
mXrv.refreshComplete();
} else {
list.addAll(newlist);
mXrv.setLoadingMoreEnabled(false);
}
myAdapter.notifyDataSetChanged();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.iv_pop:
View view = View.inflate(SecondActivity.this, R.layout.pop_item, null);
TextView tv1 = view.findViewById(R.id.tv1);
TextView tv2 = view.findViewById(R.id.tv2);
TextView tv3 = view.findViewById(R.id.tv3);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final PopupWindow popupWindow = new PopupWindow(view, layoutParams.width, layoutParams.height);
//popupWindow.showAsDropDown(mIvPop, 0, 30);
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(mIvPop);
//popupWindow.setFocusable(true);
tv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(SecondActivity.this, "待支付", Toast.LENGTH_SHORT).show();
orderPresenter.getOrder(true, "0", "1");
popupWindow.dismiss();
}
});
tv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(SecondActivity.this, "已支付", Toast.LENGTH_SHORT).show();
orderPresenter.getOrder(true, "1", "1");
popupWindow.dismiss();
}
});
tv3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(SecondActivity.this, "已取消", Toast.LENGTH_SHORT).show();
orderPresenter.getOrder(true, "2", "1");
popupWindow.dismiss();
}
});
break;
}
}
}
//MyAdapter擴充卡(訂單)
package view.adapter;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import bawei.com.gouwuche_demo2.R;
import bean.OrderBean;
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<OrderBean.DataBean> list;
public MyAdapter(Context context, List<OrderBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final OrderBean.DataBean dataBean = list.get(position);
final MyViewHolder myViewHolder = (MyViewHolder) holder;
myViewHolder.tv_title.setText(dataBean.getTitle());
final int status = dataBean.getStatus();
if (status == 0) {
myViewHolder.tv_status.setText("待支付");
myViewHolder.tv.setText("取消訂單");
myViewHolder.tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("提示");
builder.setMessage("确定取消訂單嗎?");
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context,"訂單已取消",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
} else if (status == 1) {
myViewHolder.tv_status.setText("已支付");
myViewHolder.tv.setText("檢視訂單");
} else {
myViewHolder.tv_status.setText("已取消");
myViewHolder.tv.setText("檢視訂單");
}
myViewHolder.tv_price.setText("價格:" + dataBean.getPrice());
myViewHolder.tv_time.setText("建立時間:" + dataBean.getCreatetime());
myViewHolder.tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (status == 1){
Toast.makeText(context,"檢視訂單",Toast.LENGTH_SHORT).show();
}else if (status == 0){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("提示");
builder.setMessage("确定取消訂單嗎?");
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context,"訂單已取消",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView tv_title;
private final TextView tv_status;
private final TextView tv_price;
private final TextView tv_time;
private final TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
tv_title = itemView.findViewById(R.id.tv_title);
tv_status = itemView.findViewById(R.id.tv_status);
tv_price = itemView.findViewById(R.id.tv_price);
tv_time = itemView.findViewById(R.id.tv_time);
tv = itemView.findViewById(R.id.tv);
}
}
}
//建立訂單bean
package bean;
public class AddCarBean {
/**
* msg : 訂單建立成功
* code : 0
*/
private String msg;
private String code;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
//展示請求的訂單資料bean
package bean;
import java.util.List;
public class OrderBean {
/**
* msg : 請求成功
* code : 0
* data : [{"createtime":"2017-10-19T20:28:43","orderid":20,"price":100,"status":0,"title":"訂單标題測試3","uid":71},{"createtime":"2017-10-19T20:44:40","orderid":31,"price":11800,"status":0,"title":"訂單标題測試14","uid":71},{"createtime":"2017-10-19T20:44:51","orderid":32,"price":11800,"status":0,"title":"訂單标題測試15","uid":71},{"createtime":"2017-10-20T08:02:07","orderid":43,"price":11800,"status":0,"title":"訂單标題測試","uid":71},{"createtime":"2017-10-20T08:02:16","orderid":44,"price":11800,"status":0,"title":"訂單标題測試","uid":71},{"createtime":"2017-10-22T15:14:39","orderid":890,"price":11800,"status":0,"title":"","uid":71},{"createtime":"2017-11-09T09:17:20","orderid":1446,"price":99.99,"status":0,"title":"訂單标題測試","uid":71},{"createtime":"2017-11-09T09:20:58","orderid":1447,"price":567,"status":0,"title":"訂單标題測試","uid":71},{"createtime":"2017-11-09T09:20:58","orderid":1448,"price":256.99,"status":0,"title":"訂單标題測試","uid":71},{"createtime":"2017-11-09T09:20:58","orderid":1449,"price":399,"status":0,"title":"訂單标題測試","uid":71}]
* page : 1
*/
private String msg;
private String code;
private String page;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* createtime : 2017-10-19T20:28:43
* orderid : 20
* price : 100.0
* status : 0
* title : 訂單标題測試3
* uid : 71
*/
private String createtime;
private int orderid;
private double price;
private int status;
private String title;
private int uid;
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public int getOrderid() {
return orderid;
}
public void setOrderid(int orderid) {
this.orderid = orderid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
}
}
//封裝的包(utils包下的)
//攔截器
package utils;
import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
//攔截器
public class CommonParamsInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String method = request.method();
if("GET".equals(method)){
String oldUrl = request.url().toString();
String newUrl=oldUrl+"&source=android";
request=new Request.Builder()
.url(newUrl)
.build();
}else if("POST".equals(method)){
String path = request.url().toString();
FormBody oldBody = (FormBody) request.body();
FormBody.Builder newBody=new FormBody.Builder();
for(int i=0;i<oldBody.size();i++){
String name = oldBody.name(i);
String value = oldBody.value(i);
newBody.add(name,value);
}
newBody.add("source","android");
request=new Request.Builder()
.post(newBody.build())
.url(path)
.build();
}
return chain.proceed(request);
}
}
//封裝
package utils;
import android.os.Handler;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
//網絡請求的封裝
public class OkHttpUtils {
private static OkHttpUtils OK_HTTP_UTILS=null;
private Handler handler=new Handler();
private OkHttpUtils() {
}
public static OkHttpUtils getInData(){
if(OK_HTTP_UTILS==null){
synchronized (OkHttpUtils.class){
if(OK_HTTP_UTILS==null){
OK_HTTP_UTILS=new OkHttpUtils();
}
}
}
return OK_HTTP_UTILS;
}
public void doGet(String path, Map<String,String> map, final OnFinishListener onFinishListener){
StringBuffer sb=new StringBuffer();
sb.append(path);
if(sb.indexOf("?")!=-1){
if(sb.indexOf("?")!=sb.length()-1){
sb.append("&");
}
}else{
sb.append("?");
}
for(Map.Entry<String,String> m:map.entrySet()){
sb.append(m.getKey())
.append("=")
.append(m.getValue())
.append("&");
}
if(sb.lastIndexOf("&")!=-1){
sb.deleteCharAt(sb.length()-1);
}
OkHttpClient okHttpClient=new OkHttpClient.Builder()
.addInterceptor(new CommonParamsInterceptor())//應用攔截器
.build();
Request request=new Request.Builder()
.get()
.url(sb.toString())
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onFinishListener.onFailed(e.getMessage());
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if(response.isSuccessful()){
final String result = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
onFinishListener.onSuccess(result);
}
});
}
}
});
}
public void doPost(String path, Map<String,String> map, final OnFinishListener onFinishListener){
OkHttpClient okHttpClient=new OkHttpClient.Builder()
.addInterceptor(new CommonParamsInterceptor())
.build();
FormBody.Builder body=new FormBody.Builder();
for(String key:map.keySet()){
body.add(key,map.get(key));
}
Request request=new Request.Builder()
.post(body.build())
.url(path)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onFinishListener.onFailed(e.getMessage());
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
final String result = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
onFinishListener.onSuccess(result);
}
});
}
}
});
}
}
//接口
package utils;
/**
* 定義一個接口
*/
public interface OnFinishListener {
void onFailed(String msg);
void onSuccess(Object obj);
}
//net包下的封裝(用訂單中)
package net;
public interface Api {
//擷取購物車清單接口
public static final String getCarts = "https://www.zhaoapi.cn/product/getCarts";
//建立訂單接口
public static final String createOrder = "https://www.zhaoapi.cn/product/createOrder";
//擷取訂單清單接口
public static final String getOrders = "https://www.zhaoapi.cn/product/getOrders";
//修改訂單狀态
public static final String updateOrder = "https://www.zhaoapi.cn/product/updateOrder";
}
//封裝
package net;
import java.util.Map;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class HttpUtils {
private static HttpUtils httpUtils;
private final OkHttpClient client;
public HttpUtils() {
client = new OkHttpClient.Builder()
.addInterceptor(new MyInterceptor())
.build();
}
public static HttpUtils getHttpUtils(){
if (httpUtils == null){
synchronized (HttpUtils.class){
if (httpUtils ==null){
httpUtils = new HttpUtils();
}
}
}
return httpUtils;
}
public void doPost(String url, Map<String,String> map, Callback callback){
if (map == null){
throw new RuntimeException("參數為空");
}
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry<String,String> entry: map.entrySet()) {
builder.add(entry.getKey(),entry.getValue());
}
FormBody formBody = builder.build();
Request request = new Request.Builder().url(url).post(formBody).build();
client.newCall(request).enqueue(callback);
}
public void doGet(String url,Callback callback){
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(callback);
}
}
//MyApp
package net;
import android.app.Application;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(configuration);
Fresco.initialize(this);
}
}
//攔截器 package net;
import java.io.IOException;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
public class MyInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
HttpUrl url=original.url().newBuilder()
.addQueryParameter("source","android")
.build();
//添加請求頭
Request request = original.newBuilder()
.url(url)
.build();
return chain.proceed(request);
}
}
//接口
package net;
public interface OnNetListener<T> {
void OnSuccess(T t);
void OnFailed(Exception e);
}
//購物車、訂單布局
activity_main.xml
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/layout_title" />
<ExpandableListView
android:id="@+id/expandList"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="2dp"
android:background="#000000"/>
<include layout="@layout/layout_bottom" />
</LinearLayout>
layout_title.xml
<?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/holo_red_light"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="傳回"
android:textSize="25sp"
android:padding="10dp"
android:textColor="@android:color/white"
android:id="@+id/btnBack"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:padding="10dp"
android:textColor="@android:color/white"
android:layout_weight="1"
android:gravity="center"
android:text="購物車"/>
<TextView
android:id="@+id/btnEditor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="編輯"
android:textColor="@android:color/white"
android:textSize="25sp"
android:padding="10dp"/>
</LinearLayout>
layout_bottom.xml
<?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:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全選"
android:textSize="25sp"
android:padding="10dp"
android:id="@+id/btnCheckAll"/>
<TextView
android:id="@+id/tvTotalPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="10dp"
android:textColor="@android:color/black"
android:layout_weight="1"
android:gravity="center"
android:text="合計:¥0.00"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="結算(0)"
android:textColor="@android:color/white"
android:textSize="25sp"
android:padding="10dp"
android:background="@android:color/holo_red_light"
android:id="@+id/btnAmount"/>
</LinearLayout>
layout_group_item.xml
<?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:orientation="vertical">
<CheckBox
android:id="@+id/ck_group_choosed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="商家1"
android:gravity="center_vertical"
android:textSize="25sp"
android:focusable="false"
android:padding="10dp"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/ck_child_choose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleX="0.6"
android:scaleY="0.6" />
<ImageView
android:id="@+id/iv_show_pic"
android:layout_width="70dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:src="@mipmap/ic_launcher"
android:layout_toRightOf="@id/ck_child_choose" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="@id/iv_show_pic"
android:orientation="vertical">
<TextView
android:id="@+id/tv_commodity_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="酒紅色純紅色純羊毛西服套裝"
android:textColor="@android:color/black"
android:textSize="12sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_commodity_attr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="屬性:粉藍色"
android:textSize="12sp"
android:textColor="@color/colorPrimary" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_commodity_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥390"
android:textColor="@android:color/holo_red_dark"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_commodity_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="x1"
android:textColor="@android:color/darker_gray" />
<LinearLayout
android:id="@+id/rl_edit"
android:layout_width="120dp"
android:background="@android:color/holo_orange_light"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
>
<TextView
android:id="@+id/iv_sub"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="@android:color/black"
android:background="@android:color/white"
android:layout_margin="1dp"
android:layout_height="match_parent"
android:text=" - " />
<TextView
android:id="@+id/tv_commodity_show_num"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:background="@android:color/white"
android:layout_margin="1dp"
android:layout_height="match_parent"
android:text="1"
/>
<TextView
android:id="@+id/iv_add"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_margin="1dp"
android:background="@android:color/white"
android:layout_height="match_parent"
android:text=" + " />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btn_commodity_delete"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="-"
android:background="@android:color/holo_red_light"
android:textSize="20sp"
android:textColor="@android:color/white"
android:layout_margin="5dp"
android:visibility="gone" />
</RelativeLayout>
//訂單布局
activity_second.xml
<?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:id="@+id/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="view.activity.SecondActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="訂單清單"
android:textSize="25sp" />
<ImageView
android:id="@+id/iv_pop"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@drawable/lv_icon" />
</RelativeLayout>
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="待支付" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="已支付" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="已取消" />
</RadioGroup>
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
pop_item.xml
<?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="#ffffff"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="待支付"></TextView>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="已支付"></TextView>
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="已取消"></TextView>
</LinearLayout>
item.xml
<?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:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/tv_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_centerVertical="true"
android:layout_weight="1"
android:textColor="#ff0000" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>