Reputation: 3
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
Reputation: 42710
I would highly recommended SectionedRecyclerView library.
This is the library, which I use in real-world app.
As you can see, it has header, footer and multiple content rows.
Upvotes: 0
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
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