GenericRenderers


Source link: https://github.com/aballano/GenericRenderers

GenericRenderers

Based on Renderers lib made by pedrovgs

DIFFERENCE WITH RENDERERS

The main difference of this project is that is totally generic, which means:

  • No need to wrap every model in another object.
  • Possibility to bind more complex objects without extra effort.

But be aware that also means that you'll loose type safety.

USAGE

First of all, let's create as many Renderers as different views we need, for example:

public class VideoRenderer extends Renderer<Video> {

@Bind(R.id.iv_thumbnail)
  ImageView thumbnail;
  @Bind(R.id.tv_title)
  TextView title;

/** 
  * Inflate the main layout used to render videos in the list view. 
  * 
  * @param inflater LayoutInflater service to inflate. 
  * @param parent ViewGroup used to inflate xml. 
  * @return view inflated. 
  */
  @Override
  protected View inflate(LayoutInflater inflater, ViewGroup parent) {

View inflatedView = inflater.inflate(R.layout.video_renderer, parent, false);

/* 

* You don't have to use ButterKnife library to implement the mapping between your layout 

* and your widgets you can implement setUpView and hookListener methods declared in 

* Renderer<T> class. 

*/

ButterKnife.bind(this, inflatedView);

return inflatedView;
  
}

@OnClick(R.id.iv_thumbnail)
  void onVideoClicked() {

Video video = getContent();

Toast.makeText(getContext(), "Video clicked. Title = " + video.getTitle(), Toast.LENGTH_LONG).show();

  
}

/** 
  * Main render algorithm based on render the video thumbnail, render the title, render the marker 
  * and the label. 
  */
  @Override
  public void render(List<Object> payloads) {

Video video = getContent();

Picasso.with(getContext()).cancelRequest(thumbnail);

Picasso.with(getContext())

.load(video.getThumbnail())

.placeholder(R.drawable.placeholder)

.into(thumbnail);

title.setText(video.getTitle());

  
}
 
}

Now there are 3 possible usages:

Basic usage: only 1 model

RendererBuilder.create(new VideoRenderer())

.buildWith(videoCollection)

.into(recyclerView);

Advanced usage: multiple models

Ok, let's assume we now have another Renderer called SectionRenderer which is basically a section separator for our videos. Since is a simple header we just want to bind it with a String object, like:

RendererAdapter adapter = RendererBuilder.create()

 .bind(Video.class, new VideoRenderer())

 .bind(String.class, new SectionRenderer())

 .build()

 .into(recyclerView);

As you can see we use the default create method for the RendererBuilder and then use the chained bind methods to specify the renderer type for each item type we have.

Also, we don't provide our list in the constructor anymore (but we could), since we want to add the headers dynamically, like:

for (int i = 0, videoCollectionSize = videoCollection.size();
 i < videoCollectionSize; i++) {

  adapter.add("Video #" + (i + 1));

  adapter.add(videoCollection.get(i));
 
}

As you can see there's no problem in adding different types since the list in the adapter will be of type Object. In case that you add a different type that doesn't have a Renderer associated with, an exception will be thrown.

More complex usage: multiple complex models

Ok, let's go for a bit more complex thing, let's imagine that now we want to add a single footer at the end of the list with the FooterRenderer that you can see in the example. The type will be again a String class, so we need to differentiate between the String associated with the SectionRenderer added in the previous example, like this:

RendererAdapter adapter = RendererBuilder.create()

 .bind(Video.class, new VideoRenderer())

 .bind(TYPE_FOOTER, new FooterRenderer())

 .bind(TYPE_SECTION, new SectionRenderer2())

 .build()

 .into(recyclerView);

Where those types are plain integers. Finally we do the same as we did before and we add our footer at the end:

for (int i = 0, videoCollectionSize = videoCollection.size();
 i < videoCollectionSize; i++) {

  adapter.add(new RendererContent<>("Video #" + (i + 1), TYPE_SECTION));

  adapter.add(videoCollection.get(i));
 
}
 adapter.add(new RendererContent<>("by Alberto Ballano", TYPE_FOOTER));

As you see we need to add the wrapper now, since we need a generic object in which put the TYPE integer. But as you can see that's only for the objects that have to be mapped this way, so the Video class stays the same, no wrapper at all!

IMPORTANT: We also need to modify the SectionRenderer to use a different type:

public class SectionRenderer extends Renderer<RendererContent<String>>

EXTRA: Note that binding generic classes is also possible:

Assuming that we have many different Video implementations extending from BaseVideo but we want to map all of them to the same renderer we could just do:

RendererBuilder rendererBuilder = new RendererBuilder()

.bind(BaseVideo.class, new VideoRenderer())

.bind(String.class, new SectionRenderer());

And therefore all BaseVideo subclasses added to the adapter will be mapped to the VideoRenderer. For obvious reasons bindings to Object.class are forbidden to avoid unexpected errors, for that case please check the first usage above.

INCLUDING IN YOUR PROJECT

With gradle: edit your build.gradle

allprojects {

  repositories {

...

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

  
}
 
}
  dependencies {

compile 'com.github.aballano:GenericRenderers:x.x.x' 
}

Or declare it into your pom.xml

<repositories>
  <repository>

<id>jitpack.io</id>

<url>https://jitpack.io</url>
  </repository> </repositories>  <dependency>
  <groupId>com.github.aballano</groupId>
  <artifactId>GenericRenderers</artifactId>
  <version>x.x.x</version> </dependency>

Resources

Endoscope allows you to fast link two android devices and stream live video from camera one device to another. Video stream is over Wi-Fi. One device hosts live stream and another connect to it.

An example with floating view.

A line chart library.

A library that uses technologies like artificial Intelligence, machine learning, and deep learning to make developers understand the content that they are displaying in their app.

A simple client for the Clarifai image and video recognition API.

ReactiveAwareness lets you use the Android Awareness API with RxJava and without handling all those GoogleApiClients yourself.

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