购物车是IT电商项目必不可少的功能之一,尽管该功能不算很复杂,但是却很重要。实现的方式也多种多样,就我而言就曾经通过三种方式开发过购物车:第一种方式是通过LinearLayout嵌套LinearLayout实现,这种方式会消耗更多的内存,数据过多时会造成页面的卡顿,还可能产生OOM异常,不可取;第二种方式是通过ListView嵌套LinearLayout实现,这种方式与第一种大同小异,提升效率有限;第三种方式(也是目前我认为的最佳方式)是通过ExpandableListView实现购物车分店铺功能,由于ExpandableListView是系统原生控件,因此由系统底层维护并提供了更多的方法供我们使用,操作简单、页面流畅而且代码量较前两种也小了很多。
虽然第一、二中方式现在看起来很不友好,但当时也是我逐步摸索的实践过程,对第三种方式的完成提供了必不可少的贡献。当然,购物车的实现方式多种多样,应该还有更好的实现方式,这篇博客就当作抛砖引玉了,希望大家前来学习交流、批评指正、共同成长。
注:文章末尾附项目源码下载链接。
页面布局文件
Android开发,页面的美观、流畅与用户体验及其重要,因此开发购物车功能的第一步就是布局文件。购物车的主要功能包括:单选、全选、合计、删除、商品数量的加减,为了方便测试又新增了刷新数据功能,先看下页面效果(动图)。实际开发中,美观的页面需要UI的设计与我们的开发,这一点很重要!
以下为购物车的页面布局文件代码:该文件中使用的是ExpandableListView原生控件,来实现购物车的分店铺。实际开发中,如果遇到ScrollView嵌套ExpandableListView控件,可以使用自定义控件ExpandableListViewForScrollView解决ScrollView嵌套ExpandableListView,ExpandableListView显示不全的问题。
注:文章末尾附ExpandableListViewFroScrollView源码。
<?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="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ededed"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:id="@+id/tv_titlebar_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@null"
android:gravity="center"
android:paddingLeft="13dp"
android:paddingRight="13dp"
android:singleLine="true"
android:text="刷新数据"
android:textColor="#4c4c4c"
android:textSize="15dp"
android:visibility="visible" />
<TextView
android:id="@+id/tv_titlebar_center"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:ellipsize="end"
android:gravity="center"
android:maxLength="18"
android:singleLine="true"
android:text="购物车"
android:textColor="#1a1a1a"
android:textSize="17dp"
android:visibility="visible" />
<TextView
android:id="@+id/tv_titlebar_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@null"
android:gravity="center"
android:paddingLeft="13dp"
android:paddingRight="13dp"
android:singleLine="true"
android:text="编辑"
android:textColor="#4c4c4c"
android:textSize="15dp"
android:visibility="gone" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_alignParentBottom="true"
android:background="#cccccc" />
</RelativeLayout>
<ExpandableListView
android:id="@+id/elv_shopping_car"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ededed"
android:divider="@null"
android:groupIndicator="@null"
android:scrollbars="none"
android:visibility="gone" />
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#ffffff"
android:visibility="gone">
<LinearLayout
android:id="@+id/ll_select_all"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingRight="10dp">
<ImageView
android:id="@+id/iv_select_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:background="@mipmap/unselect" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="6dp"
android:text="全选"
android:textColor="#333333"
android:textSize="14dp" />
</LinearLayout>
<Button
android:id="@+id/btn_order"
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="#ee1d23"
android:text="去结算"
android:textColor="#ffffff"
android:textSize="16dp"
android:visibility="visible" />
<Button
android:id="@+id/btn_delete"
android:layout_width="125dp"
android:layout_height="match_parent"