Saravanan Selvam
Saravanan Selvam

Reputation: 3

Android Recycler View two Sections

I want to use recycler view with two sections with a simple header. Each section data ie, list content will be coming from two different webservice. I can able to show first section with header. But I have no idea on how to create a second section with different data in the same RecycleView.Adapter .

Can anybody give a suggestion to handle this ?

Upvotes: 0

Views: 2126

Answers (3)

Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42710

I would highly recommended SectionedRecyclerView library.

  1. It supports both header and footer.
  2. It supports different states liked LOADING, LOADED, EMPTY, ... So, while you are waiting respond from webservice, you can easily display a progress bar, by just switching the Section state to LOADING. Once the data is ready, you can switch to LOADED state.
  3. The library comes with extensive unit testing to ensure its code quality.

This is the library, which I use in real-world app.

As you can see, it has header, footer and multiple content rows.

enter image description here

Upvotes: 0

NMP
NMP

Reputation: 480

Or you can use the AdvancedSectionAdapter from HERE. All you need to do is override the following six abstract methods:

public abstract int getGroupCount();

public abstract int getChildCount(int group);

public abstract SectionVH onCreateSectionViewHolder(ViewGroup parent, int viewType);

public abstract ChildVH onCreateChildViewHolder(ViewGroup parent, int viewType);

public abstract void onBindSectionViewHolder(SectionVH holder, int position, List<Object> payloads);

public abstract void onBindChildViewHolder(ChildVH holder, int belongingGroup, int position, List<Object> payloads);

The rest is taken care of for you. You have to specify the number of parent sections getGroupCount, the number of children for parent getChildCount and then you need to create and bind the parent and the children of the parent.

You can create a simple SortedMap of a String, List<> where the String keys are the sections and the List are the children. (Why SortedMap? Because it keeps your keys sorted and not random as HashMap).

Upvotes: 0

Maxime Jallu
Maxime Jallu

Reputation: 2382

Take a look at my library on Github, can be used to easily create sections: RecyclerAdapter & Easy Section

mRecylerView.setLayoutManager(...);
/*create Adapter*/
RecyclerAdapter<Customer> baseAdapter = new RecyclerAdapter<>(...);
/*create sectioned adapter. the Adapter type can be RecyclerView.Adapter*/
SectionedAdapter<String, RecyclerAdapter> adapter = new SectionedAdapter<>(SectionViewHolder.class, baseAdapter);
/*add your sections*/
sectionAdapter.addSection(0/*position*/, "Title Section 1");
/*attach Adapter to RecyclerView*/
mRecylerView.setAdapter(sectionAdapter);

Hope it helps.

Upvotes: 1

Related Questions