This library simplifies RecyclerView with multiple view types. Main points:
- Single adapter class for all project
- Easy to use - just register Cell, Model and ClickListener (optional) in adapter
- Listen clicks on any View of any type
- Built-in single / multi select
- Supports Java / Kotlin and androidx.recyclerview/support:recyclerview-v7
No more code like this:
@Override
public int getItemViewType(int position) {
// Just as an example, return 0 or 2 depending on position
return position % 2 * 2;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0: return new ViewHolder0(...);
case 2: return new ViewHolder2(...);
...
}
}
CellAdapter adapter = new CellAdapter(context);
// feel free to register multiple models and cells
// model per cell, so your RecyclerView would represent multiple view types
adapter.registerCell(SampleModel.class, SampleCell.class, new SampleCell.Listener(){});
var adapter: CellAdapter = CellAdapter().let {
it.cell(SampleCell1::class) {
item(SampleModel1::class)
listener(object : SampleCell1.Listener {})
}
it.cell(SampleCell2::class) {
item(SampleModel2::class)
listener(object : SampleCell2.Listener {})
}
}
where
SampleModel.class
is POJO and SampleCell.class
is
@Layout(R.layout.cell_sample)
public class SampleCell extends Cell<SampleModel, SampleCell.Listener> {
@Override
protected void bindView() {
getItem(); // is your Model object
}
protected void clearResources() {
//optional
}
public interface Listener extends Cell.Listener<Model> {
void callbackSample(Model model);
}
}
Kotlin is almost the same. Check samples for details.
Java + support:recyclerview-v7
Kotlin + support:recyclerview-v7
Kotlin + androidx.recyclerview
How to implement ButterKnife in Cells
For androidx - import io.erva.celladapter.x...
For support:recyclerview-v7 - import io.erva.celladapter.v7...
You have only import io.erva.celladapter
support:recyclerview-v7 by default
Add the JitPack repository in your root build.gradle at the end of repositories:
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
Add the dependency:
dependencies {
// one of two
implementation "com.android.support:recyclerview-v7:27.1.1" // any version
implementation "androidx.recyclerview:recyclerview:1.0.0-alpha3" // any version
// for java projects
implementation ('com.github.erva.CellAdapter:celladapter:3.0.0') {
exclude group: 'com.android.support', module: 'recyclerview-v7'
exclude group: 'androidx.recyclerview', module: 'recyclerview'
}
// for kotlin projects
implementation ('com.github.erva.CellAdapter:celladapter-kotlin:3.0.0') {
exclude group: 'com.android.support', module: 'recyclerview-v7'
exclude group: 'androidx.recyclerview', module: 'recyclerview'
}
}
#CellAdapter
-dontwarn io.erva.celladapter.**
-keepclasseswithmembers public class * extends io.erva.celladapter.** { *; }
#CellAdapter
-dontwarn io.erva.celladapter.**
-keep public class kotlin.reflect.jvm.internal.impl.builtins.* { public *; }
-keepclassmembers class * extends io.erva.celladapter.** {
<init>(android.view.View);
}
#CellAdapter
-dontwarn io.erva.celladapter.**
-keepclasseswithmembers public class * extends io.erva.celladapter.** { *; }
CellAdapter is licensed under the MIT License.