先写框架
跟上一个XRecyclerView 框架差不多
在model层里加一个CartModel
public class CartModel { public static Result goodsList() { String resultString = HttpUtils.get("http://www.zhaoapi.cn/product/getCarts?uid=71"); try { Gson gson = new Gson(); Type type = new TypeToken<Result<List<Shop>>>() { }.getType(); Result result = gson.fromJson(resultString, type); // Result<List<Goods>> result = new Result<>(); // result.setCode(0); // List<Goods> list = new ArrayList<>(); // for (int i = 0; i < 30; i++) { // Goods goods = new Goods(); // goods.setImages(""); // goods.setTitle("手机"+i); // list.add(goods); // } // result.setData(list); return result; } catch (Exception e) { } Result result = new Result(); result.setCode(-1); result.setMsg("数据解析异常"); return result; } }
Presenter里面的
public class CartPresenter extends BasePresenter { public CartPresenter(DataCall dataCall) { super(dataCall); } @Override protected Result getData(Object... args) { Result result = CartModel.goodsList(); return result; } }
购物车布局页面
shop.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="match_parent" android:orientation="vertical"> <ExpandableListView android:id="@+id/list_cart" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> </ExpandableListView> <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="全选" /> <TextView android:id="@+id/goods_sum_price" android:layout_toRightOf="@+id/check_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="价格:" android:layout_marginLeft="20dp" android:layout_centerVertical="true"/> </RelativeLayout> </LinearLayout>
自定义控件布局:
页面信息布局
cart_item.xml
<?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:padding="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:orientation="horizontal"> <CheckBox android:id="@+id/cart_goods_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true"/> <ImageView android:id="@+id/image" android:layout_width="100dp" android:layout_height="wrap_content" android:adjustViewBounds="true" android:minHeight="50dp" android:layout_toRightOf="@+id/cart_goods_check" android:src="@mipmap/ic_launcher"/> <TextView android:id="@&#