You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 2, 2021. It is now read-only.
1. Your POJO classes should extend/implement some Base classes. This forces us to make changes in model level.
35
+
2. Forces you to implement some boilerplate code - like managing view types by yourself.
36
+
3. Doesn't utilise diffutil or payloads from recyclerview api
37
37
38
-
If you Watch this repository, GitHub will send you an email every time there is an update.
38
+
Now the advantages of the MultiViewAdapter
39
39
40
-
---
40
+
1. No restrictions on POJO class' parent/hierarchy. Now, your model classes can truly reside inside data layer.
41
+
2. No need to cast your models, no need for switch/if-else cases when you are having multiple view types.
42
+
3. Takes advantage of diffutil and allows payload while notifying adapter.
41
43
44
+
## Key concepts
42
45
43
-
# Why this library?
46
+
1. RecyclerAdapter - This is the adapter class. It can have multiple ItemBinder and DataManagers. It extends from official RecyclerView.Adapter
47
+
2. ItemBinder - ItemBinder's responsibility is to create and bind viewholders. ItemBinder has type parameter which accepts the model class need to be displayed. ItemBinder needs to be registered inside RecyclerAdapter. ItemBinder can be registered with multiple adapters.
48
+
3. DataManger - Consider it as a List<E>. But internally it calls necessary animations when the list is modified. There are two DataManagers. <b>DataListManager</b> for list of items. <b>DataItemManager</b> for a single item (Header, Footer etc.,).
44
49
45
-
Most of the android apps out there uses recyclerview to display content. As with any other system-level api, recyclerview api is also designed in a generic way. So it needs lot of boilerplate code to be written.
50
+
## Features
46
51
47
-
If the app contains multiple view types, the boilerplate code doubles. MultiViewAdapter library helps you in removing this bolierplate code while allowing you to truly re-use the viewholder code across multiple adapters.
52
+
1. Supports different span count for different ItemBinders.
53
+
2. Adds different ItemDecoration for different ItemBinders.
54
+
3. Items can be selected - Single and Multiple selection options are available.
55
+
4. Out of the box support for DiffUtil.
48
56
49
-
There are many other libraries, which provides the same feature. But they do enforce the either or all of the following constraints :
57
+
## Usage
58
+
To get some idea about the MultiViewAdapter features kindly look at sample app code.
50
59
51
-
1. Your POJO classes should extend/implement some Base classes. This forces us to make changes in model level, sometimes this is not acceptable.
52
-
2. Forces you to implement some boilerplate code - like managing view types by yourself. (This library too have one redundant method. More about this later)
53
-
3. Doesn't utilise diffutil or payloads from recyclerview api
60
+
### Simple adapters
61
+
Let us display list of cars. No fuss. Here is the entire code.
The MultiViewAdapter itself has own cons. As mentioned earlier, you need to override one boilerplate method to differentiate different models used inside same adapter. We did the best to reduce the boilerplate methods except this one.
You can not have a different viewholder for the same model inside one adapter. For example, if the adapter shows list of 'Car', then 'Car' can have only one viewholder
Now you are good to go. Just create the CarAdapter object and set it to your recyclerview. When addData() method is called it will show the items in recyclerview.
107
+
<br/>
108
+
If you want to show multiple viewtypes just create multiple ItemBinders and register inside the adapter.
109
+
110
+
### For different span count in GridLayoutManager
111
+
If the GridLayoutManager has different span count for different view types, then override the getSpanSize() method inside ItemBinder.
112
+
113
+
```
114
+
115
+
@Override public int getSpanSize(int maxSpanCount) {
116
+
return 1; // Return any number which is less than maxSpanCount
117
+
}
118
+
119
+
```
120
+
121
+
Also don't forget to set span size lookup in GridLayoutManager. Adapter has default span size lookup object. Use that object.
Just extend your adapter from SelectableAdapter instead of RecyclerAdapter. Now the adapter is selectable.
171
+
To make an ItemBinder as selectable, extend it from SelectableBinder and also extend ViewHolder from SelectableViewHolder.
172
+
By default, on long press ViewHolder will be selectable if it extends from SelectableViewHolder.
173
+
You can also call `itemSelectionToggled()` to make it selected by yourself. Kindly go through the sample repo implementation.
174
+
<br/>
175
+
Finally, you can call `DataListManager`'s `getSelectedItems()` and `setSelectedItems(List<E> selectedItems)` to get and set selected items respectively.
176
+
177
+
### Using DiffUtil and Payload
178
+
DataListManager and DataItemManager will take care of diffutil. There is no special code needed. But to enable the payloads, you have to create custom DataManager and override `areContensSame(Object, Object)` and `getChangePayload(Object, Object)`.
returnsuper.getChangePayload(oldItem, newItem); // Own impl
194
+
}
195
+
}
196
+
```
197
+
198
+
## Roadmap
199
+
I am actively working on expanding the feature set of this library. While i don't have a exact timeline, but here are the future plans.
200
+
1. Add support for StaggeredGrid layout manager
201
+
2. Move diffutil calculation to background thread
202
+
3. Adding support for swipe listeners with composability as priority
203
+
4. Improve the sample app code and api documentation
204
+
205
+
206
+
## Changelog
207
+
See the project's Releases page for a list of versions with their changelog. [View Releases](https://github.com/DevAhamed/MultiViewAdapter/releases)<br/>
208
+
If you watch this repository, GitHub will send you an email every time there is an update.
209
+
210
+
211
+
## Contribution
212
+
Contributing to this library is simple,
213
+
1. Clone the repo
214
+
2. Make the changes
215
+
3. Make a pull request to develop branch
216
+
<br/><br/>The only requirement is whatever changes you make should be backward compatible. Also make sure its not a too specific feature which maynot be useful for everyone.
217
+
Kindly make sure your code is formatted with this codestyle - [Square Java code style](https://github.com/square/java-code-styles)
218
+
219
+
220
+
## Alternatives
221
+
This library may not suit your needs or imposes too many restrictions. In that case create an issue/feature request. In the mean time check these awesome alternatives as well.
222
+
1.[MultipleViewTypesAdapter](https://github.com/yqritc/RecyclerView-MultipleViewTypesAdapter) - Original inspiration for this library. By inspiration, we mean that parts of code were "re-used"<br/>
0 commit comments