使用recyclerView,GridView来实现动态显示商品选择规格
在很多的电商的项目当中都有添加购物车的这项功能,而添加购物车功能必不可少的就是选择商品的规格,比如说,尺寸、颜色、口味等等,如果每个商品只有一个规格,那就非常好办,但是由于商品的属性的多样性,也就意味着商品属性的不确定性,所以在编写选择商品规格的时候就不能固定商品属性的选择。由于本人在android方面也是菜鸟一只,当项目需要的时候也是上网去搜索大牛写的关于商品选择规格的实现,以下是我上网搜索的一篇博客:http://blog.youkuaiyun.com/zxw136511485/article/details/50357354,本篇文章是在此文章的基础上进行改写的,感谢这位博主的分享。
好了,接下来就进入正题了,先说说我的实现的思路,首先一般的商品选择规格的界面都是如下图所示:
一般都是点击购物车按钮,popupWindow来进行由下方向上方弹出,而在popupWindow的布局中一般是有商品名称,图片,以及商品的属性的选择,这里我采用recyclerView加GridView实现,也就是recyclerView中的每个item项由一个TextView和一个GridView实现。
首先是布局文件:
activity_recyler_sku.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.zqy.myselectorspec.RecylerSkuActivity">
<LinearLayout
android:id="@+id/ll_botttom"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/id_put_cart"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff9100"
android:text="加入购物车"
android:textColor="#fff"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff5001"
android:text="立即购买"
android:textColor="#fff"
/>
</LinearLayout><pre name="code" class="plain">
gridview_item.xml
<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="wrap_content"
android:gravity="center"
android:padding="0dp" >
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@xml/shape1"
android:gravity="center" >
<TextView
android:id="@+id/ItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView01"
android:textSize="15sp" >
</TextView>
</LinearLayout>
</LinearLayout>
recycler_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title"
android:textSize="18sp"
android:padding="12dp"
/>
<com.zqy.myselectorspec.MyGridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:cacheColorHint="#00000000"
android:horizontalSpacing="10dp"
android:listSelector="#00000000"
android:numColumns="3"
android:padding="5dp"
android:scrollbars="none"
android:verticalSpacing="10dp" />
</Li