infinity


Source link: https://github.com/thefuntasty/infinity

infinity

Tiny library with ability to provide support for infinite paginated scrolling in RecyclerView. It is able to show loading & try again layout at the bottom of the list.

Usage

Minimal implementation

  1. Implement you own RecyclerView adapter which extends InfinityAdapter<Model, ViewHolder>

    public class SampleUserAdapter extends InfinityAdapter<User, ViewHolder> {
    
    @Override  public ViewHolder onCreateContentViewHolder(ViewGroup parent, int viewType) {
    
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    
    View view = inflater.inflate(R.layout.list_item_user, parent, false);
    
    return new ViewHolder(view);
      
    }
    
    @Override  public void onBindContentViewHolder(ViewHolder holder, int position) {
    
    ((TextView) holder.itemView).setText(getContentItem(position).toString());
      
    }
    
    public class ViewHolder extends RecyclerView.ViewHolder {
    
    public ViewHolder(View itemView) {
    
     super(itemView);
    
    }
      
    }
     
    }
    
  2. Create instance of InfinityFiller<Model> class. It serves as a paginated data provider to your adapter.

    InfinityFiller<User> filler = new InfinityFiller<User>() {
      @Override public void onLoad(final int limit, final int offset, final Callback<User> callback) {
    
    // Async data provider
    DataManager.get().getDataObservable(limit, offset)
      .subscribe(new Action1<List<User>>() {
    
    @Override public void call(List<User> users) {
    
     callback.onData(users);
     // call onData() when data available
    
    }
    
      
    }
    , new Action1<Throwable>() {
    
    @Override public void call(Throwable throwable) {
    
     callback.onError(throwable);
     // call onError() when error occurs
    
    }
    
      
    }
    );
      
    }
     
    }
    ;
  3. Assign filler to adapter and start loading

    final SampleUserAdapter adapter = new SampleUserAdapter();
     recyclerView.setAdapter(adapter);
     adapter.setFiller(filler);
     adapter.start();
    

Handy features

Change error/loading layout at the bottom of the list

Simply override method getFooterLayout() in your adapter and return XML layout resource what contains 2 views with id's @+id/loading and @+id/retry.

  1. footer_layout.xml

    <LinearLayout  android:layout_width="match_parent"  android:layout_height="wrap_content">
    <LinearLayout
    android:id="@+id/loading"
    android:layout_width="...">
     <!-- content -->  </LinearLayout>
    <LinearLayout
    android:id="@+id/retry"
    android:layout_width="...">
     <!-- content -->  </LinearLayout> </LinearLayout>
  2. Adapter class

     @Override  public int getFooterLayout() {
    
    return R.layout.footer_layout;  
    }
    

Event listener

If you want to know about data loading events, you can set event listener via adapter.setEventListener() method.

adapter.setEventListener(new InfinityEventListener() {

@Override public void onPreLoadFirst(boolean pullToRefresh) {
 
}

@Override public void onFirstLoaded(boolean pullToRefresh) {
 
}

@Override public void onFirstUnavailable(Throwable error, boolean pullToRefresh) {
 
}

@Override public void onFirstEmpty(boolean pullToRefresh) {
 
}

@Override public void onPreLoadNext() {
 
}

@Override public void onNextLoaded() {
 
}

@Override public void onNextUnavailable(Throwable error) {
 
}

@Override public void onFinished() {
 
}
 
}
);

ViewType support

View types are supported by default. All you need to do is to override getContentItemViewType() method of your adapter class. View type value returned by this method must be in range <0,49>.

public static final int EVEN = 0; public static final int ODD = 1;  @Override public int getContentItemViewType(int position) {
  return position % 2 == 0 ? EVEN : ODD; 
}

Header support

In scenario you want to add header (or headers) at the top of the recycler, implement particular methods in your adapter class.

public static final int HEADER_EVEN = 50; public static final int HEADER_ODD = 51;  @Override public ViewHolder onCreateHeaderViewHolder(ViewGroup parent, int viewType) {
  if (viewType == HEADER_EVEN) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_header_type_1, parent, false);

return new ViewHolder(view);
  
}
 else {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_header_type_2, parent, false);

return new ViewHolder(view);
  
}
 
}
  @Override public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
  // ... 
}
  @Override public int getHeaderCount() {
  return NUM_OF_HEADERS; 
}
  @Override public int getHeaderItemViewType(int position) {
  if (position == 0) {

return HEADER_EVEN;  
}
 else {

return HEADER_ODD;  
}
 
}

There are two limitations:

  • Header view must share ViewHolder with content views. You can solve this problem by base ViewHolder.
  • Header view type values must be in range <50, 100>

Download

dependencies {
  compile 'com.github.thefuntasty:infinity:1.0.0' 
}

License

MIT License  Copyright (c) 2017 FUNTASTY Digital s.r.o.  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 urnished 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

Library to save image locally and shows options to open and share.

This dial widget is an advanced rotation control that you can easily include into any Android project in the form of an AAR file (Androids’ equivalent of a JAR file). You can think of this Dial control as a gesture library that is just concerned with rotation; having all of the conceivable behaviour you could possibly want in a rotation control. It comes with a free demo app.

Kotlin Wrapper for Jsoup using Pojo and Delegated Properties like bindText, bindAttr, bindImage, bindLink, bindList, bindClass

Awesome dialog is a simple wrapper class to allow user to make awesome dialogs and replace the usual dialog from android.

Ganalytics is tiny api layer for any analytics in application. It provides object oriented, typesafe, stricked and testable way for organize work with analytics through the application.

A Custom View to indicate multiple fragments with a triangular indicator.

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