Code Monkey home page Code Monkey logo

Comments (5)

luizgrp avatar luizgrp commented on May 28, 2024

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.

from sectionedrecyclerviewadapter.

grunk avatar grunk commented on May 28, 2024

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

from sectionedrecyclerviewadapter.

grunk avatar grunk commented on May 28, 2024

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();
    }
}

from sectionedrecyclerviewadapter.

luizgrp avatar luizgrp commented on May 28, 2024

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!

from sectionedrecyclerviewadapter.

luizgrp avatar luizgrp commented on May 28, 2024

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!

from sectionedrecyclerviewadapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.