前言:
RecyclerView是android 最常见的列表控件,它现在几乎已经代替了listVIew和GridView的使用,RecyclerView自带强大的功能,实现多种样式,使用更加简单,封装也很容易,相对于listIView,RecyclerView没有自己的item点击事件,需要开发人员自行通过接口回调来实现点击事件,在近期工作不忙时,我查阅了一些开源的商城项目,写了一套很全面的商城app的购物车功能的demo。
闲话少说先上运行图:
就上传这三张图,了解下大致功能。
1.项目搭配代码进行分析(这里只提供主要方法,详细的demo最下方分享)
1),个数的增加与减少实现:
自定义一个类,继承LinearLayoutb (AddSubView)
对加减按钮进行编写
通过回调方法进行回调
响应回调方法
2)最外层布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="#fff"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginLeft="30dp" android:layout_weight="1" android:text="购物车" android:textColor="#303235" android:textSize="20sp" /> <TextView android:id="@+id/tv_shopcart_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="编辑" android:textColor="#303235" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="5dp" android:background="#eeee" /> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp" android:background="#eee" /> <LinearLayout android:id="@+id/ll_check_all" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" android:orientation="horizontal" android:visibility="visible"> <CheckBox android:id="@+id/checkbox_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:button="@null" android:drawableLeft="@drawable/checkbox_selector" android:drawablePadding="10dp" android:padding="10dp" android:paddingLeft="0dp" android:text="全选" android:textColor="#303235" android:textSize="15sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="合计: