前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SearchView文字与RcyclerView进行动态搜索

SearchView文字与RcyclerView进行动态搜索

作者头像
圆号本昊
发布2021-09-24 12:21:21
5730
发布2021-09-24 12:21:21
举报
文章被收录于专栏:github@hornhuang

很多事后我们需要用到SearchView文字与RcyclerView进行动态匹配,比如说歌词的关键字索引,列表查询等 那么这里就介绍一种常用的方案:


具体运行效果:


项目Demo

https://github.com/FishInWater-1999/android-SignInSystem


为 Searchview 添加 setOnQueryTextListener 实时监听内容变化。

代码语言:javascript
复制
/*
SearchView 文字变化 动态匹配
 */
private void iniSearch(){
    setTextColor();

    mSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            List mList = ListContentMate.mate(mUserTotalList, mSearch.getQuery().toString());
            userList.clear();
            userList.addAll(mList);
            recyclerAdapter.notifyDataSetChanged();
            return false;
        }
    });
}

注:ListContentMate 是我自定义的类,内容如下


使用 contains() 方法,与 SearchView 中输入的内容,进行字符串比较

留下有子段段中有搜索内容的 List

代码语言:javascript
复制
/**该类由于将 list 与 字符串进行配对
 * 检索出符合条件的 List
 * @author fishinwater
 */
public class ListContentMate {

    public static List mate(List mList, String fullName){
        List newList = new ArrayList<>();
        for (int i = 0 ; i < mList.size() ; i++){
            if (mList.get(i).getFullname().contains(fullName)){
                newList.add(mList.get(i));
            }
        }
        return newList;
    }

}

调用 adapter 的 notifyDataSetChanged 方法,重新配置 adapter ,以达到更新 RecyclerView 内容的效果:

在给出的第一块代码中的这三行:

代码语言:javascript
复制
userList.clear();
userList.addAll(mList);
recyclerAdapter.notifyDataSetChanged();

结束,欢迎关注我获得跟多小姿势~~

关于 recyclerView 的基本使用:https://blog.csdn.net/qq_43377749/article/details/89813275

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/05/07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 具体运行效果:
  • 项目Demo
    • 为 Searchview 添加 setOnQueryTextListener 实时监听内容变化。
      • 使用 contains() 方法,与 SearchView 中输入的内容,进行字符串比较
        • 留下有子段段中有搜索内容的 List
          • 调用 adapter 的 notifyDataSetChanged 方法,重新配置 adapter ,以达到更新 RecyclerView 内容的效果:
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档