LastAdapter


Source link: https://github.com/nitrico/LastAdapter

LastAdapter

Don't write a RecyclerView adapter again. Not even a ViewHolder!

  • Based on Android Data Binding
  • Written in Kotlin
  • No need to write the adapter
  • No need to write the viewholders
  • No need to modify your model classes
  • No need to notify the adapter when data set changes
  • Supports multiple item view types
  • Optional Callbacks/Listeners
  • Very fast — no reflection
  • Super easy API
  • Tiny size: 26 KB
  • Minimum Android SDK: 9

Setup

Gradle

// apply plugin: 'kotlin-kapt' // this line only for Kotlin projects  android {

  ...
  dataBinding.enabled true  
}
  dependencies {

  compile 'com.github.nitrico.lastadapter:lastadapter:2.2.2'
  // kapt 'com.android.databinding:compiler:GRADLE_PLUGIN_VERSION' // this line only for Kotlin projects 
}

Usage

Create your item layouts with <layout> as root:

<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

<variable name="item" type="com.github.nitrico.lastadapterproject.item.Header"/>
  </data>

 <TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@{
item.text
}
"/>

 </layout>

It is important for all the item types to have the same variable name, in this case "item". This name is passed to the adapter builder as BR.variableName, in this case BR.item:

// Java new LastAdapter(listOfItems, BR.item)

.map(Header.class, R.layout.item_header)

.map(Point.class, R.layout.item_point)

.into(recyclerView);
// Kotlin LastAdapter(listOfItems, BR.item)

.map<Header>(R.layout.item_header)

.map<Point>(R.layout.item_point)

.into(recyclerView)

The list of items can be an ObservableList if you want to get the adapter automatically updated when its content changes, or a simple List if you don't need to use this feature.

LayoutHandler

The LayoutHandler interface allows you to use different layouts based on more complex criteria. Its one single method receives the item and the position and returns the layout resource id.

// Java sample new LastAdapter(listOfItems, BR.item)

.handler(typeHandler)

.into(recyclerView);
  private LayoutHandler typeHandler = new LayoutHandler() {

  @Override public int getItemLayout(@NotNull Object item, int position) {

if (item instanceof Header) {

 return (position == 0) ? R.layout.item_header_first : R.layout.item_header;

}
 else {

 return R.layout.item_point;

}

  
}
 
}
;
// Kotlin sample LastAdapter(listOfItems, BR.item).layout {
 item, position ->
  when (item) {

is Header -> if (position == 0) R.layout.item_header_first else R.layout.item_header

else -> R.layout.item_point

}
 
}
.into(recyclerView)

For further information, please take a look at my article at Medium.

Custom fonts

You might also want to try FontBinder to easily use custom fonts in your XML layouts.

Acknowledgments

Thanks to Yigit Boyar and George Mount for this talk.

Author

Miguel Ángel Moreno

I'm open to new job positions - Contact me!

AngelList Email Facebook Google+ Linked.in Twitter

License

Copyright 2016 Miguel Ángel Moreno  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 support library for VectorDrawable and AnimatedVectorDrawable classes introduced in Lollipop.

A simple SegmentControl Widget.

This library provides an elegant form for credit card entry that can be easily added to a activity or fragment. Regex is used to validate credit card types and a Luhn check is performed on the card numbers.

This library make easy to use AppStore features.

Features:

  • Check store app is installed.
  • Open app within store app.
  • Search apps by publisher within store app.
  • Search apps within store app.

Decor is a library that applies decorators to android layout with additional attributes without the need to extend and create a custom view for each functionality.

Decor plugs into Android layout inflation and applies custom attributes to views. This library was inspired by Pretty.

This is an android-library which allow you to retrieve metadata such as title and headers of audio-stream as soon as possible.

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