时间:2021-05-20
前言:
最近又做到这一块的需求,以前也做过类似仿淘宝的属性选择,当时在网上下载的demo参考,最多也支持两组商品属性,用的两个gridview结合,扩展性很差,这次不打算用之前的代码,所以重新自己写了一个demo**(文末附上项目地址)**
如图所示,界面UI这一块肯定不用gridview,那样太过繁琐,所以采用recyclerview,item里面渲染ViewGroup,根据数据源的数量,往ViewGroup里面添加Textview。这样就可以解决它的每个属性按钮宽高自适应。
这里重点是重写ViewGroup里面的onMeasure和onLayout方法:
接下来是SKU的算法,因为本人的学生时期数学没有好好学习,幂集什么的,都不是很懂。所以在这里用了另外一种方法,把选项状态(三种:不能选择,可以选择,已选中)依次对属性按钮做出修改,这里虽然做了一些不必要的循环判断,但胜在功能的实现,如果大家有更好的想法,望不吝赐教。
贴上adapter代码(重点initOptions、canClickOptions和getSelected三个方法)
/** * Created by 胡逸枫 on 2017/1/16. */public class GoodsAttrsAdapter extends BaseRecyclerAdapter<GoodsAttrsBean.AttributesBean> { private SKUInterface myInterface; private SimpleArrayMap<Integer, String> saveClick; private List<GoodsAttrsBean.StockGoodsBean> stockGoodsList;//商品数据集合 private String[] selectedValue; //选中的属性 private TextView[][] childrenViews; //二维 装所有属性 private final int SELECTED = 0x100; private final int CANCEL = 0x101; public GoodsAttrsAdapter(Context ctx, List<GoodsAttrsBean.AttributesBean> list, List<GoodsAttrsBean.StockGoodsBean> stockGoodsList) { super(ctx, list); this.stockGoodsList = stockGoodsList; saveClick = new SimpleArrayMap<>(); childrenViews = new TextView[list.size()][0]; selectedValue = new String[list.size()]; for (int i = 0; i < list.size(); i++) { selectedValue[i] = ""; } } public void setSKUInterface(SKUInterface myInterface) { this.myInterface = myInterface; } @Override public int getItemLayoutId(int viewType) { return R.layout.item_skuattrs; } @Override public void bindData(RecyclerViewHolder holder, int position, GoodsAttrsBean.AttributesBean item) { TextView tv_ItemName = holder.getTextView(R.id.tv_ItemName); SKUViewGroup vg_skuItem = (SKUViewGroup) holder.getView(R.id.vg_skuItem); tv_ItemName.setText(item.getTabName()); List<String> childrens = item.getAttributesItem(); int childrenSize = childrens.size(); TextView[] textViews = new TextView[childrenSize]; for (int i = 0; i < childrenSize; i++) { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.setMargins(5, 5, 5, 0); TextView textView = new TextView(mContext); textView.setGravity(Gravity.CENTER); textView.setPadding(15, 5, 15, 5); textView.setLayoutParams(params); textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.saddlebrown)); textView.setText(childrens.get(i)); textView.setTextColor(ContextCompat.getColor(mContext, R.color.white)); textViews[i] = textView; vg_skuItem.addView(textViews[i]); } childrenViews[position] = textViews; initOptions(); canClickOptions(); getSelected(); } private int focusPositionG, focusPositionC; private class MyOnClickListener implements View.OnClickListener { //点击操作 选中SELECTED 取消CANCEL private int operation; private int positionG; private int positionC; public MyOnClickListener(int operation, int positionG, int positionC) { this.operation = operation; this.positionG = positionG; this.positionC = positionC; } @Override public void onClick(View v) { focusPositionG = positionG; focusPositionC = positionC; String value = childrenViews[positionG][positionC].getText().toString(); switch (operation) { case SELECTED: saveClick.put(positionG, positionC + ""); selectedValue[positionG] = value; myInterface.selectedAttribute(selectedValue); break; case CANCEL: saveClick.put(positionG, ""); for (int l = 0; l < selectedValue.length; l++) { if (selectedValue[l].equals(value)) { selectedValue[l] = ""; break; } } myInterface.uncheckAttribute(selectedValue); break; } initOptions(); canClickOptions(); getSelected(); } } class MyOnFocusChangeListener implements View.OnFocusChangeListener { private int positionG; private int positionC; public MyOnFocusChangeListener(int positionG, int positionC) { this.positionG = positionG; this.positionC = positionC; } @Override public void onFocusChange(View v, boolean hasFocus) { String clickpositionC = saveClick.get(positionG); if (hasFocus) { v.setBackgroundColor(ContextCompat.getColor(mContext, R.color.pink)); if (TextUtils.isEmpty(clickpositionC)) { ((TextView) v).setTextColor(ContextCompat.getColor(mContext, R.color.dodgerblue)); } else if (clickpositionC.equals(positionC + "")) { } else { ((TextView) v).setTextColor(ContextCompat.getColor(mContext, R.color.dodgerblue)); } } else { v.setBackgroundColor(ContextCompat.getColor(mContext, R.color.saddlebrown)); if (TextUtils.isEmpty(clickpositionC)) { ((TextView) v).setTextColor(ContextCompat.getColor(mContext, R.color.white)); } else if (clickpositionC.equals(positionC + "")) { } else { ((TextView) v).setTextColor(ContextCompat.getColor(mContext, R.color.white)); } } } } /** * 初始化选项(不可点击,焦点消失) */ private void initOptions() { for (int y = 0; y < childrenViews.length; y++) { for (int z = 0; z < childrenViews[y].length; z++) {//循环所有属性 TextView textView = childrenViews[y][z]; textView.setEnabled(false); textView.setFocusable(false); textView.setTextColor(ContextCompat.getColor(mContext, R.color.gray));//变灰 } } } /** * 找到符合条件的选项变为可选 */ private void canClickOptions() { for (int i = 0; i < childrenViews.length; i++) { for (int j = 0; j < stockGoodsList.size(); j++) { boolean filter = false; List<GoodsAttrsBean.StockGoodsBean.GoodsInfoBean> goodsInfo = stockGoodsList.get(j).getGoodsInfo(); for (int k = 0; k < selectedValue.length; k++) { if (i == k || TextUtils.isEmpty(selectedValue[k])) { continue; } if (!selectedValue[k].equals(goodsInfo .get(k).getTabValue())) { filter = true; break; } } if (!filter) { for (int n = 0; n < childrenViews[i].length; n++) { TextView textView = childrenViews[i][n];//拿到所有属性TextView String name = textView.getText().toString(); //拿到属性名称 if (goodsInfo.get(i).getTabValue().equals(name)) { textView.setEnabled(true);//符合就变成可点击 textView.setFocusable(true); //设置可以获取焦点 //不要让焦点乱跑 if (focusPositionG == i && focusPositionC == n) { textView.setTextColor(ContextCompat.getColor(mContext, R.color.dodgerblue)); textView.requestFocus(); } else { textView.setTextColor(ContextCompat.getColor(mContext, R.color.white)); } textView.setOnClickListener(new MyOnClickListener(SELECTED, i, n) { }); textView.setOnFocusChangeListener(new MyOnFocusChangeListener(i, n) { }); } } } } } } /** * 找到已经选中的选项,让其变红 */ private void getSelected() { for (int i = 0; i < childrenViews.length; i++) { for (int j = 0; j < childrenViews[i].length; j++) {//拿到每行属性Item TextView textView = childrenViews[i][j];//拿到所有属性TextView String value = textView.getText().toString(); for (int m = 0; m < selectedValue.length; m++) { if (selectedValue[m].equals(value)) { textView.setTextColor(ContextCompat.getColor(mContext, R.color.red)); textView.setOnClickListener(new MyOnClickListener(CANCEL, i, j) { }); } } } } }}下载链接:
GitHub:地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
亲,原因是:商品SKU全部未勾选、SKU失效或SKU信息没有补充全,请补充完整SKU销售属性信息并将销售的SKU点击“启用”,否则系统自动删除所有SKU。(若无
本文实例讲述了Android简单实现自定义流式布局的方法。分享给大家供大家参考,具体如下:首先来看一下手淘HD-商品详情-选择商品属性页面的UI商品有很多尺码,
亲,不同类目的商品名称、商品属性是不同的,若您发布商品行业设定的商品属性(如:子类目名称、SKU属性、产品/商品属性、下拉框属性值等)无法满足发布需求,请根据实
四、外围招商规则注:1、针对多SKU促销价报名的商品一旦审核通过都不予许进行sku价格编辑、删除、新增、修改sku名称(含编辑SKU属性)、下架等操作,如需调整
商家们都知道,当一款商品拥有颜色、型号、包装容量、价格等多种属性时,可通过设置SKU属性来细分这款商品。那如何查看宝贝一个sku卖了多少?下面来我们就来给大家讲