RecyclerViewUtils


Source link: https://github.com/AdamMc331/RecyclerViewUtils

RecyclerViewUtils Library

This library contains a few core classes that are often used alongside a RecyclerView, and are intended to remove some of the tedious boilerplate code from being written many times.

Usage

To have access to the library, add the dependency to your build.gradle:

 compile 'com.adammcneilly:recyclerviewutils:2.0.2'

CoreViewHolder

At the heart of these classes is CoreViewHolder which is a RecyclerView.ViewHolder class used to display an object of a specific type. It has one abstract method for binding an object of that type.

Below is a sample of a CoreViewHolder for Account objects:

public class AccountViewHolder extends CoreViewHolder<Account> {

  private TextView tvName;
  private TextView tvBalance;

public AccountViewHolder(View view) {

super(view);

 this.tvName = (TextView) view.findViewById(R.id.account_name);

this.tvBalance = (TextView) view.findViewById(R.id.account_balance);

  
}

@Override
  protected void bindItem(Account item) {

this.tvName.setText(item.getName());

this.tvBalance.setText(String.valueOf(item.getBalance()));

  
}
 
}

Or in Kotlin:

internal inner class AccountViewHolder(view: View) : CoreViewHolder<Account>(view) {

  private val tvName: TextView = view.findViewById(R.id.account_name) as TextView
  private val tvBalance: TextView = view.findViewById(R.id.account_balance) as TextView

override fun bindItem(item: Account) {

this.tvName.text = item.name

this.tvBalance.text = item.balance.toString()
  
}
 
}

This class also implements the View.OnClickListener interface. If you want to handle clicks in your ReyclerView, just ovveride the onClick() method in your ViewHolder class.

CoreRecyclerViewAdapter

Using the CoreViewHolder class from above, the CoreRecyclerViewAdapter is an abstract base class for using an adapter of objects with a given type. By using a generic type, we were able to override many boilerplate methods such as:

  • add
  • remove
  • swapItems
  • onBindViewHolder

Thanks to this handy utils class, it cuts down on a ton of boilerplate code inside your adapter, and makes it very simple:

public class AccountAdapter extends CoreRecyclerViewAdapter<Account, AccountAdapter.AccountViewHolder>{

  public AccountAdapter(Context context, List<Account> accounts) {

super(context, accounts);

  
}

@Override
  public AccountViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

return new AccountViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item_account, parent, false));

  
}

public class AccountViewHolder extends CoreViewHolder<Account> {

...
  
}
 
}

Or in Kotlin:

internal class AccountAdapter(accounts: MutableList<Account>) : CoreRecyclerViewAdapter<Account, AccountAdapter.AccountViewHolder>(accounts) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AccountViewHolder {

return AccountViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.list_item_account, parent, false))
  
}

internal inner class AccountViewHolder(view: View) : CoreViewHolder<Account>(view) {

...
  
}
 
}

CoreDividerItemDecoration

The CoreDividerItemDecoration class is used to display a simple line divider between list items in your RecyclerView.

Credits & Contact

This library was created by Adam McNeilly.

And reviewed by Maurício Pessoa.

Version History

2.0.2

  • Added long click listener to CoreViewHolder.

2.0.1

  • Made a number of parameters nullable.

2.0.0

  • Converted library to Kotlin.

1.0.5

  • Updated support library version.

1.0.4

  • Updated support library version.

1.0.3

  • Minor update that made the items field protected, so sub classes could make use of it.

1.0.2

  • Added a default constructor and removed adapter's dependency on a Context.
  • Added the List interface to the adapter to allow easier addition/removal of items.

1.0.1

  • Added View.OnClickListener interface to the CoreViewHolder.
  • Code cleanup and removed deprecated methods.

1.0.0

  • Initial release.

License

The RecyclerViewUtils library is available under the MIT License. You are free to modify and enhance it in any way. If you submit a pull request, please add your name into the credits section! :)

Resources

A lightweight Youtube frontend for Android.

Simple Custom Tabs A library made to use custom tabs without going crazy!

Android passcode view.

A TextView subclass to show a reveal effect.

This is a library designed for highlighting hashtags ("#example") and catching click on them.

A simple, easy to use library to parse rss feeds into your app.

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