如何在Android中隐藏ListView上的筛选弹出窗口?

3
我为一个ListView启用了setTextFilterEnabled文本过滤。 过滤正常工作,但是当我输入时会出现一个显示过滤文本的弹出窗口。请参见以下图片: ListView filter text popup 如何隐藏此弹出窗口或更改其在屏幕上的位置?

1
可能是如何从ListView过滤器中删除弹出文本?的重复问题。 - blahdiblah
4个回答

4
我在这个博客上找到了一种更简单的解决方案,对我很有用:

http://blog.jamesbaca.net/?p=128

当你创建ArrayAdapter来填充ListView时,只需存储它的Filter引用,然后你就可以简单地修改ArrayAdapter上的Filter,而不是ListView

*在Xamarin中,你只需要使用arrayAdapter.InvokeFilter("my text");即可。


3
创建一个过滤器,(您可能会使用不同的适配器)。
ContentAdapter  adapter = new ContentAdapter(this, android.R.layout.simple_list_item_1,
            s);
    list1.setAdapter(adapter);
    Filter f = adapter.getFilter();

然后在'onQueryTextChange'中使用过滤器,而不是注释掉的代码。
public boolean onQueryTextChange(String newText) {
        if (TextUtils.isEmpty(newText)) {

            //list1.clearTextFilter();
            f.filter(null);
        } else {
            //list1.setFilterText(newText.toString());
            f.filter(newText);

        }
        return true;
    }

1

很可能会弹出Filter.publishResults(CharSequence constraint, Filter.FilterResults results)。因此,您可能需要子类化Filter并覆盖它。

最好的问候。


Filter.FilterResults 不可见。我认为不可能重写这个方法。 - Nevin Chen
必须重写 Filter.publishResults(...) 方法来创建一个过滤器,这不是弹出窗口的地方。 - blahdiblah

0

你好,正如Vatsal所提到的那样,你可以从适配器中创建一个过滤器。我解决这个问题的方法是创建了一个ArrayAdapter作为类变量。

// ...
private void setListAdapter() {
    List<Word> words = Dictionary.getInstance(this).getWords();

    arrayAdapter = new ArrayAdapter<>(
            this,
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1,
            words);
    // Sets the List Adapter that we are using
    this.setListAdapter(arrayAdapter);
}
// ...

然后在 onQueryTextChange 或 onQueryTextSubmit 中,您可以通过这个类变量获取过滤器,如下所示:

//...
public boolean onQueryTextChange(String query) {
    // By doing this it removes the popup completely which was arising from using the this.getListView().setFilterText(query) method
    arrayAdapter.getFilter().filter(query);

    return true;
}
//...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接