Efficient Adapter for Android


Source link: https://github.com/StanKocken/EfficientAdapter

Efficient Adapter for Android

Create a new adapter for a RecyclerView or ViewPager is now much easier.

Overview

Create a list of elements into a RecyclerView or ViewPager is not that easy for a beginner, and repetitive for others. The goal of this library is to simplify that for you.

How does it work?

Create a class ViewHolder ( BookViewHolder for example). The method updateView will be call with the object, when an update of your view is require:

public class BookViewHolder extends EfficientViewHolder<Book> {

  public BookViewHolder(View itemView) {
  super(itemView);
 
}

@Override
  protected void updateView(Context context, Book object) {

TextView textView = findViewByIdEfficient(R.id.title_textview);

textView.setText(object.getTitle());

// or just
setText(R.id.title_textview, object.getTitle());

  
}
 
}
 

Give this ViewHolder class to the constructor of the adapter (SimpleAdapter) of your RecyclerView, with the resource id of your item view and the list of objects:

EfficientRecyclerAdapter<Plane> adapter = new EfficientRecyclerAdapter<Plane>(R.layout.item_book, BookViewHolder.class, listOfBooks);
 recyclerView.setAdapter(adapter);
 

And that's it!

It's also working with a ViewPager:

EfficientPagerAdapter<Plane> adapter = new EfficientPagerAdapter<Plane>(R.layout.item_book, BookViewHolder.class, listOfBooks);
 viewPager.setAdapter(adapter);
 

Other features

Heterogenous list

For a list of different kind of objects, layout, viewholder…

EfficientRecyclerAdapter adapter = new EfficientRecyclerAdapter(generateListObjects()) {

  @Override
  public int getItemViewType(int position) {

if (get(position) instanceof Plane) {

 return VIEW_TYPE_PLANE;

}
 else {

 return VIEW_TYPE_BOOK;

}

  
}

@Override
  public Class<? extends EfficientViewHolder> getViewHolderClass(int viewType) {

switch (viewType) {

 case VIEW_TYPE_BOOK:

  return BookViewHolder.class;

 case VIEW_TYPE_PLANE:

  return PlaneViewHolder.class;

 default:

  return null;

}

  
}

@Override
  public int getLayoutResId(int viewType) {

switch (viewType) {

 case VIEW_TYPE_BOOK:

  return R.layout.item_book;

 case VIEW_TYPE_PLANE:

  return R.layout.item_plane;

 default:

  return 0;

}

  
}
 
}
; 

Efficient findViewById()

Because findViewById(int) is CPU-consuming (this is why we use the ViewHolder pattern), this library use a findViewByIdEfficient(int id) into the ViewHolder class.

You can use it like a findViewById(int id) but the view return will be cached to be returned into the next call.

Your view id should be unique into your view hierarchy, but sometimes is not that easy (with an include for example). It's now easier to find a subview by specify the parent of this subview with findViewByIdEfficient(int parentId, int id) to say "the view with this id into the parent with this id".

Let the element be clickable

Your ViewHolder class can override the method isClickable() to tell is this element is clickable or not.

By default, the view is clickable if you have a listener on your adapter.

Proguard

This library includes the proguard configuration file. If you want to add it manually:

-keepclassmembers public class * extends com.skocken.efficientadapter.lib.viewholder.EfficientViewHolder {

  public <init>(...);
 
}
 

Gradle

dependencies {

  compile 'com.skocken:efficientadapter:2.2.1' 
}
 

License

Contributing

Please fork this repository and contribute back using pull requests.

Any contributions, large or small, major features, bug fixes, additional language translations, unit/integration tests are welcomed and appreciated but will be thoroughly reviewed and discussed.

Resources

A simple easy-to-use debug utility which helps you capture the logs simultaneously to adb terminal as well to a file on SD Card.

Makes it easy and simple to integrate "Attach that file" feature into your android apps.

A standalone packaging of the annotations from Google's AutoValue library.

A ViewPager that shows items from right to left for RTL locales and behaves like a regular ViewPager otherwise.

Template for Android Studio wizard for projects using Dagger.

An android library which provides a compact calendar view much like the one used in google calenders, following material design.

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