前言
其实做一个电商购物车,还真不是一个轻松的活。但是只要掌握思路,一步一步来做,就会发现也就这样。废物不多说,直接上效果图
完整代码,github链接,希望能给个星,谢谢
效果图

主要思路
整一个布局就是ExpandableListView,然后自定义一个ActionBar,ActionBar上面显示购物车数量,通过ActionBar上面的编辑状态,店铺布局,所有商品布局,底部布局要进行相应的变化,编辑状态下需要改变商品的数量,删除商品,全选商品,隐藏店铺的编辑。非编辑状态可以显示店铺的编辑,显示结算,商品的信息。通过每一个店铺上面的编辑状态,该店铺旗下的所有商品布局都要进行相应的变化。编辑状态下,需要改变商品的数量和删除商品。非编辑状态下只需要显示商品的信息。当该店铺下所有商品都被勾选,对应的店铺也要被勾选。反之,该店铺下只要有一个商品没有被勾选,那么店铺就不用勾选。其实逻辑挺简单的,复杂的逻辑其实就是很多简单逻辑组成的,我们只需要把复杂的逻辑简单化成很多简单的逻辑,我们就能完成一个大概的思路
代码教学
我们第一步要做就是自定义一个ActionBar,几行代码就能解决。
private void initActionBar() {
//隐藏标题栏
if (getSupportActionBar() != null) {
//去掉阴影
getSupportActionBar().setElevation(0);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
View view = getLayoutInflater().inflate(R.layout.acitonbar, null);
findView(view);
getSupportActionBar().setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ActionBar.LayoutParams lp = (ActionBar.LayoutParams) view.getLayoutParams();
lp.gravity = Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.CENTER_HORIZONTAL;
getSupportActionBar().setCustomView(view, lp);
}
完成之后效果图:

第二步就是写整个布局xml文件了,基础知识也没有什么好说的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<in.srain.cube.views.ptr.PtrFrameLayout
android:background="#f1f1f1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mPtrframe"
xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
cube_ptr:ptr_resistance="1.7"
cube_ptr:ptr_ratio_of_header_height_to_refresh="1.2"
cube_ptr:ptr_duration_to_close="300"
cube_ptr:ptr_duration_to_close_header="2000"
cube_ptr:ptr_keep_header_when_refresh="true"
cube_ptr:ptr_pull_to_fresh="false" >
<FrameLayout
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_cart"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"/>
<!--分割线-->
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/gray" />
<LinearLayout
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<CheckBox
android:textSize="16sp"
android:background="@null"
android:clickable="true"
android:drawablePadding="10dp"
android:drawableLeft="@drawable/checkbox_bg"
android:text="@string/all"
android:id="@+id/all_checkBox"
android:button="@null"
android:minHeight="64dp"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.3"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<LinearLayout
android:gravity="center"
android:orientation="horizontal"
android:id="@+id/order_info"
android:layout_weight="0.7"
android:layout_width="0dp"
android:layout_height="wrap_content">
<LinearLayout
android:layout_marginEnd="20dp"
android:orientation="vertical"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="wrap_content">
<LinearLayout
android:gravity="end"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="@android:color/black"
android:textSize="18sp"
android:text="@string/order_total"
android:layout_marginStart="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/total_price"
android:text="¥0.00"
android:textSize="18sp"
android:textColor="@color/ic_taobao"
android:layout_width="wrap_c

最低0.47元/天 解锁文章
1039

被折叠的 条评论
为什么被折叠?



