本篇博客将在前两篇基础上,完成第三个页面购物车页面的基本逻辑代码以及最后支付界面的布局绘制,实现效果图如下:
在商品列表界面的分析中,可知通过购物车的图标点击跳转到购物车界面,然后获取统一数据的buyList集合作为ListView列表的数据源展示,购物车界面布局与商品列表界面布局相似,上中下结构,下面为一个按钮和总计金额TextView表示,金额会随着购物车的内容变化发生改变。
购物车布局activity_shop.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".hw.ShopActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:id="@+id/shop_top">
<TextView
android:text="购物车"
android:textSize="22sp"
android:textColor="#FFFFFF"
android:padding="15dp"
android:textStyle="bold"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:id="@+id/shop_daily_title_tv"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/shop_daily_back"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:src="@mipmap/icon_back"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/shop_bottom">
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000"/>
<Button
android:id="@+id/shop_calculate_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结算"
android:textColor="#FFFFFF"
android:layout_margin="10dp"
android:background="@drawable/buy_button"
android:layout_alignParentRight="true"/>
<TextView
android:id="@+id/shop_cal_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥14.50"
android:textColor="#FF0000"
android:textSize="22sp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/shop_calculate_btn"/>
<TextView
android:id="@+id/shop_cal_tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="总计:"
android:textSize="22sp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/shop_cal_tv"/>
</RelativeLayout>
<ListView
android:id="@+id/shop_daily_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:layout_below="@id/shop_top"
android:layout_above="@id/shop_bottom"
android:divider="#DCDCDC"></ListView>
</RelativeLayout>
ListView列表的每一个item的布局与商品列表界面相似,只不过将购买按钮,编程购物车删除按钮,故布局改变不大,其item布局item_shop.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="20dp"
android:descendantFocusability="blocksDes