luizgrp / SectionedRecyclerViewAdapter

An Adapter that allows a RecyclerView to be split into Sections with headers and/or footers. Each Section can have its state controlled individually.
MIT License
1.68k stars 372 forks source link

Support for searchview #2

Closed grunk closed 8 years ago

grunk commented 8 years ago

Hi, nice work with your adapter , light and easy to use , that's pretty rare ;)

Is there any searchview support available ? If no, should i just extends SectionedRecyclerViewAdapter to implement it ? I currently have a searchable adapter (implemented following this : http://stackoverflow.com/a/30429439/393984) but it need sections :)

luizgrp commented 8 years ago

Hi @grunk,

Thanks for your words, glad to hear that it's been useful to someone :)

I don't think you need to extend SectionedRecyclerViewAdapter, just set is as the RecyclerView adapter as usual.

You should create your sections with a method to filter the data. Make sure to call setVisible(false) if there is no data to be displayed for the section after the filter is applied.

Then in onQueryTextChange you should call the filter method for all the sections.

Let me know if you need any further clarification.

grunk commented 8 years ago

Thanks i will try implementing a filter directly in the sections. I let you know ;)

grunk commented 8 years ago

Ok , for those who are interested it's how i implemented it. It's far from an optimal solution but it works.

In the activity/fragment , implements SearchView.OnQueryTextListener Then filter each section :

@Override
public boolean onQueryTextChange(String query) {

    for(ServerSection sec : mSections) {
        final List<String> filteredItem = filter(sec.getOriginalList(),query);
        sec.update(filteredItem);
    }
    return true;
}

private List<String> filter(List<String> items, String query) {

    query = query.toLowerCase();

    if(query.isEmpty())
        return items;

    final List<String> filteredItemList = new ArrayList<>();
    for (String item : items) {
        final String text = item.toLowerCase();
        if (text.contains(query)) {
            filteredItemList.add(item);
        }
    }
    return filteredItemList;
}

In each section i keep the original list of item and a filtered list. Each search update the filtered list of each section :

class ServerSection extends StatelessSection {
    String mTitle;
    List<String> mList;
    List<String> mOriginalList;

    public ServerSection(String title, List<String> list)
    {
        super(R.layout.rv_header,R.layout.rv_srvlist);
        mTitle = title;
        mOriginalList = new ArrayList<>(list);
        mList = mOriginalList;
    }

    public List<String> getOriginalList()
    {
        return mOriginalList;
    }

    public void update(List<String> items)
    {
        mList = items;
        mSectionnedAdapter.notifyDataSetChanged();
    }
}
luizgrp commented 8 years ago

Great work grunk, I will give some thought to it and see if it can be added to the library. Will keep you posted, thanks!

luizgrp commented 8 years ago

hey, I updated the library (version 1.0.4) with a method to retrieve the sections of the adapter (getSectionsMap). I also added a full example of a SearchView with Sections to the demo app. thanks again for your suggestion and for sharing your code!