電商材質選擇邏輯(不限材質數量)

這裏寫圖片描述

邏輯分析

非java語言 只需要看下面幾句

1.百度適用你的流式佈局
2.下面的假設性原則

後臺只是告訴我,可能多種材質具體又幾種,不確定,這就意味着我得寫出通用性的。
一開始我想的十分複雜。你每選擇一個 再選擇一個。都要去發生界面變化,還要去跟庫存比較選中和可選不可選的問題。
每一次選擇只能絕對其他行材質,不能決定和他同一類的。這樣想就複雜了。千變萬化。

  1. 我就開始思考什麼是不變的,那就是庫存,如下例,不管怎麼變,總庫存才5件不同的類型。
  2. 只要選中項符合這5件那麼就可選 這樣問題就明朗了
  3. 假設性原則
    我們沒有選中爲-1,初始化根據材質的數量-1_-1_-1_-1_-1_-1,假設 第一個材質的第一個被選中position【0,0】被選中 (此處要注意position和id是不一樣的)
    我們就能得到如黑色對應的datas+position—>id是1, 那麼現在選中項就是1_-1_-1_-1_-1_-1
    然後再選擇一個【4,2】 繼續這樣的比較1_-1_-1_-1_3_-1保存下來

    我們就開始假設性原則,把每一個按鈕假設選中進去替換-1 如果我們開始假設第二行某按鈕1_XID_-1_-1_3_-1
    XID是你假設被選中的id 我們去和5個庫存類型對比-1則視爲相等。不爲-1必須,至少有一個相等。相等則你假設的 選項就可以選
    如果沒有一個相等 那麼他就不符合 即當他被假設選中時候加上當前選中項,庫存裏面是沒有的。那麼他應該變爲灰色 示例代碼中
    我們是以#按鈕#作爲不可選狀態

  4. 既然得到了數據源,那麼界面的顯示變化到android裏面就簡單了

場景模擬

爲方便大家實際查看代碼有效性,用javase模擬一個場景
這裏寫圖片描述

//假設後臺傳回來的庫存每個商品具體材質的id 用“_”連接了起來,這裏就有5個商品了
//例如黑色大碼加厚_純棉_XX_XX 當然具體可能沒有這麼多材質。但爲了寫出通用的 不管他有多少材質 我們就做了個實驗

texture_ids_list.add("3_1_1_4_3_6");// 添加材質對應的id集合
        texture_ids_list.add("1_1_1_1_2_6");
        texture_ids_list.add("3_2_1_4_1_6");
        texture_ids_list.add("4_1_3_2_2_6");
        texture_ids_list.add("3_1_1_4_1_4");

直接運行 不是android項目

接入android直接改寫.PrintUI即可,然後main方法改爲初始化數據。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class aaad {

    public static List<String> texture_ids_list = new ArrayList<>();
    public static List<DataListInfo> datass = new ArrayList<>();

    // 根據庫存 動態根據用戶點擊 顯示該材質是否可選 ,多種材質有庫存牽制,選中變紅 未選中 白色 不可選變灰色
    public static void main(String[] args) {

        // 假設庫存中有的材質 爲了試驗可靠性 我們將材質分類設多一點,如 黑色_L_羊毛_品牌_男裝_XX
        // 假設後臺數據將材質id按3_1_1_4_5_6連接。此例子只提供邏輯到時候適配一下你們後臺數據即可
        texture_ids_list.add("3_1_1_4_3_6");// 添加材質對應的id集合
        texture_ids_list.add("1_1_1_1_2_6");
        texture_ids_list.add("3_2_1_4_1_6");
        texture_ids_list.add("4_1_3_2_2_6");
        texture_ids_list.add("3_1_1_4_1_4");

        // 所有材質集合 每層材質可能數量不相同
        // 服務器傳回來包括id的數據
        int[][] aaid = new int[][] { { 1, 2, 3, 4, 5 }, { 1, 2 }, { 1, 2, 3 },
                { 1, 2, 3, 4 }, { 1, 2, 3 }, { 1, 2, 3, 4, 5, 6 } };

        // 將帶有id的數據和狀態整合在一起。但實際操作中是服務器傳回來的數據裏面多加一個字段STATE 即可
        for (int i = 0; i < aaid.length; i++) {
            int[] item = aaid[i];
            List<Data> datas = new ArrayList<>();
            for (int j = 0; j < item.length; j++) {
                datas.add(new Data(item[j], null));
            }
            DataListInfo dataListInfo = new DataListInfo(datas);
            datass.add(dataListInfo);
        }

        // 根據庫存 初始化 是否可選材質
        // FormatState(-1, -1);
        freshUI();
        // 將材質UI顯示給用戶 初始化 用戶未選擇
        PrintUI(datass);

        String str = null;
        Scanner sc = null;
        System.out
                .println("請模擬點擊事件,選擇材質d :(如1空格3(下標)-----》點擊了第2行4列個材質)\n **缺貨 []選中  其他爲可選");
        while (true) {
            sc = new Scanner(System.in);
            str = sc.nextLine();

            if (str == null || str.length() != 3 || !str.contains(" "))
                System.out.println("輸入格式錯誤,請重新輸入");
            else {

                String[] split = str.split(" ");
                try {
                    int rowPosition = Integer.valueOf(split[0]).intValue();
                    int colPosition = Integer.valueOf(split[1]).intValue();

                    if (rowPosition + 1 > datass.size()
                            || colPosition + 1 > datass.get(rowPosition)
                                    .getDatas().size()) {
                        System.out.println("您選擇了一塊空區域,請重新輸入");
                        continue;
                    }

                    List<Data> datas = datass.get(rowPosition).getDatas();
                    Data item = datas.get(colPosition);
                    if (item.currState == Data.CHOOSE_STATE.disabled) {
                        System.out.println(item.getIdPrintStr()
                                + "該材質已經爲disabled不可選狀態");
                    } else {
                        ClearChoosed(datas);
                        item.setCurrState(Data.CHOOSE_STATE.choose);
                        datass.get(rowPosition).setCurrSelectPosition(
                                colPosition);
                        // FormatState(rowPosition, colPosition);
                        freshUI();
                        //
                        PrintUI(datass);
                        System.out.println("選擇成功,界面已經變化,請繼續選擇:");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    if (e.toString().contains("NumberFormatException")) {
                        System.out.println("輸入格式錯誤,您輸入了非數字,請重新輸入");
                    } else {
                        System.out.println("未知錯誤,請重新輸入");
                    }

                }

            }
        }
    }

    /**
     * 根據當前選擇+庫存 進行不可選狀態設定
     */
    private static void freshUI() {

        // 除了選中項狀態重置
        for (int i = 0; i < datass.size(); i++) {
            for (int j = 0; j < datass.get(i).datas.size(); j++) {

                if (datass.get(i).datas.get(j).getCurrState() != Data.CHOOSE_STATE.choose) {
                    datass.get(i).datas.get(j).setCurrState(
                            Data.CHOOSE_STATE.disabled);
                }

            }
        }
        int[] selectArr = new int[datass.size()];
        for (int i = 0; i < datass.size(); i++) {
            selectArr[i] = datass.get(i).getCurrSelectPosition() == -1?-1:datass.get(i).getDatas().get(datass.get(i).getCurrSelectPosition()).getId();
        }
        for (int i = 0; i < datass.size(); i++) {
            for (int j = 0; j < datass.get(i).datas.size(); j++) {

                int[] selectArr2 = selectArr.clone();
                selectArr2[i] = datass.get(i).getDatas().get(j).getId();// 將所有選擇項對應哪一項替換爲自己
                boolean is = isArrAvailable(selectArr2);

                if (is
                        && datass.get(i).getDatas().get(j).getCurrState() == Data.CHOOSE_STATE.disabled) {
                    datass.get(i).getDatas().get(j)
                            .setCurrState(Data.CHOOSE_STATE.unchoose);
                } else if (datass.get(i).getDatas().get(j).getCurrState() == Data.CHOOSE_STATE.unchoose) {
                    datass.get(i).getDatas().get(j)
                            .setCurrState(Data.CHOOSE_STATE.disabled);
                }
            }
        }
        System.err.println("可用");
        for (int i = 0; i < texture_ids_list.size(); i++) {
            System.out.println("--->" + texture_ids_list.get(i));
        }

    }

    private static boolean isArrAvailable(int[] selectArr) {
        for (int i = 0; i < texture_ids_list.size(); i++) {
            String string = texture_ids_list.get(i);
            String[] texture_idsplit = null;
            if (string.contains("_")) {
                texture_idsplit = string.split("_");
            } else {
                texture_idsplit = new String[] { string };
            }
            boolean is = true;
            for (int j = 0; j < texture_idsplit.length; j++) {
                if (selectArr[j] != -1
                        && Integer.valueOf(texture_idsplit[j]).intValue() != selectArr[j]) {
                    is = false;
                    break;
                }
            }

            if (is) {
                // 只要完美符合一個材質即可
                return is;
            }

        }

        for (int i = 0; i < selectArr.length; i++) {
            System.out.print(selectArr[i] + ",");
        }
        System.out.println("不匹配");
        return false;
    }
    /**
     * 模擬根據狀態顯示界面
     */
    private static void PrintUI(List<DataListInfo> datass) {
        for (int i = 0; i < datass.size(); i++) {
            List<Data> datas = datass.get(i).getDatas();
            for (int j = 0; j < datas.size(); j++) {
                System.out.print(datas.get(j).getIdPrintStr() + "  ");
            }
            System.out.println();
        }
    }

    /**
     * 清除當前層材質的選中項
     */
    private static void ClearChoosed(List<Data> datas) {
        for (int j = 0; j < datas.size(); j++) {
            if (datas.get(j).getCurrState() == Data.CHOOSE_STATE.choose) {
                datas.get(j).setCurrState(Data.CHOOSE_STATE.unchoose);
            }
        }
    }

    public static class DataListInfo {
        private int currSelectPosition = -1;
        private List<Data> datas;

        @Override
        public String toString() {
            return "DataListInfo [currSelectPosition=" + currSelectPosition
                    + ", datas=" + datas + "]";
        }

        public DataListInfo() {
            super();
        }

        public DataListInfo(List<Data> datas) {
            super();
            this.datas = datas;
        }

        public DataListInfo(int currSelectPosition, List<Data> datas) {
            super();
            this.currSelectPosition = currSelectPosition;
            this.datas = datas;
        }

        public int getCurrSelectPosition() {
            return currSelectPosition;
        }

        public void setCurrSelectPosition(int currSelectPosition) {
            this.currSelectPosition = currSelectPosition;
        }

        public List<Data> getDatas() {
            return datas;
        }

        public void setDatas(List<Data> datas) {
            this.datas = datas;
        }

    }

    public static class Data {
        // 除了原有的數據
        private CHOOSE_STATE currState = CHOOSE_STATE.disabled;// 默認未選中
        private int id;

        @Override
        public String toString() {
            return "Data [currState=" + currState + ", id=" + id + "]";
        }

        // unchoose可選而沒有選擇的狀態
        public static enum CHOOSE_STATE {
            choose, unchoose, disabled
        }

        public Data(int id, CHOOSE_STATE currState) {
            super();
            if (currState != null)
                this.currState = currState;
            this.id = id;
        }

        public String getIdPrintStr() {
            if (currState == CHOOSE_STATE.choose) {

                return "[" + id + "]";
            } else if (currState == CHOOSE_STATE.disabled) {

                return "*" + id + "*";
            } else {
                return " " + id + " ";
            }
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public CHOOSE_STATE getCurrState() {
            return currState;
        }

        public void setCurrState(CHOOSE_STATE currState) {
            this.currState = currState;
        }

    }

    // 已選id連接形式 1,-1,3,4,-1(-1爲沒有選擇比較時候爲空) 是否被庫存所包含 若不包括則爲不可選庫存texture_id
    // 返回仍然有效的庫存
    public static List<String> checkChoosePositionExistTexture_ids() {

        List<String> nextTextureIdList = new ArrayList<>();

        for (int i = 0; i < texture_ids_list.size(); i++) {
            boolean isContain = true;
            String texture_id = texture_ids_list.get(i);
            if (texture_id.contains("_")) {
                String[] split = texture_id.split("_");

                for (int j = 0; j < split.length; j++) {
                    int currId = Integer.valueOf(split[j]).intValue();
                    int currSelectPosition = datass.get(j)
                            .getCurrSelectPosition();// 注意下標不是id 不同材質id可能重複
                                                        // 但同一行position是唯一的
                    if (currSelectPosition == -1) {
                        continue;
                    } else {
                        // for datass
                        int currSelectId = datass.get(j).getDatas()
                                .get(currSelectPosition).getId();
                        if (currSelectId != currId) {
                            // 如果當前材質確實有選中且 選中對應到該條庫存不相同
                            // 那麼該條庫存不適用於 已經選擇的按鈕
                            isContain = false;
                            break;
                        }
                    }

                }
            } else {

                if (datass.size() == 1
                        && datass.get(0).getDatas()
                                .get(datass.get(0).getCurrSelectPosition())
                                .getId() == Integer.valueOf(texture_id)) {
                    isContain = true;
                } else {
                    isContain = false;
                    System.out
                            .println("checkChoosePositionExistTexture_ids數據出錯!");
                }
            }
            if (isContain)
                nextTextureIdList.add(texture_id);

        }
        System.out.println("仍然可選:nextTextureIdList:"
                + nextTextureIdList.toString());

        return nextTextureIdList;

    }
}

android實際使用代碼

  • 調用部分
textureSelectUtils = new TextureSelectUtils();

                    textureSelectUtils.initData(new TextureSelectUtils.PrintUIImpl() {
                        @Override
                        public void reFreshChooseUI(List<TextureSelectUtils.DataListInfo> datass) {

                            for (int i = 0; i < tables.size(); i++) {
                                CustomFlowTables tb = tables.get(i);
                                for (int j = 0; j < tb.getChildCount(); j++) {
                                    View view = tb.getChildAt(j);
                                    //這是我的自定義view會根據setEnabled+setSelected顯示效果、
                                    //根據屏幕寬度流佈局 有興趣的可以聯繫我
                                    TextureSelectUtils.Data.CHOOSE_STATE state = datass.get(i).getDatas().get(j).getCurrState();
                                    if (state == TextureSelectUtils.Data.CHOOSE_STATE.disabled) {
                                        view.setEnabled(false);
                                    } else {
                                        view.setEnabled(true);
                                        if (state == TextureSelectUtils.Data.CHOOSE_STATE.choose) {
                                            view.setSelected(true);
                                        } else {
                                            view.setSelected(false);
                                        }
                                    }


//
                                }
                            }

                            updataProductInf(datass);//繼續更新其他部分 如價格  或者已選擇的名字
                            //通過getCurrSelectName()拼接
                        }
                    }, data.getData());
  • 工具類部分
package com.wcl.module.rex.utils.TextureSelect;

/**
 * Created by  Rex on 2017/5/2.
 */

import com.wcl.entity.response.RespProductTexture;

import java.util.ArrayList;
import java.util.List;


public class TextureSelectUtils {


    public interface PrintUIImpl {
        void reFreshChooseUI(List<DataListInfo> datass);

    }

    private List<String> texture_ids_list = new ArrayList<>();
    private List<DataListInfo> datass = new ArrayList<>();
    private PrintUIImpl Impl;

    public void initData(PrintUIImpl Impl, RespProductTexture.DataBean textureDatas) {
        if (textureDatas == null) {
            return;
        }
        this.Impl = Impl;
        List<RespProductTexture.DataBean.TextureBean> texture = textureDatas.getTexture();//庫存
        // 添加材質對應的id集合
        for (int i = 0; i < texture.size(); i++) {
            texture_ids_list.add(texture.get(i).getTexture_ids());
        }
        List<RespProductTexture.DataBean.ListBean> textureDatasList = textureDatas.getList();//所有選項

        for (int i = 0; i < textureDatasList.size(); i++) {
            List<RespProductTexture.DataBean.ListBean.ListBean2> list2 = textureDatasList.get(i).getList();
            List<Data> datas = new ArrayList<>();
            for (int j = 0; j < list2.size(); j++) {
                datas.add(new Data(list2.get(j).getId(), null,list2.get(j).getGname()));
            }
            DataListInfo dataListInfo = new DataListInfo(datas);
            datass.add(dataListInfo);
        }
        initUI();
        // 所有材質集合 每層材質可能數量不相同
        // 服務器傳回來包括id的數據
    }

    // 根據庫存 動態根據用戶點擊 顯示該材質是否可選 ,多種材質有庫存牽制,選中變紅 未選中 白色 不可選變灰色
    public void initUI() {
        freshUIData();
        // 將材質UI顯示給用戶 初始化 用戶未選擇
        PrintUI(datass);
    }

    public void onClick(int textureI, int textureJ) {

        List<Data> datas = datass.get(textureI).getDatas();
        Data item = datas.get(textureJ);
        if (item.currState == Data.CHOOSE_STATE.disabled) {
            System.out.println(item.getIdPrintStr()
                    + "該材質已經爲disabled不可選狀態");
        } else {
            ClearChoosed(datas);
            item.setCurrState(Data.CHOOSE_STATE.choose);
            datass.get(textureI).setCurrSelectPosition(
                    textureJ);
            // FormatState(rowPosition, colPosition);
            freshUIData();
            //
            PrintUI(datass);
            System.out.println("選擇成功,界面已經變化,請繼續選擇:");
        }
    }

    /**
     * 根據當前選擇+庫存 進行不可選狀態設定
     */
    private void freshUIData() {

        // 除了選中項狀態重置
        for (int i = 0; i < datass.size(); i++) {
            for (int j = 0; j < datass.get(i).datas.size(); j++) {

                if (datass.get(i).datas.get(j).getCurrState() != Data.CHOOSE_STATE.choose) {
                    datass.get(i).datas.get(j).setCurrState(
                            Data.CHOOSE_STATE.disabled);
                }

            }
        }
        int[] selectArr = new int[datass.size()];
        for (int i = 0; i < datass.size(); i++) {
            selectArr[i] = datass.get(i).getCurrSelectPosition() == -1 ? -1 : datass.get(i).getDatas().get(datass.get(i).getCurrSelectPosition()).getId();
        }
        for (int i = 0; i < datass.size(); i++) {
            for (int j = 0; j < datass.get(i).datas.size(); j++) {

                int[] selectArr2 = selectArr.clone();
                selectArr2[i] = datass.get(i).getDatas().get(j).getId();// 將所有選擇項對應哪一項替換爲自己
                boolean is = isArrAvailable(selectArr2);


                if (is && datass.get(i).getDatas().get(j).getCurrState() == Data.CHOOSE_STATE.disabled) {
                    datass.get(i).getDatas().get(j)
                            .setCurrState(Data.CHOOSE_STATE.unchoose);
                } else if (datass.get(i).getDatas().get(j).getCurrState() == Data.CHOOSE_STATE.unchoose) {
                    datass.get(i).getDatas().get(j)
                            .setCurrState(Data.CHOOSE_STATE.disabled);
                }
            }
        }
        for (int i = 0; i < texture_ids_list.size(); i++) {
            System.out.println("--->" + texture_ids_list.get(i));
        }

    }

    private boolean isArrAvailable(int[] selectArr) {
        for (int i = 0; i < texture_ids_list.size(); i++) {
            String string = texture_ids_list.get(i);
            String[] texture_idsplit = null;
            if (string.contains("_")) {
                texture_idsplit = string.split("_");
            } else {
                texture_idsplit = new String[]{string};
            }
            boolean is = true;
            for (int j = 0; j < texture_idsplit.length; j++) {
                if (selectArr[j] != -1
                        && Integer.valueOf(texture_idsplit[j]).intValue() != selectArr[j]) {
                    is = false;
                    break;
                }
            }

            if (is) {
                // 只要完美符合一個材質即可
                return is;
            }

        }

        for (int i = 0; i < selectArr.length; i++) {
            System.out.print(selectArr[i] + ",");
        }
        System.out.println("不匹配");
        return false;
    }

    /**
     * 模擬根據狀態顯示界面
     */
    private void PrintUI(List<DataListInfo> datass) {
        if (Impl != null) {
            Impl.reFreshChooseUI(datass);
        }

        for (int i = 0; i < datass.size(); i++) {
            for (int j = 0; j < datass.get(i).getDatas().size(); j++) {
               System.out.print(datass.get(i).getDatas().get(j).getIdPrintStr());
            }
            System.out.println();
        }
    }

    /**
     * 清除當前層材質的選中項
     */
    private void ClearChoosed(List<Data> datas) {
        for (int j = 0; j < datas.size(); j++) {
            if (datas.get(j).getCurrState() == Data.CHOOSE_STATE.choose) {
                datas.get(j).setCurrState(Data.CHOOSE_STATE.unchoose);
            }
        }
    }

    public static class DataListInfo {
        private int currSelectPosition = -1;
        private String currSelectName = "";
        private List<Data> datas;

        @Override
        public String toString() {
            return "DataListInfo [currSelectPosition=" + currSelectPosition
                    + ", datas=" + datas + "]";
        }

        public String getCurrSelectName() {
            if (currSelectPosition == -1 || datas == null || datas.size() == 0)
                return "";
            else
                return datas.get(currSelectPosition).getName();

        }

        public void setCurrSelectName(String currSelectName) {
            this.currSelectName = currSelectName;
        }

        public DataListInfo() {
            super();
        }

        public DataListInfo(List<Data> datas) {
            super();
            this.datas = datas;
        }

        public DataListInfo(int currSelectPosition, List<Data> datas) {
            super();
            this.currSelectPosition = currSelectPosition;
            this.datas = datas;
        }

        public int getCurrSelectPosition() {
            return currSelectPosition;
        }

        public void setCurrSelectPosition(int currSelectPosition) {
            this.currSelectPosition = currSelectPosition;
        }

        public List<Data> getDatas() {
            return datas;
        }

        public void setDatas(List<Data> datas) {
            this.datas = datas;
        }

    }

    public static class Data {
        // 除了原有的數據
        private CHOOSE_STATE currState = CHOOSE_STATE.disabled;// 默認未選中
        private int id;
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Data [currState=" + currState + ", id=" + id + "]";
        }

        // unchoose可選而沒有選擇的狀態
        public  enum CHOOSE_STATE {
            choose, unchoose, disabled
        }

        public Data(int id, CHOOSE_STATE currState, String name) {
            super();
            if (currState != null)
                this.currState = currState;
            this.id = id;
            this.name = name;
        }

        public String getIdPrintStr() {
            if (currState == CHOOSE_STATE.choose) {

                return "[" + id + "]";
            } else if (currState == CHOOSE_STATE.disabled) {

                return "*" + id + "*";
            } else {
                return " " + id + " ";
            }
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public CHOOSE_STATE getCurrState() {
            return currState;
        }

        public void setCurrState(CHOOSE_STATE currState) {
            this.currState = currState;
        }

    }


}
發佈了58 篇原創文章 · 獲贊 56 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章