Skip to content

Instantly share code, notes, and snippets.

@jeffdgr8
Last active August 23, 2023 08:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffdgr8/61a4c6432401bf2740cd17cd04f7ee4e to your computer and use it in GitHub Desktop.
Save jeffdgr8/61a4c6432401bf2740cd17cd04f7ee4e to your computer and use it in GitHub Desktop.
SectionedRecyclerViewAdapter that implements StickyHeaderHandler for using https://github.com/bgogetap/StickyHeaders with https://github.com/luizgrp/SectionedRecyclerViewAdapter
import com.brandongogetap.stickyheaders.exposed.StickyHeader
import com.brandongogetap.stickyheaders.exposed.StickyHeaderHandler
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionedRecyclerViewAdapter
/**
* [SectionedRecyclerViewAdapter] that implements [StickyHeaderHandler].
*/
class StickyHeaderSectionedRecyclerViewAdapter: SectionedRecyclerViewAdapter(), StickyHeaderHandler {
override fun getAdapterData(): List<*> {
val data = ArrayList<StickyHeader?>()
copyOfSectionsMap.values.forEach { section ->
if (section.hasHeader()) {
data.add(StickyHeaderImpl())
}
repeat(section.contentItemsTotal) { data.add(null) }
}
return data
}
class StickyHeaderImpl: StickyHeader
}
@cedb777
Copy link

cedb777 commented Feb 14, 2020

Do you have an example ? Because in my project the "getAdapterData()" is never called.
This is how I use it :

`

            StickyHeaderSectionedRecyclerViewAdapter sectionedAdapter = new StickyHeaderSectionedRecyclerViewAdapter();

            for (int i=0; i<arrSections.size(); i++) {

                Section section  = arrSections.get(i);
                sectionedAdapter.addSection(new ArticlesSection(MainActivity.this, section));

            }

`

And this is my class based on your code :

`

    public class StickyHeaderSectionedRecyclerViewAdapter extends SectionedRecyclerViewAdapter implements StickyHeaderHandler{

        public StickyHeaderSectionedRecyclerViewAdapter() {
        }

        @Override
        public List<?> getAdapterData() {

            ArrayList<StickyHeader> data = new ArrayList<>();

            for(Map.Entry<String, Section> entry : getCopyOfSectionsMap().entrySet()) {

                Section section = entry.getValue();
                if(section.hasHeader()) {
                    data.add(new StickyHeaderImpl());
                }

            }

            return data;

        }


        private class StickyHeaderImpl implements StickyHeader {}
    }

`

Have I had to do something more ?

Thank you.
Have a good day !

@jeffdgr8
Copy link
Author

@cedb777 that all looks correct. How are you setting up your RecyclerView? Besides assigning the StickyHeaderSectionedRecyclerViewAdapter to the adapter, be sure to set the layout manager to StickyLayoutManager, which takes the adapter as a constructor argument. See docs.

@tamhuynhit
Copy link

Thank you a lot, 4 years already but still helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment