EasiestGenericRecyclerAdapter


Source link: https://github.com/burakeregar/EasiestGenericRecyclerAdapter

Easiest Generic Recycler Adapter

Easiest way to use RecyclerView. Reduce boilerplate code! You don't need to write adapters for listing pages anymore!

You can check it out Demo Application here

How to Setup:

  • Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories:
 allprojects {

repositories {

 ...
 maven {
 url 'https://jitpack.io' 
}

}
  
}
  • Step 2. Add the dependency
 dependencies {

compile 'com.github.burakeregar:EasiestGenericRecyclerAdapter:v1.5'  
}

How to Use:

Create your ViewHolder class & extend GenericViewHolder

public class YourViewHolder extends GenericViewHolder {

  private YourModel mItem;

public YourViewHolder(final View itemView) {

super(itemView);

  
}

public void bindData(final Object element) {

mItem = (YourModel) element;

// set whatever you want. for instance;

surname.setText(mItem.getSurname());

name.setText(mItem.getName());

  
}
 
}

In your activity/fragment class you can initialize adapter like below;

GenericRecyclerAdapter mAdapter; RecyclerView mMainRv;
 mMainRv.setLayoutManager(new LinearLayoutManager(this));
 //set your layout manager
  mAdapter = new GenericAdapterBuilder()

  .addModel(

R.layout.contact_row, //set your row's layout file

 YourViewHolder.class, //set your view holder class

 YourModel.class) // set your model class(If you use just String list, it can be just String.class)

  .execute();

  mMainRv.setAdapter(mAdapter);

  

If your list contains different row types, you can add them like below;

mAdapter = new GenericAdapterBuilder()

  .addModel(

R.layout.contact_row,

  YourViewHolder.class,

  YourModel.class)

  .addModel(

R.layout.second_row,

  YourSecondViewHolder.class,

  YourSecondModel.class)

  .addModel(

R.layout.third_row,

  YourThirdViewHolder.class,

  YourThirdModel.class)

  .execute();

Now you are ready to set your list!

Assume that you have

List<YourModel> mList

or

List<Object> mList

then you can set your list like this.

mAdapter.setList(mList);

if you want to add new row(s) to your existing list, you can use addNewRows method. You can pass any object or object list to addNewRows method like below;

mAdapter.addNewRows(mList);

or

YourModel lItem; mAdapter.addNewRows(lItem);

You can use getItems method to get current items of the list;

List<YourModel> mList = mAdapter.getItems();

Using with Filter

If you want to use filter with your recyclerview for searching etc. you have to enable & set filter like below;

mAdapter = new GenericAdapterBuilder()

  .addModel(

  R.layout.contact_row,

 YourViewHolder.class,

 YourModel.class)

  .setFilterEnabled() //important!

  .execute();

 mAdapter.setFilter(new SearchFilter(mAdapter));

Example search filter class;

public class SearchFilter extends Filter {

  private GenericRecyclerAdapter mAdapter;
  public SearchFilter(GenericRecyclerAdapter pAdapter) {

 mAdapter = pAdapter;
 
}

  @Override
 protected FilterResults performFiltering(CharSequence constraint) {

 FilterResults results = new FilterResults();

  final List<Object> list = mAdapter.mOriginalObjectList;

  final ArrayList<Object> filteredList = new ArrayList<>();

  String filterableString = "";

  for (Object lModel : list) {

 if (lModel instanceof YourModel) {

 filterableString = ((YourModel) lModel).getValue();

 
}

if (filterableString.contains(constraint.toString())) {

 filteredList.add(lModel);

 
}

 
}

  results.values = filteredList;

 results.count = filteredList.size();

  return results;
 
}

  @Override
 protected void publishResults(CharSequence constraint, FilterResults results) {

 mAdapter.mObjectList = (List<Object>) results.values;

 mAdapter.notifyDataSetChanged();

 
}
 
}

you can set & listen the result in your edittext's afterTextChanged method;

mAdapter.getFilter().filter(editable.toString(), new Filter.FilterListener() {

 @Override

 public void onFilterComplete(int pI) {

 if (mAdapter.mObjectList == null || mAdapter.mObjectList.size() <= 0) {

  //If the search key is deleted, set the original list

mAdapter.addItem(mAdapter.mOriginalObjectList);

 
}

 
}

 
}
);

Contribution

Feel free to add new features to the library, I am happy to accept pull requests.

PROGUARD CONFIGURATION

Add this line to your proguard file

-keep class com.burakeregar.easiestgenericrecycleradapter.base.** {
 *; 
}

LICENSE

MIT License

Copyright (c) 2017 Burak Eregar

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Resources

Tool which allows to quickly try the demo project of any Android Library

Dexposed is a powerful yet non-invasive runtime AOP (Aspect-oriented Programming) framework for Android app development, based on the work of open-source Xposed framework project.

A method call logging based on dexposed.

A gallery used to host an array of images.

Android library to use Google Analytics through annotations.

The SimpleCropView is an image cropping library for Android. It simplify your code for cropping image and provides easily customizable UI.

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