安卓高仿京东淘宝的筛选功能,全网最简单方便

最近需要实现一个筛选的功能。但是在网上找了半天都写得特别乱,要不就是收费,今天做一个。实现效果图如下。

安卓高仿京东淘宝的筛选功能,全网最简单方便_第1张图片

 

 

点击按钮直接弹出。可以放在任何控件下面。下面是使用代码:

 

首先需要添加依赖:

在build.gradle中添加:

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

在app。gradle中添加:

dependencies {
	        implementation 'com.github.zhujiang521:Filter:Tag'
	}

 

添加好依赖之后直接初始化控件即可开始使用,下面是添加数据及使用方法(文末附有项目的Github):

private void initView() {
        tvFlow = findViewById(R.id.tv_flow);
        tvFlow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                flowPopWindow = new FlowPopWindow(MainActivity.this, dictList);
                flowPopWindow.showAsDropDown(tvFlow);
                flowPopWindow.setOnConfirmClickListener(new FlowPopWindow.OnConfirmClickListener() {
                    @Override
                    public void onConfirmClick() {
                        StringBuilder sb = new StringBuilder();
                        for (FiltrateBean fb : dictList) {
                            List cdList = fb.getChildren();
                            for (int x = 0; x < cdList.size(); x++) {
                                FiltrateBean.Children children = cdList.get(x);
                                if (children.isSelected())
                                    sb.append(fb.getTypeName() + ":" + children.getValue() + ";");
                            }
                        }
                        if (!TextUtils.isEmpty(sb.toString()))
                            Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
                    }
                });

            }
        });

    }


     /**
     * 添加筛选框的数据
     */
    private void initParam() {
        String[] sexs = {"本地", "在线"};
        String[] colors = {"新建", "原有", "改造"};
        String[] company = {"交流20kv", "交流10kv", "交流5kv"};

        FiltrateBean fb1 = new FiltrateBean();
        fb1.setTypeName("获取方式");
        List childrenList = new ArrayList<>();
        for (String sex : sexs) {
            FiltrateBean.Children cd = new FiltrateBean.Children();
            cd.setValue(sex);
            childrenList.add(cd);
        }
        fb1.setChildren(childrenList);

        FiltrateBean fb2 = new FiltrateBean();
        fb2.setTypeName("建设类型");
        List childrenList2 = new ArrayList<>();
        for (String color : colors) {
            FiltrateBean.Children cd = new FiltrateBean.Children();
            cd.setValue(color);
            childrenList2.add(cd);
        }
        fb2.setChildren(childrenList2);

        FiltrateBean fb3 = new FiltrateBean();
        fb3.setTypeName("电压等级");
        List childrenList3 = new ArrayList<>();
        for (String aCompany : company) {
            FiltrateBean.Children cd = new FiltrateBean.Children();
            cd.setValue(aCompany);
            childrenList3.add(cd);
        }
        fb3.setChildren(childrenList3);

        dictList.add(fb1);
        dictList.add(fb2);
        dictList.add(fb3);
    }

GitHub

你可能感兴趣的:(安卓进阶之旅)