工具栏与搜索查看临时过滤RecyclerView [英] Toolbar with SearchView temporary filtering RecyclerView

查看:1708
本文介绍了工具栏与搜索查看临时过滤RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现它使用的工具栏,SlidingTabLayout和ViewPager持有片段我的Andr​​oid应用程序内搜索功能。每个片段里面有与项目列表的RecyclerView。

在单独的类(DataAccess.java)

RecyclerView数据是静态的定义,这些名单只是通过调用更新和RecyclerView被刷新(没有通过新的数据)

  mRecyclerView.getAdapter()notifyDataSetChanged()。

有没有简单的方法来临时过滤RecyclerView不改变数据和用户presses里面后返回的工具栏按钮来删除该过滤器,并显示inital列表。

在工具栏菜单内pressing搜索图标:

因此​​,当用户键入约瑟普......过滤器将被激活。

和他$ P $中搜索查看psses的X按钮后,用户将像以前一样得到同样的数据,而无需过滤器。

  @覆盖
公共布尔onQueryTextChange(字符串newText){
    //过滤数据(临时remove从DataAccess.list不这样做.startsWith(newText的所有项目)
}@覆盖
公共布尔onQueryTextSubmit(查询字符串)
    //没有帮助,如果我在这里恢复已删除项目
}


解决方案

  @覆盖
公共布尔onQueryTextSubmit(查询字符串){
    ((ItemAdapter)myRecList.getAdapter())。使用setfilter(查询)
}


 公共类ItemAdapter扩展RecyclerView.Adapter< ItemAdapter.ViewHolder> {    私人列表<串GT; visibleObjects;
    私人列表<串GT; allObjects;    .....    公共无效flushFilter(){
        visibleObjects =新的ArrayList<>();
        visibleObjects.addAll(allObjects);
        notifyDataSetChanged();
    }    公共无效使用setfilter(字符串QUERYTEXT){        visibleObjects =新的ArrayList<>();
        约束= constraint.toString()与toLowerCase()。
        对于(字符串项:allObjects){
                如果(item.toLowerCase()。包括(QUERYTEXT))
                    visibleObjects.add(项目);
        }
       notifyDataSetChanged();
    }
}

I need to implement search functionality inside my android app which uses toolbar, SlidingTabLayout and ViewPager that holds fragments. Inside each fragment there is a RecyclerView with list of items.

RecyclerView data is static defined in separate class (DataAccess.java) and those lists are updated and RecyclerView gets refreshed just by calling (without passing new data)

mRecyclerView.getAdapter().notifyDataSetChanged();

Is there any simple way to temporary filter RecyclerView without changing the data and after the user presses return button inside Toolbar to remove the filter and show inital list.

Before pressing Search icon inside toolbar menu:

So when the user is typing "Josip..." the filter will be active

and after he presses the X button in SearchView the user will get the same data as before without filter.

@Override 
public boolean onQueryTextChange(String newText) {
    // filter data (temporary remove all items from DataAccess.list that don't .startsWith(newText)
}

@Override 
public boolean onQueryTextSubmit(String query)
    // Doesn't help if I revert deleted items here
}

解决方案

@Override 
public boolean onQueryTextSubmit(String query){
    ((ItemAdapter) myRecList.getAdapter()).setFilter(query)
}


public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {

    private List<String> visibleObjects;
    private List<String> allObjects;

    .....

    public void flushFilter(){
        visibleObjects=new ArrayList<>();
        visibleObjects.addAll(allObjects);
        notifyDataSetChanged();
    }

    public void setFilter(String queryText) {

        visibleObjects = new ArrayList<>();
        constraint = constraint.toString().toLowerCase();
        for (String item: allObjects) {            
                if (item.toLowerCase().contains(queryText))                     
                    visibleObjects.add(item);            
        }
       notifyDataSetChanged();
    }
}

这篇关于工具栏与搜索查看临时过滤RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆