Android 选择商品属性sku
最近项目中使用SKU属性查询,类似淘宝京东商品的选择,在网上查询了弄了几个源码看看,发现还是实现不了多属性选择问题,再原基础上改动相当费事,所以想干脆自己处理这个问题,而多种熟悉问题貌似没有人分享,所以写出来思路,具体重点方法,提供给需要的人:
数据是这样的分两个大块儿 第一块儿就是刚进来的初始化数据List
第二块数据是有几种产品List 最终咱们要通过这个集合里的数据进行比较 来对按钮的颜色刷新,有红色灰色黑色
"{\"retCode\":\"0000\",\"retMsg\":\"操作成功\",\"retData\":[{\"goodsDetailVo\":{\"productId\":73318,\"saId\":2020409,\"productName\":\"荣耀 V9 play 3G+32G 极光蓝 全网通 原装\",\"mobileimg\":\"/20170914153743595_596.jpg\",\"overplusnumber\":1000,\"maxPrice\":null,\"minPrice\":1,\"activeId\":null,\"actyDetailId\":null,\"active_type\":-1,\"saleType\":\"2\",\"is_limited\":null,\"limited_num\":null,\"supply_city_name\":\"香港特别行政区\",\"buyer_county_name\":null,\"local_time\":null,\"local_before_date\":\"\",\"local_after_date\":\"\",\"orderCount\":0,\"productParams\":[{\"param\":\"颜色\",\"attrTypeLs\":[{\"value\":\"极光蓝\",\"select\":\"1\"},{\"value\":\"铂光金\",\"select\":\"0\"},{\"value\":\"幻夜黑\",\"select\":\"0\"}]},{\"param\":\"内存\",\"attrTypeLs\":[{\"value\":\"3G+32G\",\"select\":\"0\"},{\"value\":\"4G+32G\",\"select\":\"1\"}]},{\"param\":\"版本\",\"attrTypeLs\":[{\"value\":\"全网通\",\"select\":\"1\"}]},{\"param\":\"备注\",\"attrTypeLs\":[{\"value\":\"原装\",\"select\":\"1\"}]}],\"productLs\":[{\"productId\":73319,\"saId\":2020418,\"productName\":\"荣耀 V9 play 4G+32G 极光蓝 全网通 原装\",\"mobileimg\":\"/20170914153743595_596.jpg\",\"overplusnumber\":1000,\"minPrice\":1,\"maxPrice\":null,\"activeId\":null,\"actyDetailId\":null,\"active_type\":0,\"saleType\":\"2\",\"is_limited\":null,\"limited_num\":null,\"orderCount\":null,\"params\":[\"极光蓝\",\"4G+32G\",\"全网通\",\"原装\"],\"publicity\":false,\"actTime\":false},{\"productId\":73320,\"saId\":2020417,\"productName\":\"荣耀 V9 play 3G+32G 幻夜黑 全网通 原装\",\"mobileimg\":\"/20170914153819308_990.jpg\",\"overplusnumber\":1000,\"minPrice\":1,\"maxPrice\":null,\"activeId\":null,\"actyDetailId\":null,\"active_type\":0,\"saleType\":\"2\",\"is_limited\":null,\"limited_num\":null,\"orderCount\":null,\"params\":[\"幻夜黑\",\"3G+32G\",\"全网通\",\"原装\"],\"publicity\":false,\"actTime\":false},{\"productId\":73321,\"saId\":2020416,\"productName\":\"荣耀 V9 play 3G+32G 铂光金 全网通 原装\",\"mobileimg\":\"/20170914153849981_60.jpg\",\"overplusnumber\":1000,\"minPrice\":1,\"maxPrice\":null,\"activeId\":null,\"actyDetailId\":null,\"active_type\":0,\"saleType\":\"2\",\"is_limited\":null,\"limited_num\":null,\"orderCount\":null,\"params\":[\"铂光金\",\"3G+32G\",\"全网通\",\"原装\"],\"publicity\":false,\"actTime\":false},{\"productId\":73318,\"saId\":2020409,\"productName\":\"荣耀 V9 play 3G+32G 极光蓝 全网通 原装\",\"mobileimg\":\"/20170914153743595_596.jpg\",\"overplusnumber\":1000,\"minPrice\":1,\"maxPrice\":null,\"activeId\":null,\"actyDetailId\":null,\"active_type\":0,\"saleType\":\"2\",\"is_limited\":null,\"limited_num\":null,\"orderCount\":null,\"params\":[\"极光蓝\",\"3G+32G\",\"全网通\",\"原装\"],\"publicity\":false,\"actTime\":false},],\"publicity\":false,\"actTime\":false}}],\"cmd\":null,\"totalDataCount\":1,\"serviceTime\":1524020996415}"
最重要的就是adapter中对数据的逻辑处理,筛选,比对,结果集中拿到真正的数据 进行UI更新
/**
* 刚进页面 给默认值
*
* @param
*/
private void initData() {
for (int y = 0; y < childrenViews.length; y++) {
for (int z = 0; z < childrenViews[y].length; z++) {
//TextView textView = childrenViews[y][z];
if ("1".equals(mData.get(y).getAttrTypeLs().get(z).getSelect())) {
String value = childrenViews[y][z].getText().toString();
selectedValue[y] = value;
}
}
}
myInterface.selectedAttribute(selectedValue, paramsBean.getMobileimg(), paramsBean.getMinPrice() + "", paramsBean.getOverplusnumber() + "");
}
private void getSelectedProduct() {//获取选中当前哪一款商品
for (int i = 0; i < stockGoodsList.size(); i++) {
for (int j = 0; j < stockGoodsList.get(i).getParams().size(); j++) {
String str = stockGoodsList.get(i).getParams().get(j);
String selValue = selectedValue[j];
if (selValue.equals(str)) {
if (j == stockGoodsList.get(i).getParams().size() - 1) {
index = i;
}
}else{
break;
}
}
}
//Log.d("-----------index-->",index+"");
}
代码太多就不一一列举了 发个demo给大家借鉴一下 只要数据吻合 界面效果自然就出来啦
下面附上我的demo csdn下载地址