Recycler-Core


Source link: https://github.com/carrot/recycler-core

Recycler-Core

Recycler-Core provides a clean MVC framework for managing your RecyclerView Adapters.

Motivation

Blindly dealing with multiple view types in a RecyclerView.Adapter can frequently lead to unmaintainable source code. The standard pattern of managing multiple views also does not promote reusability between multiple adapters.

Recycler-Core attempts to solve the maintainability and reusability problems that are ran into when dealing with multiple views in a RecyclerView.Adapter by creating an MVC pattern for RecyclerViews.

Download

Configure the project-level build.gradle to include the 'android-apt' plugin

buildscript {

repositories {

  jcenter()

}

dependencies {

  ...
  classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

}
 
}

Then, apply the 'android-apt' plugin in the module-level build.gradle and add the Recycler-Core dependencies:

Gradle:

apply plugin: 'com.neenbedankt.android-apt'  android {

... 
}
  dependencies {

compile 'com.carrotcreative.recyclercore:recycler-core:2.0-alpha'
apt 'com.carrotcreative.recyclercore:recyclercore-compiler:2.0-alpha' 
}

Usage

The View

To create a View Type for your RecyclerView, you're going to have to build out a component for the Model, View, and Controller.

In this example, we will build out an element that will display basic information about a user.

The View is a standard Layout XML file with nothing special about it.

element_user.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:padding="32dp"
  >

<TextView

android:id="@+id/name"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

/>

<TextView

android:id="@+id/email"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

/>  </LinearLayout>

The Model

Next I create the model.

The model is a standard POJO class, which is responsible for storing all of the data that defines our views.

When defining the model, we need to declare a class level annotation InjectController and pass in the controller and the layout.

UserModel.java:

// ... @RCModel(controller = UserController.class) public class UserModel {

public String mName;
  public String mEmail;

 // ... Setters + Getters  
}

The Controller

Now we create the controller.

This class must extend RecyclerCoreController with the generic of the model that was just created.

The magic happens in the bind method, where this object will be passed an instance of the model.

UserController.java:

// ... @RCController(layout = R.layout.element_user_list) public class UserController extends RecyclerCoreController<UserModel> {

private TextView mName;
  private TextView mEmail;

public UserController(View itemView, Activity activity)
  {

super(itemView, activity);

mName = (TextView) itemView.findViewById(R.id.name);

mEmail = (TextView) itemView.findViewById(R.id.email);

  
}

@Override
  public void bind(UserModel model)
  {

mName.setText(model.getName());

mEmail.setText(model.getEmail());

  
}
  
}

In use

MainActivity.java:

// ... ArrayList<RecyclerCoreModel> models = new ArrayList<>();
  UserModel brandon = new UserModel()

.setEmail("[email protected]")

.setName("Brandon");
 models.add(brandon);
  UserModel rob = new UserModel()

.setEmail("[email protected]")

.setName("Rob");
 models.add(rob);
  RecyclerCoreAdapter adapter = new RecyclerCoreAdapter(models);
 mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
 mRecyclerView.setAdapter(adapter);

And now you have:

Extras

ProgressRecyclerViewLayout

RecyclerCore also provides a custom view called ProgressRecyclerViewLayout. This is just a RecyclerView with a ProgressBar inside of a RelativeLayout and it manages displaying the ProgressBar when the adapter is set.

I pretty much never use RecyclerViews that aren't encapsulated inside this pattern so I decided to include it in this library.

SwipeRefreshProgressRecyclerView

We also have a SwipeRefreshProgressRecyclerView that is same as SwipeRefreshLayout with ProgressRecyclerViewLayout as a child. So it encapsulates the features or SwipeRefreshLayout, RecyclerView in one View.

OnLoadPointListener

ProgressRecyclerViewLayout also provides us interfaces that can allow us to add unlimited scroll.


 /**

* An interface to add a callback that gets called when the load point is reached.

* For the load point callback to work, we need to set

* {
@link #setDistanceFromBottomToLoadMore(int)
}
 and the

* {
@link #setOnLoadPointListener(OnLoadPointListener)
}

* <p>

* This currently only supports {
@link android.support.v7.widget.LinearLayoutManager
}
,

* {
@link android.support.v7.widget.StaggeredGridLayoutManager
}
,

* {
@link android.support.v7.widget.GridLayoutManager
}

*/
  public interface OnLoadPointListener
  {

void onReachedLoadPoint();

  
}
 

In order to get that working, we need to set distanceFromBottomToLoadMore attribute value, which can be set either in the Layout or programmatically.

<com.carrotcreative.recyclercore.widget.ProgressRecyclerViewLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/recycler_view_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  app:distanceFromBottomToLoadMore="0"
  /> 

OR


 /**

* Helper method to set the distance from bottom value programmatically, if its not set in

* the layout file.

* <p>

* A value of 0, means the #onReachedLoadPoint is called when the last child starts becoming

* visible.

* <p>

* A value of 1 means the #onReachedLoadPoint is called when the secnd last child starts

* becoming visible

* <p>

* A Negative value is considered and invalid value

*

* @param distanceFromBottomToLoadMore The no of item from the bottom of the recycler view,

*

after which #OnLoadPointListener is called

*/
  public void setDistanceFromBottomToLoadMore(int distanceFromBottomToLoadMore) 

Once this is done, we need to set a listener that will be called when the recycler view reached the load point



  mRecyclerViewLayout.setOnLoadPointListener(new ProgressRecyclerViewLayout

  .OnLoadPointListener()

{

 @Override

 public void onReachedLoadPoint()

 {

  loadMoreData();

 
}

}
);
 

License

MIT

Resources

This app shows how fast flat buffer works when we compare it with json.

Sample demonstrating how to set up SensorEventListeners for step detectors and step counters.

Simple and powerful MVP library for Android.

Ferro elegantly solves two age-old problems of Android Framework related to configuration changes:

  • restore screen's data
  • managing background tasks

ADB client implemented in pure Java.

Interactive Extensions for Java.

RxMarkdown is an Android library that helps to display simple markdown text in android.widget.EditText or android.widget.TextView.

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