view的两种实现方式

想通过一个小功能的实现来表达我对编程的一些看法
一个多列表选择框,我第一种方案用了一个自定义view逻辑全写在一个类里,一个小时内很快就搞定了
但我也清楚,会有很大一些人如果看这个代码,会觉得很乱,甚至看不懂,
甚至有些人会很生气的说 这些的什么代码啊,根本没法看。
我理解他们,所以我特意写了一个进一步拆解成不同对象来更好理解的方式
但我想说的是如果是一个对编码有感觉的话,看第一个是很容易看懂的,不是他们写的不好理解,而是另一些人太不懂和机器打交道了,我也知道面向对象的设计,但设计的粒度要多细,我觉得还是的得看人

根据个人对代码量的控制程度来选择两种编码方式,
第一种方式,直接在filterview里面进行点击及列表响应的处理(我的推荐,简单高效)
第二种方式,专门通过一个对象FilterObject来管理选择按钮的状态和数据

第一种方式

public class FilterView extends SkinCompatLinearLayout {
    public final static int searchType = 7309;
    public final static String[] ChargeTitle ={"Speed","Brand"};

    public final static String[] ChargeType ={"All","Slow","Fast"};
    public final static String[] ChargeBrand ={"All","EV Station Pluz","PEA Volta","Elex by EGAT","EA Anywhere","Evolt","Sharge"};

    //power > 50 为快充
    public final static String[] ChargeTypeValue ={"0","0","50"};
    public final static String[] ChargeBrandValue ={"","EV Station Pluz","PEA Volta","Elex by EGAT","EA Anywhere","Evolt","Sharge"};

    private SkinCompatTextView[] titleViews = new SkinCompatTextView[2];

    private final int TYPE_INDEX = 0;
    private final int BRAND_INDEX = 1;

    private SparseArray<String[]> allColums = new SparseArray();
    private SparseArray<String[]> allColumValues = new SparseArray();
    //每列对应的选中index
    private SparseArray<Integer> selectFilter = new SparseArray();
    //第几列
    private int showRow = -1;
    private Context mContext;

    private RecyclerView recyclerView;
    private int selectColor = 0xff8ACB04;/
    private int normolColor = 0xff35383D;

    public interface FilterChange{
        void onFilterChange(String chargetype ,String brand);
    }

    FilterChange mFilterChange;

    public void setFilterChange(FilterChange filterChange){
        mFilterChange = filterChange;
    }

    public FilterView(Context context) {
        super(context);
        initdata();
        initView(context);
    }

    private void initdata(){

        allColums.put(TYPE_INDEX,ChargeType);
        allColums.put(BRAND_INDEX,ChargeBrand);

        allColumValues.put(TYPE_INDEX,ChargeTypeValue);
        allColumValues.put(BRAND_INDEX,ChargeBrandValue);

        selectFilter.put(TYPE_INDEX,0);
        selectFilter.put(BRAND_INDEX,0);
    }

    public FilterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initdata();
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        SkinCompatLinearLayout moreView = (SkinCompatLinearLayout) LayoutInflater.from(mContext).inflate(R.layout.filterlayout, null);
        addView(moreView);
        moreView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        titleViews[TYPE_INDEX] = moreView.findViewById(R.id.filter_left);
        titleViews[BRAND_INDEX] = moreView.findViewById(R.id.filter_right);
        recyclerView = moreView.findViewById(R.id.recyclerView);
        titleViews[TYPE_INDEX].setOnClickListener(onClickListener);
        titleViews[BRAND_INDEX].setOnClickListener(onClickListener);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        //freshListview();
    }

    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.filter_left){
                showRow = TYPE_INDEX;
            }else if(v.getId() == R.id.filter_right){
                showRow = BRAND_INDEX;
            }
            freshListview();
        }
    };

    private void freshListview(){
        if (recyclerView.getVisibility() == View.VISIBLE) {
            recyclerView.setVisibility(View.GONE);
            return;
        }
        recyclerView.setVisibility(View.VISIBLE);
        recyclerView.setAdapter(new BaseRecyclerAdapter(mContext, Arrays.asList(allColums.get(showRow)),R.layout.charge_filter_view) {
            @Override
            public void convert(MViewHolder holder, int position) {
                ((TextView)holder.getView(R.id.filter_name)).setText(allColums.get(showRow)[position]);
                if(position == selectFilter.get(showRow)){
                    ((TextView)holder.getView(R.id.filter_name)).setTextColor(selectColor);
                    ((TextView)holder.getView(R.id.filter_name)).setCompoundDrawablesWithIntrinsicBounds(null,null,ContextCompat.getDrawable(ContextHelper.getContext(), R.drawable.charge_select),null);
                }else{
                    ((TextView)holder.getView(R.id.filter_name)).setTextColor(normolColor);
                    ((TextView)holder.getView(R.id.filter_name)).setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
                }
            }
        }.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(MViewHolder holder, int position) {

                selectFilter.put(showRow,position);
                recyclerView.setVisibility(View.GONE);
                resetTitleview();

                if (mFilterChange != null) {
                    String brand = allColumValues.get(BRAND_INDEX)[selectFilter.get(BRAND_INDEX)];
                    String chargetype = allColumValues.get(TYPE_INDEX)[selectFilter.get(TYPE_INDEX)];
                    mFilterChange.onFilterChange(chargetype,brand);
                }
            }
        }));
        freshTitleview();
    }

    private void freshTitleview(){
        for(int i = 0;i<titleViews.length;i++) {
            if(showRow == i){
                titleViews[i].setTextColor(selectColor);
                titleViews[i].setCompoundDrawablesWithIntrinsicBounds(null,null,ContextCompat.getDrawable(ContextHelper.getContext(), R.drawable.charge_title_arrow_select),null);
            }else{
                titleViews[i].setTextColor(normolColor);
                titleViews[i].setCompoundDrawablesWithIntrinsicBounds(null,null,ContextCompat.getDrawable(ContextHelper.getContext(), R.drawable.icon_filterdown),null);
            }
        }
    }

    private void resetTitleview(){
        for(int i = 0;i<titleViews.length;i++) {
            titleViews[i].setTextColor(normolColor);
            titleViews[i].setText(allColums.get(i)[selectFilter.get(i)]);
            titleViews[i].setCompoundDrawablesWithIntrinsicBounds(null,null,ContextCompat.getDrawable(ContextHelper.getContext(), R.drawable.icon_filterdown),null);
        }
    }

}
第二种,通过对象来控制两个选择框的状态和数据填充
public class FilterView extends SkinCompatLinearLayout {
    public final static int searchType = 7309;
    public final static String[] ChargeTitle ={"Speed","Brand"};

    public final static String[] ChargeType ={"All","Slow","Fast"};
    public final static String[] ChargeBrand ={"All","EV Station Pluz","PEA Volta","Elex by EGAT","EA Anywhere","Evolt","Sharge"};

    //power > 50 为快充
    public final static String[] ChargeTypeValue ={"0","0","50"};
    public final static String[] ChargeBrandValue ={"","EV Station Pluz","PEA Volta","Elex by EGAT","EA Anywhere","Evolt","Sharge"};

    private SkinCompatTextView[] titleViews = new SkinCompatTextView[2];

    private final int TYPE_INDEX = 0;
    private final int BRAND_INDEX = 1;

    private SparseArray<String[]> allColums = new SparseArray();
    private SparseArray<String[]> allColumValues = new SparseArray();

    //第几列
    private int showRow = -1;
    private Context mContext;

    private RecyclerView recyclerView;
    private int selectColor = 0xff8ACB04;//R.color.download_cicle_color;
    private int normolColor = 0xff35383D;

    FilterObject[] objs = new FilterObject[2];

    public interface FilterChange{
        void onFilterChange(String chargetype ,String brand);
    }

    FilterChange mFilterChange;

    public void setFilterChange(FilterChange filterChange){
        mFilterChange = filterChange;
    }

    public FilterView(Context context) {
        super(context);
        initdata();
        initView(context);
    }

    private void initdata(){

        allColums.put(TYPE_INDEX,ChargeType);
        allColums.put(BRAND_INDEX,ChargeBrand);

        allColumValues.put(TYPE_INDEX,ChargeTypeValue);
        allColumValues.put(BRAND_INDEX,ChargeBrandValue);
    }

    public FilterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initdata();
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        SkinCompatLinearLayout moreView = (SkinCompatLinearLayout) LayoutInflater.from(mContext).inflate(R.layout.filterlayout, null);
        addView(moreView);
        moreView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        titleViews[TYPE_INDEX] = moreView.findViewById(R.id.filter_left);
        titleViews[BRAND_INDEX] = moreView.findViewById(R.id.filter_right);
        recyclerView = moreView.findViewById(R.id.recyclerView);
        titleViews[TYPE_INDEX].setOnClickListener(onClickListener);
        titleViews[BRAND_INDEX].setOnClickListener(onClickListener);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        objs[BRAND_INDEX] = new FilterObject(ChargeTitle[BRAND_INDEX],ChargeBrand,ChargeBrandValue,titleViews[BRAND_INDEX],0);
        objs[TYPE_INDEX] = new FilterObject(ChargeTitle[TYPE_INDEX],ChargeType,ChargeTypeValue,titleViews[TYPE_INDEX],0);
    }

    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.filter_left){
                showRow = TYPE_INDEX;
            }else if(v.getId() == R.id.filter_right){
                showRow = BRAND_INDEX;
            }
            freshListview();
        }
    };

    private void freshListview(){
        if (recyclerView.getVisibility() == View.VISIBLE) {
            recyclerView.setVisibility(View.GONE);
            return;
        }
        recyclerView.setVisibility(View.VISIBLE);
        recyclerView.setAdapter(new BaseRecyclerAdapter(mContext, Arrays.asList(allColums.get(showRow)),R.layout.charge_filter_view) {
            @Override
            public void convert(MViewHolder holder, int position) {
                ((TextView)holder.getView(R.id.filter_name)).setText(objs[showRow].getItemName(position));
                if(position == objs[showRow].selectIndex){
                    ((TextView)holder.getView(R.id.filter_name)).setTextColor(selectColor);
                    ((TextView)holder.getView(R.id.filter_name)).setCompoundDrawablesWithIntrinsicBounds(null,null,ContextCompat.getDrawable(ContextHelper.getContext(), R.drawable.charge_select),null);
                }else{
                    ((TextView)holder.getView(R.id.filter_name)).setTextColor(normolColor);
                    ((TextView)holder.getView(R.id.filter_name)).setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
                }
            }
        }.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(MViewHolder holder, int position) {

                objs[showRow].setSelectIndex(position);
                objs[showRow].setNormorState();
                recyclerView.setVisibility(View.GONE);

                if (mFilterChange != null) {
                    mFilterChange.onFilterChange(objs[TYPE_INDEX].getSelectValue(),objs[BRAND_INDEX].getSelectValue());
                }
            }
        }));
        objs[showRow].setSelectState();
    }

    // todo use obj instead
    class FilterObject {

       public FilterObject(String title,String[] listShows,String[] listValues,SkinCompatTextView titleView,int selectIndex){
            this.title = title;
            this.listShows = listShows;
            this.listValues = listValues;
            this.titleView = titleView;

        }

        public String title ;

        public  String[] listShows ;

        public  String[] listValues;

        public SkinCompatTextView titleView ;

        public void setSelectIndex(int selectIndex) {
            this.selectIndex = selectIndex;
        }

        public String getSelectValue(){
            return listValues[selectIndex];
        }

        public String getItemName(int index){
            return listShows[index];
        }

        //第几列
        public int selectIndex = 0;

        public void setNormorState(){
            titleView.setTextColor(normolColor);
            titleView.setText(listShows[selectIndex]);
            titleView.setCompoundDrawablesWithIntrinsicBounds(null,null,ContextCompat.getDrawable(ContextHelper.getContext(), R.drawable.icon_filterdown),null);
        }

        public void setSelectState(){
            titleView.setTextColor(selectColor);
            titleView.setText(listShows[selectIndex]);
            titleView.setCompoundDrawablesWithIntrinsicBounds(null,null,ContextCompat.getDrawable(ContextHelper.getContext(), R.drawable.charge_title_arrow_select),null);
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值