MultiViewAdapter


Source link: https://github.com/DevAhamed/MultiViewAdapter

MultiViewAdapter

Helper library for recyclerviews to create composable view holders without boilerplate code.

Gradle Dependency

The Gradle dependency is available via JCenter. JCenter is the default maven repository used by Android Studio.

The minimum API level supported by this library is API 9.

dependencies {

  compile 'com.github.devahamed:multi-view-adapter:1.2.3'

 // If you want to use data binding
  compile 'com.github.devahamed:multi-view-adapter-databinding:1.2.3' 
}

Why this library?

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 code to be written for displaying a simple list. And it doubles, if you need to display multiple view types. MultiViewAdapter library helps you in removing this boilerplate code while allowing you to truly re-use the viewholder code across various adapters.

There are many other libraries, which provides the same feature. But they do enforce the either or all of the following constraints :

  1. Your POJO classes should extend/implement some Base classes. This forces us to make changes in model level.
  2. Forces you to implement some boilerplate code - like managing view types by yourself.
  3. Doesn't utilise diffutil or payloads from recyclerview api

Now the advantages of the MultiViewAdapter

  1. No restrictions on POJO class' parent/hierarchy. Now, your model classes can truly reside inside data layer.
  2. No need to cast your models, no need for switch/if-else cases when you are having multiple view types.
  3. Takes advantage of diffutil and allows payload while notifying adapter.

Key concepts

  1. RecyclerAdapter - This is the adapter class. It can have multiple ItemBinder and DataManagers. It extends from official RecyclerView.Adapter
  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.
  3. DataManger - Consider it as a List. But internally it calls necessary animations when the list is modified. There are two DataManagers. DataListManager for list of items. DataItemManager for a single item (Header, Footer etc.,).

You can read more about this library here in this Medium article.

Features

  1. Multiple data set can be added to the adapter. WikiDoc
  2. Adds different ItemDecoration for different ItemBinders. WikiDoc
  3. Single and Multiple selection options are available. WikiDoc
  4. Out of the box support for DiffUtil. WikiDoc
  5. Custom span count for every binder. WikiDoc
  6. Data binding support. WikiDoc
  7. Advanced drag and drop support. WikiDoc
  8. Swipe to dismiss. WikiDoc
  9. Infinite scrolling. WikiDoc
  10. Helper class for contextual action mode. WikiDoc
  11. Items can be expanded/collapsed. WikiDoc
  12. Groups can be expanded/collapsed. WikiDoc

File Templates

This library has default file templates for creating Adapters, ItemBinders for given model name. Take a look at wiki page for using the file templates. Wiki page

Usage

Let us display list of cars. No fuss. Here is the entire code.

CarBinder

class CarBinder extends ItemBinder<CarModel, CarBinder.CarViewHolder> {

 @Override public CarViewHolder create(LayoutInflater inflater, ViewGroup parent) {

  return new CarViewHolder(inflater.inflate(R.layout.item_car, parent, false));

}

 @Override public boolean canBindData(Object item) {

  return item instanceof CarModel;

}

 @Override public void bind(CarViewHolder holder, CarModel item) {

  // Bind the data here

}

 static class CarViewHolder extends BaseViewHolder<CarModel> {

  // Normal ViewHolder code

}
 
}

In your Activity/Fragment

class CarListActivity extends Activity {

 private RecyclerView recyclerView;
private List<CarModel> cars;
 public void initViews() {

  SimpleRecyclerAdapter<CarModel, CarBinder> adapter =

new SimpleRecyclerAdapter<>(new CarBinder());

recyclerView.setAdapter(adapter);

  adapter.setDataList(cars);

}
 
}

Now you are good to go.

For advanced usage and features kindly take a look at sample app code. Also we have comprehensive wiki pages as well. Take a look at Wiki home.

Changelog

See the project's Releases page for a list of versions with their changelog. View Releases
If you watch this repository, GitHub will send you an email every time there is an update.

Contribution

Contributing to this library is simple,

  1. Clone the repo
  2. Make the changes
  3. Make a pull request to develop branch

    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. Kindly make sure your code is formatted with this codestyle - Square Java code style

Alternatives

This library may not suit your needs or imposes too many restrictions. In that case create an issue/feature request. Mean time check these awesome alternatives as well.

  1. MultipleViewTypesAdapter - Original inspiration for this library.
  2. AdapterDelegates
  3. Groupie
  4. Epoxy

License

Copyright 2017 Riyaz Ahamed  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

A Gradle plugin for optimizing png files. It reduces volume of APK file and has support of extreme compression and lossless compression.

An alternative Gradle helper to publish multi-flavored Android artifacts to local and remote Maven repositories with pretty POM files using simple property configuration.

Reactive List implementation using RxJava.

GridBuilder is a view builder of Android native view group - GridLayout.

Create native mobile apps using Java.

Binds RxList to the RecyclerView.Adapter.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes