美文网首页GB 移动小组
仿淘宝多商品SKU组合(Android版)

仿淘宝多商品SKU组合(Android版)

作者: 君山茫茫云归处 | 来源:发表于2017-07-26 15:13 被阅读134次

本文算法参考淘宝SKU查询算法,本文主要在于功能的实现,存在一些瑕疵,多多包涵。

查询原理这里就不做过多的赘述了,效果如下:

result.gif
  • 图中的按钮有3种状态,分别为已选中,可点击,无法点击。

功能实现步骤

Item正选
  • 假设选中值为5的Item后,对非5所在行的其他行的每个可以选中的Item进行遍历
for(遍历的item:所有可以被选中的item){
       for(可选结果的积:所有可选结果的积){
              result=(可选结果的积)%(5*(遍历的item的值/当前行选中的Item值))
         }
     }

PS:result为这个Item能否选中的结果。这里需要注意的遍历的只有所有可以选中的Item,不可以被选中的Item不参与遍历。

Item反选

相比正选逻辑,反选取消逻辑相对复杂

  • 假设取消值为113的Item。取消的界面刷新逻辑分为2部分,第一部分为取消Item的所在行,第二部分为非取消Item的其他行的。

    1. 取消的Item所在行
       for(所在行的Item:所在行的所有Item){
            for(可选结果的积:所有可选结果的积){
               result=可选结果的积%(所在行Item的值*(所有已选中的Item的积))
                }
         }
    
   2.非取消Item所在行
    ``` 
      for(当前行的Item:其他在行的所有Item){
           for(可选结果的积:所有可选结果的积){
              result=可选结果的积%(其他行Item的值*(所有已选中的Item的积/当前行的选中的Item的值))
               }
        }
  • 代码示例
    
      /**
       * @param list 所有已选中的Item的值
       * @param parent 当前点击Item所在的行
       * @param isCheck 是否是选中,true是选中,false为取消选中
       * @param checktag 当前点击的Item的值
       * @value cacheView 所有的行的集合
       * @value checkedProduct 所有行对应的已选中的Item的值的集合
       * @value cancheck 所有可选值的积的集合
       */
      public void checkSKU(ArrayList<Long> list, FlexboxLayout parent, boolean isCheck, Long checktag) {
          Long checkedResult = 1L;
          for (Long checked : list) {
              checkedResult *= checked;
          }
          for (int i = 0; i < cacheView.size(); i++) {
              FlexboxLayout flowLayout = (FlexboxLayout) cacheView.get(i);
              if (!isCheck) {//取消选择时候不做过滤
                  Long id = checkedProduct.get(flowLayout);
                  if (id != null && id != 0) {//有选中行
                      long r = checkedResult / id;//除去单前选中行后的积
                      for (int v = 0; v < flowLayout.getFlexItemCount(); v++) {
                          TextView tafText = (TextView) flowLayout.getFlexItemAt(v);
                          Long tag = (Long) tafText.getTag();
                          if (!tafText.isSelected()) {
                              for (Long cancheck : resultData) {
                                  if (cancheck % (tag * r) == 0) {
                                      tafText.setEnabled(true);
                                      tafText.setClickable(true);
                                      tafText.setSelected(false);
                                      break;
                                  }
                              }
                          }
    
                      }
                  } else {//当前行没有任何按钮被选中
                      for (int v = 0; v < flowLayout.getFlexItemCount(); v++) {
                          TextView tafText = (TextView) flowLayout.getFlexItemAt(v);
                          Long tag = (Long) tafText.getTag();
                          for (Long cancheck : resultData) {
                              if (cancheck % (tag * checkedResult) == 0) {
                                  tafText.setEnabled(true);
                                  tafText.setClickable(true);
                                  tafText.setSelected(false);
                                  break;
                              }
                          }
                      }
                  }
    
              } else {//选中按钮
                  for (int v = 0; v < flowLayout.getFlexItemCount(); v++) {
                     TextView tafText = (TextView) flowLayout.getFlexItemAt(v);
                      if (parent != flowLayout) {//选中时,当前行的其他Item不做更新
                          Long id = checkedProduct.get(flowLayout);
                          if(id==null||id==0)
                              id=1L;
                              Long tag = (Long) tafText.getTag();
                              long selectResult = tag *(checkedResult/id);//选中的按钮和可以选择的按钮的积,是否在集合里面
                              for (Long cancheck : resultData) {
                                  if (cancheck % selectResult == 0) {
                                      tafText.setEnabled(true);
                                      tafText.setClickable(true);
                                      tafText.setSelected(false);
                                      if (id != null && id.equals(tag)) {
                                          tafText.setSelected(true);
                                          defaultCheckTv.put(flowLayout, tafText);
                                      }
                                      break;
                                  } else {
                                      tafText.setSelected(false);
                                      tafText.setEnabled(false);
                                      tafText.setClickable(false);
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
    

最后附上github地址:仿淘宝SKU查询

相关文章

网友评论

    本文标题:仿淘宝多商品SKU组合(Android版)

    本文链接:https://www.haomeiwen.com/subject/heunkxtx.html