1、首先導入依賴
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
2、寫入權限
<uses-permission android:name="android.permission.INTERNET" />
3、來看一下包結構
4、接着看一下MainActivity.java
public class MainActivity extends AppCompatActivity implements CarAdapter.TotalPriceLister, DataCall<List<Shop>> {
private TextView mSunPirce;
private CheckBox mCheck_All;
private CarAdapter carAdapter;
private ExpandableListView mGoodsList;
private CartPresenter cartPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到控件
mSunPirce = findViewById(R.id.goods_sum_price);
mCheck_All = findViewById(R.id.check_all);
mGoodsList = findViewById(R.id.list_car);
// 沒有箭頭
mGoodsList.setGroupIndicator(null);
// 讓group不能被點擊
mGoodsList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;
}
});
//點擊全選按鈕
mCheck_All.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
carAdapter.checkAll(isChecked);//全部選中
}
});
// 得到適配器對象
carAdapter = new CarAdapter();
carAdapter.setTotalPriceLister(this);// 回調總金額
// 設置適配器
mGoodsList.setAdapter(carAdapter);
cartPresenter = new CartPresenter(this);// 調用P層
cartPresenter.requestData();// 使用MVP模式
}
@Override
public void totalprice(double totalprice) {
// 設置總金額
mSunPirce.setText(String.valueOf(totalprice));
}
@Override
public void success(List<Shop> data) {
// 添加數據
carAdapter.addAll(data);
// 默認展開 所有數據
int size = data.size();
for (int i = 0; i < size; i++) {
mGoodsList.expandGroup(i);// 展開
}
// 更新數據
carAdapter.notifyDataSetChanged();
}
@Override
public void fail(Result result) {
cartPresenter.unBindCall();// 防治內存泄露
Toast.makeText(this, "" + result.getMsg(), Toast.LENGTH_SHORT).show();
}
}
5、看一下二級列表的適配器CarAdapter.java
public class CarAdapter extends BaseExpandableListAdapter {
private List<Shop> mList = new ArrayList<>();
private TotalPriceLister priceLister;
//實例化自定義接口
public void setTotalPriceLister(TotalPriceLister totalPriceLister) {
this.priceLister = totalPriceLister;
}
// 點擊全選 則調用次方法
public void checkAll(boolean is) {
for (int i = 0; i < mList.size(); i++) {
Shop shop = mList.get(i);
shop.setCheck(is);// 設置
for (int j = 0; j < shop.getList().size(); j++) {
Goods goods = shop.getList().get(j);
goods.setSelected(is?1:0);//設置選中狀態
}
}
notifyDataSetChanged();// 刷新數據
calculatePrice();//計算總價
}
// 自定義接口
public interface TotalPriceLister{
void totalprice(double totalprice);
}
public void addAll(List<Shop> list){
if (list != null) {
mList.addAll(list);// 添加數據
}
}
@Override
public int getGroupCount() {
return mList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mList.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return mList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mList.get(groupPosition).getList().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) {
GroupHolder groupHolder = null;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.group_item, null);
groupHolder = new GroupHolder();
groupHolder.checkBox = convertView.findViewById(R.id.g_checkbox);
convertView.setTag(groupHolder);
} else {
groupHolder = (GroupHolder) convertView.getTag();
}
final Shop shop = mList.get(groupPosition);// 得到此商鋪
groupHolder.checkBox.setText(shop.getSellerName());// 設置商品的名字
groupHolder.checkBox.setChecked(shop.isCheck());// 這是商鋪的選中狀態
groupHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 防治點擊事件與選中發生衝突
CheckBox checkBox= (CheckBox) v;
shop.setCheck(checkBox.isChecked());// 設置商品的選中狀態
List<Goods> goodsList = mList.get(groupPosition).getList();// 得到商品的信息
for (int i = 0; i < goodsList.size(); i++) {
goodsList.get(i).setSelected(checkBox.isChecked() ? 1 : 0);// 設置是否選中
}
// 更新適配器
notifyDataSetChanged();
// 計算價格
calculatePrice();
}
});
return convertView;
}
/**
* 計算總價格
*/
private void calculatePrice() {
double totalprice=0;
for (int i = 0; i < mList.size(); i++) {
Shop shop = mList.get(i);
for (int j = 0; j < shop.getList().size(); j++) {
Goods goods = shop.getList().get(j);
if (goods.getSelected() == 1) {
// 是選中狀態的話則計算進去
totalprice = totalprice +goods.getNum()*goods.getPrice();
}
}
}
// 判斷
if (priceLister != null) {
priceLister.totalprice(totalprice);// 將值利用接口回調傳回Activity進行展示
}
}
class GroupHolder {
CheckBox checkBox;// 這是二級列表的第一級
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder childHolder=null;
if (convertView == null) {
childHolder = new ChildHolder();
convertView = View.inflate(parent.getContext(),R.layout.child_item,null);
childHolder.addSub= convertView.findViewById(R.id.add_sub);
childHolder.check = convertView.findViewById(R.id.child_check);
childHolder.image = convertView.findViewById(R.id.image_cc);
childHolder.price = convertView.findViewById(R.id.text_price);
childHolder.text = convertView.findViewById(R.id.text_ttt);
convertView.setTag(childHolder);
}else{
childHolder= (ChildHolder) convertView.getTag();
}
// 得到單個商品
final Goods goods = mList.get(groupPosition).getList().get(childPosition);
childHolder.text.setText(goods.getTitle());
childHolder.price.setText(goods.getPrice()+"");// 單價
// 點擊選中,計算價格
childHolder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 防治點擊事件與選中發生衝突
CheckBox checkBox = (CheckBox) v;
goods.setSelected(checkBox.isChecked()?1:0);//設置此商品是否選中
// 計算價格
calculatePrice();
}
});
// 設置商品的複選框
if (goods.getSelected()==0) {
childHolder.check.setChecked(false);
}else{
childHolder.check.setChecked(true);
}
//設置圖片
String images = goods.getImages();
String[] split = images.split("\\|");
if (split .length>0) {
Glide.with(parent.getContext()).load(split[0].replace("https","http")).into(childHolder.image);
}
// 設置次商品的數量
childHolder.addSub.setCount(goods.getNum());
childHolder.addSub.setAddSubListener(new AddSubLayout.AddSubListener() {
@Override
public void addSub(int number) {
goods.setNum(number);//設置數量
calculatePrice();// 計算價格
}
});
return convertView;
}
class ChildHolder {
CheckBox check;
TextView text;
TextView price;
ImageView image;
AddSubLayout addSub;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
6、BaseAdapter.java
public abstract class BasePresenter {
DataCall dataCall;
public BasePresenter(DataCall dataCall){
this.dataCall = dataCall;
}
Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
Result result = (Result) msg.obj;
if (result.getCode()==0){
dataCall.success(result.getData());
}else{
dataCall.fail(result);
}
}
};
public void requestData(final Object...args){
new Thread(new Runnable() {
@Override
public void run() {
Message message = mHandler.obtainMessage();
message.obj = getData(args);
mHandler.sendMessage(message);
}
}).start();
}
protected abstract Result getData(Object...args);
public void unBindCall(){
this.dataCall = null;//防治內存泄露
}
}
7、看一下DataCall.java
public interface DataCall<T> {
// 成功
void success(T data);
// 失敗
void fail(Result result);
}
8、Model層
public class CartModel {
private static Result result;
public static Result goodsList() {
// 可以調用okhttp 因接口容易崩潰直接將數據取了出來
// String resultString = HttpUtils.get("http://www.zhaoapi.cn/product/getCarts?uid=71");
String resultString = "{\"msg\":\"請求成功\",\"code\":\"0\",\"data\":[{\"list\":[],\"sellerName\":\"\",\"sellerid\":\"0\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:48:08\",\"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\":11,\"pid\":11,\"price\":8989.0,\"pscid\":1,\"selected\":0,\"sellerid\":4,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家4\",\"sellerid\":\"4\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-03T23:43:53\",\"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\":13,\"price\":465.0,\"pscid\":1,\"selected\":0,\"sellerid\":6,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家6\",\"sellerid\":\"6\"},{\"list\":[{\"bargainPrice\":11800.0,\"createtime\":\"2017-10-14T21:38:26\",\"detailUrl\":\"https:\\/\\/mitem.jd.hk\\/ware\\/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t6130\\/97\\/1370670410\\/180682\\/1109582a\\/593276b1Nd81fe723.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5815\\/178\\/2614671118\\/51656\\/7f52d137\\/593276c7N107b725a.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5878\\/60\\/2557817477\\/30873\\/4502b606\\/593276caN5a7d6357.jpg!q70.jpg\",\"num\":3,\"pid\":63,\"price\":10000.0,\"pscid\":40,\"selected\":0,\"sellerid\":7,\"subhead\":\"購買電腦辦公部分商品滿1元返火車票5元優惠券(返完即止)\",\"title\":\"全球購 新款Apple MacBook Pro 蘋果筆記本電腦 銀色VP2新13英寸Bar i5\\/8G\\/256G\"}],\"sellerName\":\"商家7\",\"sellerid\":\"7\"},{\"list\":[{\"bargainPrice\":399.0,\"createtime\":\"2017-10-03T23:53:28\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5887\\/201\\/859509257\\/69994\\/6bde9bf6\\/59224c24Ne854e14c.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5641\\/233\\/853609022\\/57374\\/5c73d281\\/59224c24N3324d5f4.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5641\\/233\\/853609022\\/57374\\/5c73d281\\/59224c24N3324d5f4.jpg!q70.jpg\",\"num\":1,\"pid\":87,\"price\":888.0,\"pscid\":85,\"selected\":0,\"sellerid\":8,\"subhead\":\"滿2件,總價打6.50折\",\"title\":\"Gap男裝 休閒舒適簡約水洗五袋直筒長褲緊身牛仔褲941825 深灰色 33\\/32(175\\/84A)\"},{\"bargainPrice\":3455.0,\"createtime\":\"2017-10-03T23:53:28\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9106\\/106\\/1785172479\\/537280\\/253bc0ab\\/59bf78a7N057e5ff7.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9106\\/106\\/1785172479\\/537280\\/253bc0ab\\/59bf78a7N057e5ff7.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8461\\/5\\/1492479653\\/68388\\/7255e013\\/59ba5e84N91091843.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8461\\/5\\/1492479653\\/68388\\/7255e013\\/59ba5e84N91091843.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8803\\/356\\/1478945529\\/489755\\/2a163ace\\/59ba5e84N7bb9a666.jpg!q70.jpg\",\"num\":2,\"pid\":52,\"price\":666.0,\"pscid\":39,\"selected\":0,\"sellerid\":8,\"subhead\":\"【現貨新品搶購】全面屏2.0震撼來襲,驍龍835處理器,四曲面陶瓷機\",\"title\":\"小米(MI) 小米MIX2 手機 黑色 全網通 (6GB+64GB)【標配版】\"}],\"sellerName\":\"商家8\",\"sellerid\":\"8\"},{\"list\":[{\"bargainPrice\":11800.0,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/mitem.jd.hk\\/ware\\/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t6130\\/97\\/1370670410\\/180682\\/1109582a\\/593276b1Nd81fe723.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5815\\/178\\/2614671118\\/51656\\/7f52d137\\/593276c7N107b725a.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5878\\/60\\/2557817477\\/30873\\/4502b606\\/593276caN5a7d6357.jpg!q70.jpg\",\"num\":3,\"pid\":65,\"price\":12000.0,\"pscid\":40,\"selected\":0,\"sellerid\":9,\"subhead\":\"購買電腦辦公部分商品滿1元返火車票5元優惠券(返完即止)\",\"title\":\"全球購 新款Apple MacBook Pro 蘋果筆記本電腦 銀色VP2新13英寸Bar i5\\/8G\\/256G\"},{\"bargainPrice\":2999.0,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/2385655.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2068\\/298\\/2448145915\\/157953\\/7be197df\\/56d51a42Nd86f1c8e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2437\\/128\\/1687178395\\/117431\\/bcc190c1\\/56d3fcbaNb2963d21.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2467\\/222\\/2263160610\\/95597\\/927b8a2f\\/56d3eafeNdecebeb6.jpg!q70.jpg\",\"num\":2,\"pid\":53,\"price\":777.0,\"pscid\":39,\"selected\":0,\"sellerid\":9,\"subhead\":\"Super AMOLED三星雙曲面2K 屏,支持無線充電!\",\"title\":\"三星 Galaxy S7 edge(G9350)4GB+32GB 鉑光金 移動聯通電信4G手機 雙卡雙待\"},{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:39:05\",\"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\":16,\"price\":199.0,\"pscid\":1,\"selected\":0,\"sellerid\":9,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家9\",\"sellerid\":\"9\"},{\"list\":[{\"bargainPrice\":11800.0,\"createtime\":\"2017-10-14T21:38:26\",\"detailUrl\":\"https:\\/\\/mitem.jd.hk\\/ware\\/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t6130\\/97\\/1370670410\\/180682\\/1109582a\\/593276b1Nd81fe723.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5815\\/178\\/2614671118\\/51656\\/7f52d137\\/593276c7N107b725a.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5878\\/60\\/2557817477\\/30873\\/4502b606\\/593276caN5a7d6357.jpg!q70.jpg\",\"num\":3,\"pid\":66,\"price\":13000.0,\"pscid\":40,\"selected\":0,\"sellerid\":10,\"subhead\":\"購買電腦辦公部分商品滿1元返火車票5元優惠券(返完即止)\",\"title\":\"全球購 新款Apple MacBook Pro 蘋果筆記本電腦 銀色VP2新13英寸Bar i5\\/8G\\/256G\"},{\"bargainPrice\":159.0,\"createtime\":\"2017-10-14T21:49:15\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8716\\/197\\/1271594444\\/173291\\/2f40bb4f\\/59b743bcN8509428e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8347\\/264\\/1286771527\\/92188\\/5cf5ec04\\/59b7420fN65378e9e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7363\\/165\\/3000956253\\/190883\\/179a372\\/59b743bfNd0c79d93.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7399\\/112\\/2935531768\\/183594\\/b77c7d4a\\/59b7441aNc3d40133.jpg!q70.jpg\",\"num\":5,\"pid\":99,\"price\":2100.0,\"pscid\":112,\"selected\":0,\"sellerid\":10,\"subhead\":\"針織針織閃閃閃亮你的眼\",\"title\":\"維邇旎 2017秋冬新款長袖針織連衣裙韓版氣質中長款名媛包臀A字裙 zx179709 黑色 XL\"},{\"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\":2,\"pid\":17,\"price\":299.0,\"pscid\":1,\"selected\":0,\"sellerid\":10,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家10\",\"sellerid\":\"10\"},{\"list\":[{\"bargainPrice\":159.0,\"createtime\":\"2017-10-14T21:49:15\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8716\\/197\\/1271594444\\/173291\\/2f40bb4f\\/59b743bcN8509428e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8347\\/264\\/1286771527\\/92188\\/5cf5ec04\\/59b7420fN65378e9e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7363\\/165\\/3000956253\\/190883\\/179a372\\/59b743bfNd0c79d93.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7399\\/112\\/2935531768\\/183594\\/b77c7d4a\\/59b7441aNc3d40133.jpg!q70.jpg\",\"num\":1,\"pid\":100,\"price\":2200.0,\"pscid\":112,\"selected\":0,\"sellerid\":11,\"subhead\":\"針織針織閃閃閃亮你的眼\",\"title\":\"維邇旎 2017秋冬新款長袖針織連衣裙韓版氣質中長款名媛包臀A字裙 zx179709 黑色 XL\"},{\"bargainPrice\":22.9,\"createtime\":\"2017-10-14T21:38:26\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t1930\\/284\\/2865629620\\/390243\\/e3ade9c4\\/56f0a08fNbd3a1235.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2137\\/336\\/2802996626\\/155915\\/e5e90d7a\\/56f0a09cN33e01bd0.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t1882\\/31\\/2772215910\\/389956\\/c8dbf370\\/56f0a0a2Na0c86ea6.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2620\\/166\\/2703833710\\/312660\\/531aa913\\/57709035N33857877.jpg!q70.jpg\",\"num\":5,\"pid\":34,\"price\":9.0,\"pscid\":2,\"selected\":0,\"sellerid\":11,\"subhead\":\"三隻松鼠零食特惠,專區滿99減50,滿199減100,火速搶購》\",\"title\":\"三隻松鼠 堅果炒貨 零食奶油味 碧根果225g\\/袋\"},{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:48:08\",\"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\":2,\"pid\":18,\"price\":399.0,\"pscid\":1,\"selected\":0,\"sellerid\":11,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家11\",\"sellerid\":\"11\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:39:05\",\"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\":3,\"pid\":19,\"price\":499.0,\"pscid\":1,\"selected\":0,\"sellerid\":12,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家12\",\"sellerid\":\"12\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:39:05\",\"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\":3,\"pid\":1,\"price\":118.0,\"pscid\":1,\"selected\":0,\"sellerid\":17,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家17\",\"sellerid\":\"17\"}]}";
try {
Gson gson = new Gson();
Type type = new TypeToken<Result<List<Shop>>>() {
}.getType();
result = gson.fromJson(resultString, type);
return result;
} catch (Exception e) {
}
return result;
}
}
9、Presenter層
public class CartPresenter extends BasePresenter {
public CartPresenter(DataCall dataCall) {
super(dataCall);
}
@Override
protected Result getData(Object... args) {
Result result = CartModel.goodsList();//調用網絡請求獲取數據
return result;
}
}
10、看一下自定義的View(用於加減商品的數量)
public class AddSubLayout extends LinearLayout implements View.OnClickListener {
private TextView jian;
private TextView jia;
private TextView num;
private AddSubListener addSubListener;
// 實例化接口的變量
public void setAddSubListener(AddSubListener addSubListener) {
this.addSubListener = addSubListener;
}
public AddSubLayout(Context context) {
super(context);
initView();
}
public AddSubLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public AddSubLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
// 第三個參數寫成this
View view = View.inflate(getContext(), R.layout.add_sub,this);
jian = view.findViewById(R.id.add_jian);
jia = view.findViewById(R.id.add_jia);
num = view.findViewById(R.id.num);
jia.setOnClickListener(this);
jian.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// 得到數量的值
int number = Integer.parseInt(num.getText().toString());
switch (v.getId()){
case R.id.add_jia:
number++;
num.setText(number+"");
break;
case R.id.add_jian:
if (number == 0) {
Toast.makeText(getContext(), "數量不可小於0", Toast.LENGTH_SHORT).show();
return;
}
number--;
num.setText(number+"");
break;
}
if (addSubListener!=null) {
addSubListener.addSub(number);
}
}
public void setCount(int count) {
num.setText(count+"");
}
// 自定義接口
public interface AddSubListener{
void addSub(int number);
}
}
11、看一下請求網絡的類(寫了日誌攔截器)
public class HttpUtils {
public static String get(String urlString) {
// 添加日誌攔截器
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)//連接超時
.readTimeout(20, TimeUnit.SECONDS)//讀取超時
.callTimeout(20, TimeUnit.SECONDS)//呼叫超時
.addInterceptor(loggingInterceptor)// 日誌攔截器
.build();
Request request = new Request.Builder().url(urlString).get().build();
try {
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
//okhttp使用單例模式
private static OkHttpClient singleton;
//非常有必要,要不此類還是可以被new,但是無法避免反射,好惡心
private HttpUtils(){
}
public static OkHttpClient getInstance() {
if (singleton == null)
{
synchronized (HttpUtils.class)
{
if (singleton == null)
{
singleton = new OkHttpClient();
}
}
}
return singleton;
}
}
12、看一下主佈局
<?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=".MainActivity">
<ExpandableListView
android:id="@+id/list_car"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<CheckBox
android:id="@+id/check_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="全選" />
<!--
android:layout_centerVertical="true"
表示在此佈局中(高)居中顯示
-->
<TextView
android:id="@+id/goods_sum_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/check_all"
android:text="價格:" />
</RelativeLayout>
</LinearLayout>
13、看一下自定義VIew的佈局
<?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:orientation="horizontal">
<TextView
android:id="@+id/add_jia"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/car_btn_bg"
android:focusable="false"
android:gravity="center"
android:text="+"
android:textSize="22sp" />
<TextView
android:id="@+id/num"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:focusable="false"
android:gravity="center"
android:text="1000"
android:textSize="22sp" />
<TextView
android:id="@+id/add_jian"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/car_btn_bg"
android:focusable="false"
android:gravity="center"
android:text="-"
android:textSize="22sp" />
</LinearLayout>
14、二級列表的第一級的佈局
<?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:orientation="vertical">
<CheckBox
android:id="@+id/g_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
android:focusable="false"/>
</LinearLayout>
15、二級列表的第二級的佈局
<?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"
android:background="#ddd"
android:orientation="horizontal"
android:padding="10dp">
<CheckBox
android:id="@+id/child_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp" />
<ImageView
android:id="@+id/image_cc"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/child_check"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/text_ttt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-3dp"
android:layout_toRightOf="@+id/image_cc"
android:padding="10dp"
android:text="aa" />
<TextView
android:id="@+id/text_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text_ttt"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/image_cc"
android:padding="3dp"
android:text="價格" />
<此處是你自定義View的類名+包名
android:id="@+id/add_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"></bawie.com.month_01_demo.core.AddSubLayout>
</RelativeLayout>
16、最後是自定義view的樣式
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp"></corners>
<stroke
android:width="2dp"
android:color="@android:color/holo_red_dark">
</stroke>
</shape>
//////最後看一下我的bean類吧
返回結果
Result.java
public class Result<T> {
int code;
String msg;
T data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
// 商家
Shop.java
public class Shop {
List<Goods> list;
String sellerName;
String sellerid;
// 自己添加的 爲了判斷複選框是否選中
boolean check;
public void setCheck(boolean check) {
this.check = check;
}
public boolean isCheck() {
return check;
}
public List<Goods> getList() {
return list;
}
public void setList(List<Goods> list) {
this.list = list;
}
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;
}
}
// 商品
Goods.java
public class Goods {
private double bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private int pid;
private double price;
private int pscid;
private int selected;
private int sellerid;
private String subhead;
private String title;
// 自己添加的 接口裏沒有 爲了設置商品的數量
private int count=1;
public void setCount(int count) {
this.count = count;
}
public int getCount() {
return count;
}
public double getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(double 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 double getPrice() {
return price;
}
public void setPrice(double 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;
}
}
// 到了這裏應該就可以出效果了吧 !!!!