Android RecyclerView通过我的EditText搜索过滤
创始人
2024-08-15 07:00:32
0

要实现RecyclerView的搜索过滤功能,可以按照以下步骤进行操作:

  1. 在布局文件中,添加一个EditText用于输入搜索关键字:

  1. 在RecyclerView的Adapter中创建一个新的List用于保存原始数据源,以便进行搜索过滤:
private List originalList;
private List filteredList;
  1. 在Adapter的构造函数中,将原始数据源复制到originalList中,并将filteredList初始化为originalList的副本:
public MyAdapter(List itemList) {
    this.originalList = new ArrayList<>(itemList);
    this.filteredList = new ArrayList<>(itemList);
}
  1. 在Adapter中添加一个方法用于执行搜索过滤操作,该方法将根据搜索关键字更新filteredList:
public void filter(String text) {
    filteredList.clear();
    if (text.isEmpty()) {
        filteredList.addAll(originalList);
    } else {
        text = text.toLowerCase();
        for (Item item : originalList) {
            if (item.getName().toLowerCase().contains(text)) {
                filteredList.add(item);
            }
        }
    }
    notifyDataSetChanged();
}
  1. 在Activity或Fragment中,获取EditText的引用,并添加一个TextWatcher监听器,当搜索关键字发生变化时调用Adapter的filter方法:
EditText editTextSearch = findViewById(R.id.editTextSearch);
editTextSearch.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        // Do nothing
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        myAdapter.filter(charSequence.toString());
    }

    @Override
    public void afterTextChanged(Editable editable) {
        // Do nothing
    }
});
  1. 在Activity或Fragment中,创建RecyclerView的Adapter并设置给RecyclerView:
RecyclerView recyclerView = findViewById(R.id.recyclerView);
MyAdapter myAdapter = new MyAdapter(itemList);
recyclerView.setAdapter(myAdapter);

现在,当用户在EditText中输入搜索关键字时,RecyclerView会自动更新以显示匹配的项。

相关内容

热门资讯

推荐十款!wepoker透视脚... 推荐十款!wepoker透视脚本免费下载pc(辅助挂)都是有挂(有挂方法辅助方法)1、下载好脚本下载...
玩家亲测!hhpoker买挂(... 玩家亲测!hhpoker买挂(辅助挂)果然有挂(有挂方针辅助工具)能透视中分为三种模型:模拟器、私人...
一分钟教你!aapoker透视... 一分钟教你!aapoker透视方法(辅助挂)本来是真的有挂(的确有挂辅助app)亲,关键说明,透视脚...
我来教教大家!wepoker作... 我来教教大家!wepoker作弊方法(辅助挂)确实有挂(有挂秘诀辅助教程)1、该软件可以轻松地帮助玩...
每日必备!wepoker辅助是... 每日必备!wepoker辅助是真的假的(辅助挂)都是真的是有挂(有挂助手辅助教程)1、不需要AI权限...
一分钟了解!wepoker免费... 一分钟了解!wepoker免费脚本咨询(辅助挂)一贯真的是有挂(有挂秘诀辅助工具)一分钟了解!wep...
发现一款!wepoker辅助透... 发现一款!wepoker辅助透视软件(辅助挂)果然有挂(有挂技巧辅助工具)脚本下载中分为三种模型:挂...
总算清楚!pokermaste... 总算清楚!pokermaster修改器(辅助挂)其实是有挂(证实有挂辅助app)1、起透看视 辅助软...
盘点几款!wepoker辅助真... 盘点几款!wepoker辅助真的假的(辅助挂)竟然真的是有挂(有挂解密辅助技巧)一、游戏安装教程牌型...
记者揭秘!wepoker透视有... 记者揭秘!wepoker透视有吗(辅助挂)总是有挂(有挂方法辅助教程)1、这是跨平台的轻量版有透视,...