Add support for multiple view holders in a given section
Would it be possible to implement something like RecyclerView.Adapter's public int getItemViewType(int position) to allow one to define logic for loading different view holders in a given section?
My problem is the same.@luizgrp will you please add getItemViewType(int position) for multiple view holders.
Hey, it sounds good, I will have look over the weekend. If you guys want to submit a pull request you are more than welcome. cheers
@luizgrp Thanks for your works.. I want this option to develop my app. Add different Section Header View Holder to view different section header design. Waiting for your awesome reply.
addSection overrides the previous section, rather than appending? @luizgrp #11
Hey @luizgrp , what did you conclude about this? Possible to implement? If so, and you're too busy, point me in the right direction and I can work up the code and create a pull request.
In reference to Pull Request #57
Use Case
There are many use cases for this. Sometimes, within a Section, we want to display a header, some text, maybe a photo, some more text, a map, and then maybe even an advertisement. All of these require vastly different ViewHolders.
Design
Modified/Created Methods:
- Created: Section.getItemViewType(int position), lets the user specify the ViewType depending on the position of the item.
- Created: Getter methods to retrieve list of CustomViewTypes added to the Adapter (or Section)
- Modified: SectionedRecyclerViewAdapter.onCreateViewHolder(parent, viewType), will check if the viewType matches one of the CustomViewTypes and return the applicable ViewHolder.
- Created: Test methods
Created Classes:
- Created: CustomViewType, stores the ViewHolder.class and the layout resource to inflate
- Created: Example9Fragment to demonstrate functionality
User lib perspective
Some users of the library really want this, as shown above. Others wouldn't want to be bothered by it, which is why this feature should be added in such a way that it does not necessitate any work if the user wants to continue using a single Item ViewHolder.
Hi @percula,
Let's discuss the Use Case. I understand the need of having different view types. The native RecyclerView adapter allows you to have many different view types. But this library's goal is to help who needs to group repeatable items into sections.
If your requirement is to display a header, some text, a photo, a map, an ad, etc, the first question is, why would you use a RecyclerView? You won't be recycling any view if they are all different. Why not add everything in a ScrollView?
But let's say you have a list of repeatable items that you want to group in a section and you have another section with many different view types as you said. Then ok, it starts to make sense to use this library. So in order to make it happen you can still use the library the way it is, you can create one section with your repeatable items, and another section, with one single item, which contains all the views you just said in one single layout file. But let's say you don't like to have all the views in one single layout, maybe you want to be able to control them individually. Fine, you can still use the library the way it is, create one section for every single view.
Let's discuss another example: you have a list of repeatable items, but eventually you want to display an ad in the middle of that list. You can still use this lib the way it is. You can add the ad view inside the items view, with visibility false. When you want to display the ad you switch the visibility of the ad view to true and the visibility of the item content to false. This was done in #45.
Let me know your thoughts.
@luizgrp , you make some good points.
The first alternative that you mention, using a ScrollView, doesn't work very well for large amounts of items, as it doesn't have the "recycle" feature. It also doesn't have many of the nice features of RecyclerView such as add/delete/rearrange animations.
The second alternative you mentioned, creating a new section for each item with a different ViewType, would definitely work for some use cases. The main drawback to this solution is that you would not be able to utilize some of the main features of this library, such as hiding/showing an entire section, For example, even if you had a group of items with different ViewTypes arranged together, without headers/footers, as a DIY "Section", they would still technically be different Sections. Thus you couldn't utilize Section features such as showing load state, empty state, etc.
The third option, having all the views within one layout and turning their visiblity on/off, would work but seems very hacky. For simple layout, such as a list item and an ad, it would be fine. But if you have many different layouts, it gets messy really quickly.
Perhaps the main reason to add this functionality is simply because the native RecyclerView adapter has it. I believe users expect to be able to use all the features of the native adapter with the addition of the nice Section features this library provides. The code I submitted does more than just provide this functionality, so at the very least I believe this library should add a getItemViewHolder() class for users to override if they choose. This would put back in the functionality from the native adapter.
Hello. Here is the simple example to have different cells in the RecyclerViewAdapter. Take a look at the screenshot, please. In iOS i can easily use different cells layout for each section and row index.

In my example it's required to have different cell layout in the first section.
For now i solved this by changing visibility of some TextViews in the view holder in onBindViewHolder, but it looks ugly an inefficient.
Any news on this? I have tried solution that @percula have created, but not very successful.
I don't think I have capacity to introduce and maintain this feature in this library, if @percula wants to build and publish another library based on this one with this feature, I'm happy to reference it in this repo. this seems to be a very popular request
I forked and merged the current status of @percula's fork into the current development branch, making this feature available in the latest development version of this library.
All tests run through, however, there are no specific tests for this feature. I am using it in my own app and haven't noticed any problems so far.
Everyone is welcome to use my fork (see the Readme on how to add it as git submodule), and if possible without much effort, I will incorporate updates from this main repository, but no guarantees.
For usage see the new example, I might add instructions later. There are also some improvements I can think of but at the moment I don't have the time to implement them.
Right now i found a solution by creating a Custom Sectioned Adapter that overrides the fun onCreateViewHolder(parent: ViewGroup, viewType: Int).
E.G.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
if (layoutMode == GRID || (viewType <= VIEW_TYPE_QTY && viewType != VIEW_TYPE_ITEM_LOADED) || (viewType >= VIEW_TYPE_QTY && (viewType % VIEW_TYPE_QTY) == 0)
return super.onCreateViewHolder(parent, viewType) //DEFAULT CREATES THE GRID VIEW HOLDER FOR ITEMS, THATS WHY RETURNS THE SUPER METHOD. IN CASE OF HEADERS, FOOTERS ETC...ALWAYS WANT TO RETURN SUPER.
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_test_linear, parent, false)
return NonGridViewHolder(view)
}
@JonathanImperato please see if this feature introduced in version 1.2.0 helps you: https://github.com/luizgrp/SectionedRecyclerViewAdapter/issues/98
@JonathanImperato please see if this feature introduced in version 1.2.0 helps you: #98
Can I use it with AndroidX?
Yes
@JonathanImperato please see if this feature introduced in version 1.2.0 helps you: #98
Can't see what should help me.
I forked and merged the current status of @percula's fork into the current development branch, making this feature available in the latest development version of this library.
All tests run through, however, there are no specific tests for this feature. I am using it in my own app and haven't noticed any problems so far.
Everyone is welcome to use my fork (see the Readme on how to add it as git submodule), and if possible without much effort, I will incorporate updates from this main repository, but no guarantees.
For usage see the new example, I might add instructions later. There are also some improvements I can think of but at the moment I don't have the time to implement them.
hello @felixwiemuth when i try to add the submodule i 'am facing this bug Plugin with id 'findbugs' not found.have you any idea about this please ?
hello @felixwiemuth when i try to add the submodule i 'am facing this bug Plugin with id 'findbugs' not found.have you any idea about this please ?
Hi @khalilsam, the findbugs plugin seems to be used by the static analysis in library/build_files/static-code-analysis.gradle. Either try to make sure the plugin is available, or disable the findbugs gradle task which is declared in that file.
@khalilsam Note that Gradle 6 deprecates the findbugs plugin (see https://stackoverflow.com/questions/58985945/resolve-findbugs-issue-in-gradle). Therefore, if you use a newer gradle version, it won't work anymore. With the current version used on my fork (3.3.2) it works.