如何让自定义视图回复搜索视图?

时间:2016-04-08 00:19:18

标签: java android

通过谷歌google keep看看应用程序之后,我立即对应用搜索的确切运作方式感到好奇。

以下是该应用的图片: enter image description here

每当您搜索某些内容时,不匹配的卡片就会消失。他们究竟是如何实现这一目标的,因为我认为该卡基本上是自定义视图,而布局是自定义布局。我的问题是我不知道如何实现搜索自定义视图。

注意:我已经在网上查看了一些互联网搜索视图示例,我只能找到listview搜索的实现,这不是我想要的。

1 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,所以我将自定义视图属性(例如,名称,标签,ID等)制作成一个由一个空格隔开的字符串" "

我的CustomView类中有类似的东西:

public String toString() {
    return (this.name + " " + this.tag + " " + this.coords + " " + this.id );
}

然后我通过显示的所有当前自定义视图过滤了我的自定义视图:

ArrayList<CustomView> filteredList = new ArrayList<>();
...

private void filter(String constraint) {
    for(CustomView view : AllViews) {
        if (!filteredList.contains(view))
            if (view.toString().toLowerCase().contains(constraint.toLowerCase())) {
                filteredList.add(view);
                break;
            }
        }
    }
    // Do something to add the filteredList to your adapter
    // and show the new list of CustomViews.
}

您可以从SearchView OnQueryTextListener()调用onTextTextChange(String newText)中的filter(newText)方法。

这样,如果在任何CustomView中的任何地方找到您在SearchView中键入的任何单词,则会显示相应的CustomView,否则视图将会消失&#34;。

希望这有帮助。