SectionedRecyclerViewAdapter icon indicating copy to clipboard operation
SectionedRecyclerViewAdapter copied to clipboard

Add support for multiple view holders in a given section

Open kevinwo opened this issue 10 years ago • 21 comments

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?

kevinwo avatar Jul 16 '16 07:07 kevinwo

My problem is the same.@luizgrp will you please add getItemViewType(int position) for multiple view holders.

sandeshnaroju avatar Jul 20 '16 08:07 sandeshnaroju

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 avatar Jul 20 '16 10:07 luizgrp

@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.

shihabmi7 avatar Aug 03 '16 06:08 shihabmi7

addSection overrides the previous section, rather than appending? @luizgrp #11

dduvedi avatar Sep 07 '16 16:09 dduvedi

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.

percula avatar Jul 03 '17 19:07 percula

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.

percula avatar Jul 10 '17 15:07 percula

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 avatar Jul 14 '17 09:07 luizgrp

@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.

percula avatar Jul 15 '17 01:07 percula

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.

screen shot 2017-10-22 at 17 02 58

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.

roma86 avatar Oct 22 '17 14:10 roma86

Any news on this? I have tried solution that @percula have created, but not very successful.

droid-wise avatar Dec 22 '17 02:12 droid-wise

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

luizgrp avatar Jan 27 '19 14:01 luizgrp

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.

felixwiemuth avatar Feb 12 '19 14:02 felixwiemuth

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 avatar Nov 06 '19 16:11 JonathanImperato

@JonathanImperato please see if this feature introduced in version 1.2.0 helps you: https://github.com/luizgrp/SectionedRecyclerViewAdapter/issues/98

luizgrp avatar Nov 06 '19 17:11 luizgrp

@JonathanImperato please see if this feature introduced in version 1.2.0 helps you: #98

Can I use it with AndroidX?

JonathanImperato avatar Nov 06 '19 22:11 JonathanImperato

Yes

luizgrp avatar Nov 07 '19 10:11 luizgrp

@JonathanImperato please see if this feature introduced in version 1.2.0 helps you: #98

Can't see what should help me.

JonathanImperato avatar Nov 11 '19 09:11 JonathanImperato

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 ?

khalilsam avatar Sep 04 '20 16:09 khalilsam

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.

felixwiemuth avatar Sep 04 '20 17:09 felixwiemuth

@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.

felixwiemuth avatar Sep 11 '20 15:09 felixwiemuth