Skip to content
This repository was archived by the owner on Jan 2, 2021. It is now read-only.

Commit fa54feb

Browse files
committed
Merge branch 'release/v0.9.0'
2 parents 7d0c6bd + e1ae6eb commit fa54feb

34 files changed

Lines changed: 1257 additions & 315 deletions

README.md

Lines changed: 187 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33
[ ![Core](https://api.bintray.com/packages/devahamed/MultiViewAdapter/multi-view-adapter/images/download.svg) ](https://bintray.com/devahamed/MultiViewAdapter/multi-view-adapter/_latestVersion)
44
[![GitHub license](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://github.com/DevAhamed/MultiViewAdapter/blob/master/LICENSE)
55

6-
------
76

8-
# Sample Project
7+
Helper library for recyclerviews to create composable view holders without boilerplate code.
98

10-
You can download the latest sample APK from this repo here: https://github.com/DevAhamed/MultiViewAdapter/blob/master/sample/sample.apk
119

12-
---
13-
14-
15-
# Gradle Dependency
10+
## Gradle Dependency
1611

1712
The Gradle dependency is available via [jCenter](https://bintray.com/devahamed/MultiViewADapter/multi-view-adapter/view).
1813
jCenter is the default Maven repository used by Android Studio.
@@ -22,53 +17,215 @@ The minimum API level supported by this library is API 14.
2217
```gradle
2318
dependencies {
2419
// ... other dependencies here
25-
compile 'com.github.devahamed:multi-view-adapter:0.6.0'
20+
compile 'com.github.devahamed:multi-view-adapter:0.9.0'
2621
}
2722
```
2823

29-
---
3024

25+
## Why this library?
3126

32-
# Changelog
27+
Most of the android apps out there uses recyclerview to display content.
28+
As with any other system-level api, recyclerview api is also designed in a generic way.
29+
So it needs lot of boilerplate code to be written for displaying a simple list. And it doubles, if you need to display multiple view types.
30+
MultiViewAdapter library helps you in removing this boilerplate code while allowing you to truly re-use the viewholder code across various adapters.
3331

34-
See the project's Releases page for a list of versions with their changelog.
32+
There are many other libraries, which provides the same feature. But they do enforce the either or all of the following constraints :
3533

36-
### [View Releases](https://github.com/DevAhamed/MultiViewAdapter/releases)
34+
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
3737

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
3939

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

44+
## Key concepts
4245

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.,).
4449

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
4651

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

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

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

55-
Now the advantages of the MultiViewAdapter
63+
<b>CarBinder</b>
64+
65+
```java
66+
class CarBinder extends ItemBinder<CarModel, CarBinder.CarViewHolder> {
5667

57-
1. No restrictions on POJO class' parent/hierarchy. Now, your model classes can truly reside inside data layer.
58-
2. No need to cast your models, no need for switch/if-else cases when you are having multiple view types.
59-
3. Takes advantage of diffutil and allows payload while notifying adapter.
68+
@Override public CarViewHolder create(LayoutInflater inflater, ViewGroup parent) {
69+
return new CarViewHolder(inflater.inflate(R.layout.item_car, parent, false));
70+
}
6071

61-
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.
72+
@Override public boolean canBindData(Object item) {
73+
return item instanceof CarModel;
74+
}
6275

63-
## Limitations
76+
@Override public void bind(CarViewHolder holder, CarModel item) {
77+
// Bind the data here
78+
}
6479

65-
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
80+
static class CarViewHolder extends BaseViewHolder<ItemModel> {
81+
// Normal ViewHolder code
82+
}
83+
}
84+
```
6685

67-
---
86+
<b>CarAdapter</b>
6887

88+
```java
89+
class CarAdapter extends RecyclerAdapter {
90+
91+
private DataListManager<CarModel> dataManager;
92+
93+
public SimpleAdapter() {
94+
this.dataManager = new DataListManager<>(this);
95+
addDataManager(dataManager);
96+
97+
registerBinder(new CarBinder());
98+
}
99+
100+
public void addData(List<CarModel> dataList) {
101+
dataManager.addAll(dataList);
102+
}
103+
}
104+
```
105+
106+
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.
122+
123+
```
124+
layoutManager.setSpanSizeLookup(adapter.getSpanSizeLookup());
125+
```
126+
127+
### ItemDecoration support
128+
Create your own item decoration class implementing ItemDecorator. It goes like this,
69129

70-
## License
71130

131+
```java
132+
133+
public class MyItemDecorator implements ItemDecorator {
134+
135+
public MyItemDecorator() {
136+
// Any initialization code
137+
}
138+
139+
@Override public void getItemOffsets(Rect outRect, int position, int positionType) {
140+
// Set item offset for each item
141+
// outRect.set(0, 0, 0, 0);
142+
}
143+
144+
@Override public void onDraw(Canvas canvas, RecyclerView parent, View child, int position,
145+
int positionType) {
146+
// Canvas drawing code implementation
147+
// Unlike default ItemDecoration, this method will be called for individual items. Do not create objects here.
148+
}
149+
}
150+
151+
```
152+
153+
The methods, getItemOffsets and onDraw will be called for each item. So avoid creating objects there.
154+
<br/>
155+
MyItemDecorator will be used with the ItemBinder as follows.
156+
157+
158+
```java
159+
160+
public class CustomItemBinder implements ItemBinder {
161+
162+
public CustomItemBinder(CustomItemBinder customItemBinder) {
163+
super(customItemBinder);
164+
}
165+
}
166+
167+
```
168+
169+
### Making RecyclerView selectable
170+
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)`.
179+
180+
181+
```java
182+
public class CustomDataManager extends DataListManager<GridItem> {
183+
184+
public CustomDataManager(RecyclerAdapter recyclerAdapter) {
185+
super(recyclerAdapter);
186+
}
187+
188+
@Override public boolean areContentsTheSame(GridItem oldItem, GridItem newItem) {
189+
// own impl
190+
}
191+
192+
@Override public Object getChangePayload(GridItem oldItem, GridItem newItem) {
193+
return super.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/>
223+
2. [AdapterDelegates](https://github.com/sockeqwe/AdapterDelegates)
224+
3. [Groupie](https://github.com/lisawray/groupie)
225+
4. [Epoxy](https://github.com/airbnb/epoxy)
226+
227+
228+
## License
72229
```
73230
Copyright 2017 Riyaz Ahamed
74231

bintray.gradle

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
apply plugin: 'com.jfrog.bintray'
2+
3+
version = libraryVersion
4+
5+
if (project.hasProperty("android")) {
6+
// Android libraries
7+
task sourcesJar(type: Jar) {
8+
classifier = 'sources'
9+
from android.sourceSets.main.java.srcDirs
10+
}
11+
12+
task javadoc(type: Javadoc) {
13+
source = android.sourceSets.main.java.srcDirs
14+
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
15+
failOnError false
16+
}
17+
} else {
18+
// Java libraries
19+
task sourcesJar(type: Jar, dependsOn: classes) {
20+
classifier = 'sources'
21+
from sourceSets.main.allSource
22+
}
23+
}
24+
25+
task javadocJar(type: Jar, dependsOn: javadoc) {
26+
classifier = 'javadoc'
27+
from javadoc.destinationDir
28+
}
29+
30+
artifacts {
31+
archives javadocJar
32+
archives sourcesJar
33+
}
34+
35+
// Bintray
36+
Properties properties = new Properties()
37+
properties.load(project.rootProject.file('local.properties').newDataInputStream())
38+
39+
bintray {
40+
user = properties.getProperty("bintray.user")
41+
key = properties.getProperty("bintray.apikey")
42+
43+
configurations = ['archives']
44+
pkg {
45+
repo = bintrayRepo
46+
name = bintrayName
47+
desc = libraryDescription
48+
websiteUrl = siteUrl
49+
vcsUrl = gitUrl
50+
licenses = allLicenses
51+
publish = true
52+
publicDownloadNumbers = true
53+
version {
54+
desc = libraryDescription
55+
gpg {
56+
sign = true //Determines whether to GPG sign the files. The default is false
57+
passphrase = properties.getProperty("bintray.gpg.password")
58+
//Optional. The passphrase for GPG signing'
59+
}
60+
}
61+
}
62+
}

install.gradle

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
apply plugin: 'com.github.dcendents.android-maven'
2+
3+
group = publishedGroupId // Maven Group ID for the artifact
4+
5+
install {
6+
repositories.mavenInstaller {
7+
// This generates POM.xml with proper parameters
8+
pom {
9+
project {
10+
packaging 'aar'
11+
groupId publishedGroupId
12+
artifactId artifact
13+
14+
// Add your description here
15+
name libraryName
16+
description libraryDescription
17+
url siteUrl
18+
19+
// Set your license
20+
licenses {
21+
license {
22+
name licenseName
23+
url licenseUrl
24+
}
25+
}
26+
developers {
27+
developer {
28+
id developerId
29+
name developerName
30+
email developerEmail
31+
}
32+
}
33+
scm {
34+
connection gitUrl
35+
developerConnection gitUrl
36+
url siteUrl
37+
}
38+
}
39+
}
40+
}
41+
}

multi-view-adapter/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ android {
1212
minSdkVersion 9
1313
targetSdkVersion 25
1414
versionCode 1
15-
versionName "1.0"
15+
versionName "0.9.0"
1616

1717
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1818
}
@@ -38,12 +38,12 @@ ext {
3838
artifact = devProperties['artifactId']
3939

4040
libraryDescription =
41-
'A utility library for creating re-usable view binders for recyclerview adapter'
41+
'Recyclerview Adapter library to create composable view holders'
4242

4343
siteUrl = 'https://github.com/DevAhamed/MultiViewAdapter'
4444
gitUrl = 'https://github.com/DevAhamed/MultiViewAdapter.git'
4545

46-
libraryVersion = '0.7.0'
46+
libraryVersion = '0.9.0'
4747

4848
developerId = devProperties['devId']
4949
developerName = devProperties['devName']
@@ -54,5 +54,5 @@ ext {
5454
allLicenses = ["Apache-2.0"]
5555
}
5656

57-
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
58-
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
57+
apply from: '../install.gradle'
58+
apply from: '../bintray.gradle'

0 commit comments

Comments
 (0)