自定义菜单的实现

  • 内容
  • 评论
  • 相关

gradle 引入

compile 'com.github.fg2q1q3q:DropDownMenu:1.1.1'
dorpmenu(修改过的部分类,增加了GirdLayout 的动态添加)
    private String headers[] = {"综合排序", "商品类别", "价格筛选"};
    private int[] types = new int[]{MyDropDownMenu.TYPE_LIST_CITY, MyDropDownMenu.TYPE_GRID, MyDropDownMenu.TYPE_CUSTOM};
    private String methods[] = {"综合排序", "按价格升序", "按价格降序", "按销量降序"};
    private String ages[] = {"更新时间", "最新更新", "综合排序"};
    private String constellations[] = {"全部类别"};
    //contentView 为下拉菜单的view
   contentView = getLayoutInflater().inflate(R.layout.contentview, null);
   dropDownMenu=(MyDropDownMenu) findViewById(R.id.dropDownMenu);
  dropDownMenu.addMenuSelectListener(new MyDropDownMenu.OnDefultMenuSelectListener() {
            @Override
            public void onSelectDefaultMenu(int index, int pos, String clickstr) {
                //index:点击的tab索引,pos:单项菜单中点击的位置索引,clickstr:点击位置的字符串
                //排序
                if (index == 0) {
                    if (pos == 0) {
                        type = "0";
                    } else if (pos == 1) {
                        type = "1";
                    } else if (pos == 2) {
                        type = "2";
                    } else if (pos == 3) {
                        type = "3";
                    } else {
                        type = "0";
                    }
                    page = 1;
                } 
            }
        });
 dropDownMenu.setDropDownMenu(Arrays.asList(headers), initViewData(), contentView);

 

    /**
     * 设置类型和数据源:
     * DropDownMenu.KEY对应类型(DropDownMenu中的常量,参考上述核心源码) 此处的MyDropDownMenu是上面下载的自定义的MyDropDownMenu
     * DropDownMenu.VALUE对应数据源:key不是TYPE_CUSTOM则传递string[],key是TYPE_CUSTOM类型则传递对应view
     */
    private List<HashMap<String, Object>> initViewData() {
        List<HashMap<String, Object>> viewDatas = new ArrayList<>();
        HashMap<String, Object> map;
        for (int i = 0; i < headers.length; i++) {
            map = new HashMap<String, Object>();
            map.put(MyDropDownMenu.KEY, types[i]);
            switch (types[i]) {
                case MyDropDownMenu.TYPE_LIST_CITY:
                    map.put(MyDropDownMenu.VALUE, methods);
                    map.put(MyDropDownMenu.SELECT_POSITION, 0);
                    break;
                case MyDropDownMenu.TYPE_LIST_SIMPLE:
                    map.put(MyDropDownMenu.VALUE, ages);
                    map.put(MyDropDownMenu.SELECT_POSITION, 0);
                    break;
                case MyDropDownMenu.TYPE_GRID:
                    map.put(MyDropDownMenu.VALUE, constellations);
                    break;
                default:
                    map.put(MyDropDownMenu.VALUE, getCustomView());
                    break;
            }
            viewDatas.add(map);
        }
        return viewDatas;
    }

 

    private View getCustomView() {
        View v = getLayoutInflater().inflate(R.layout.custom, null);
        return v;
    }

针对Girdlayout 可以调用refreshGridItem来动态添加 GirdLayout 的内容。

 

评论

0条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注