• Stars
    star
    1,205
  • Rank 38,877 (Top 0.8 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Renderers is an Android library created to avoid all the boilerplate needed to use a RecyclerView/ListView with adapters.

Renderers Build Status Maven Central

Renderers is an Android library created to avoid all the RecyclerView/Adapter boilerplate needed to create a list/grid of data in your app and all the spaghetti code that developers used to create following the ViewHolder classic implementation. As performance is also important for us, we've added a new diffUpdate and a RVListRendererAdapter method supporting differential updated transparently in the main thread and a background thred respectively.

With this library you can improve your RecyclerView/Adapter/ViewHolder code. The one sometimes we copy and paste again and again 馃槂. Using this library you won't need to create any new class extending from RecyclerViewAdapter.

Create your Renderer classes and declare the mapping between the object to render and the Renderer. The Renderer will use the model information to draw your user interface. You can reuse them in all your RecyclerView and ListView implementations easily. That's it!

Screenshots

Demo Screenshot

Usage

To use Renderers Android library you only have to follow three steps:

    1. Create your Renderer class or classes extending Renderer<T>. Inside your Renderer classes. You will have to implement some methods to inflate the layout you want to render and implement the rendering algorithm.
public class VideoRenderer extends Renderer<Video> {

       @BindView(R.id.iv_thumbnail)
       ImageView thumbnail;
       @BindView(R.id.tv_title)
       TextView title;
       @BindView(R.id.iv_marker)
       ImageView marker;
       @BindView(R.id.tv_label)
       TextView label;

       @Override
       protected View inflate(LayoutInflater inflater, ViewGroup parent) {
           View inflatedView = inflater.inflate(R.layout.video_renderer, parent, false);
           ButterKnife.bind(this, inflatedView);
           return inflatedView;
       }

       @Override
       protected void render() {
           Video video = getContent();
           renderThumbnail(video);
           renderTitle(video);
           renderMarker(video);
           renderLabel();
       }

       @OnClick(R.id.iv_thumbnail)
       void onVideoClicked() {
           Video video = getContent();
           Log.d("Renderer", "Clicked: " + video.getTitle());
       }

       private void renderThumbnail(Video video) {
           Picasso.with(context).load(video.getResourceThumbnail()).placeholder(R.drawable.placeholder).into(thumbnail);
       }

       private void renderTitle(Video video) {
           this.title.setText(video.getTitle());
       }
}

You can use Jake Wharton's Butterknife library to avoid findViewById calls inside your Renderers if you want. But the usage of third party libraries is not mandatory.

    1. If you have just one type of item in your list, instantiate a RendererBuilder with a Renderer instance and you are ready to go:
Renderer<Video> renderer = new LikeVideoRenderer();
RendererBuilder<Video> rendererBuilder = new RendererBuilder<Video>(renderer);

If you need to render different objects into your list/grid you can use RendererBuilder.bind fluent API and that's it:

RendererBuilder<Video> rendererBuilder = new RendererBuilder<Video>()
         .bind(VideoHeader.class, new VideoHeaderRenderer())
         .bind(Video.class, new LikeVideoRenderer());
    1. Initialize your ListView or RecyclerView with your RendererBuilder and an optional List inside your Activity or Fragment. You should provide a list of items to configure your RendererAdapter or RVRendererAdapter.
private void initListView() {
    adapter = new RendererAdapter<Video>(rendererBuilder, list);
    listView.setAdapter(adapter);
}

or

private void initListView() {
    adapter = new RVRendererAdapter<Video>(rendererBuilder, list);
    recyclerView.setAdapter(adapter);
}

Remember, if you are going to use RecyclerView instead of ListView you'll have to use RVRendererAdapter instead of RendererAdapter.

    1. Diff updates:

If the RecyclerView performance is crucial in your application remember you can use diffUpdate method in your RVRendererAdapter instance to update just the items changed in your adapter and not the whole list/grid.*

adapter.diffUpdate(newList)

This method provides a ready to use diff update for our adapter based on the implementation of the standard equals and hashCode methods from the Object Java class. The classes associated to your renderers will have to implement equals and hashCode methods properly. Your hashCode implementation can be based on the item ID if you have one. You can use your hashCode implementation as an identifier of the object you want to represent graphically. We know this implementation is not perfect, but is the best we can do wihtout adding a new interface you have to implement to the library breaking all your existing code. Here you can review the DiffUtil.Callback implementation used in this library. If you can't follow this implementation you can always use a different approach combined with your already implemented renderers.

Also, RVListRendererAdapter provides a way to perform diff updates in a background thread transparently. When using RVListRendererAdapter you'll have a default DiffUtil.ItemCallback implementation (https://developer.android.com/reference/android/support/v7/util/DiffUtil.ItemCallback)) based on referencial equality for areItemsTheSame method and structural equality for areContentsTheSame method. You also have constructors on this class to provide your own implementation for DiffUtil.ItemCallback. You can even configure the threads used to perform the calculations through AsynDifferConfig class (https://developer.android.com/reference/android/support/v7/recyclerview/extensions/AsyncDifferConfig).

This library can also be used to show views inside a ViewPager. Take a look at VPRendererAdapter 馃槂

Usage

Add this dependency to your build.gradle:

dependencies{
    implementation 'com.github.pedrovgs:renderers:4.1.0'
}

Complex binding

If your renderers binding is complex and it's not based on different classes but in properties of these classes, you can also extend RendererBuilder and override getPrototypeClass to customize your binding as follows:

public class VideoRendererBuilder extends RendererBuilder<Video> {

  public VideoRendererBuilder() {
    List<Renderer<Video>> prototypes = getVideoRendererPrototypes();
    setPrototypes(prototypes);
  }

  /**
   * Method to declare Video-VideoRenderer mapping.
   * Favorite videos will be rendered using FavoriteVideoRenderer.
   * Live videos will be rendered using LiveVideoRenderer.
   * Liked videos will be rendered using LikeVideoRenderer.
   *
   * @param content used to map object-renderers.
   * @return VideoRenderer subtype class.
   */
  @Override
  protected Class getPrototypeClass(Video content) {
    Class prototypeClass;
    if (content.isFavorite()) {
      prototypeClass = FavoriteVideoRenderer.class;
    } else if (content.isLive()) {
      prototypeClass = LiveVideoRenderer.class;
    } else {
      prototypeClass = LikeVideoRenderer.class;
    }
    return prototypeClass;
  }

  /**
   * Create a list of prototypes to configure RendererBuilder.
   * The list of Renderer<Video> that contains all the possible renderers that our RendererBuilder
   * is going to use.
   *
   * @return Renderer<Video> prototypes for RendererBuilder.
   */
  private List<Renderer<Video>> getVideoRendererPrototypes() {
    List<Renderer<Video>> prototypes = new LinkedList<Renderer<Video>>();
    LikeVideoRenderer likeVideoRenderer = new LikeVideoRenderer();
    prototypes.add(likeVideoRenderer);

    FavoriteVideoRenderer favoriteVideoRenderer = new FavoriteVideoRenderer();
    prototypes.add(favoriteVideoRenderer);

    LiveVideoRenderer liveVideoRenderer = new LiveVideoRenderer();
    prototypes.add(liveVideoRenderer);

    return prototypes;
  }
}

References

You can find implementation details in these talks:

Software Design Patterns on Android Video

Software Design Patterns on Android Slides

Developed By

Follow me on Twitter Add me to Linkedin

License

Copyright 2016 Pedro Vicente G贸mez S谩nchez

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

EffectiveAndroidUI

Sample project created to show some of the best Android practices to work in the Android UI Layer. The UI layer of this project has been implemented using MVP or MVVM (without binding engine) to show how this patterns works. This project is used during the talk "EffectiveAndroidUI".
Java
6,027
star
2

AndroidWiFiADB

IntelliJ/AndroidStudio plugin which provides a button to connect your Android device over WiFi to install, run and debug your applications without a USB connected.
Java
4,138
star
3

DraggablePanel

Android library used to create an awesome Android UI based on a draggable element similar to the last YouTube graphic component.
Java
2,996
star
4

Algorithms

Solutions for some common algorithm problems written in Java.
Java
2,776
star
5

Shot

Screenshot testing library for Android
Kotlin
1,173
star
6

Lynx

Lynx is an Android library created to show a custom view with all the information Android logcat is printing, different traces of different levels will be rendererd to show from log messages to your application exceptions. You can filter this traces, share your logcat to other apps, configure the max number of traces to show or the sampling rate used by the library.
Java
773
star
7

TuentiTV

Tuenti application for Android TV created to show some of the most important features related to Android TV projects. This little sample uses mocked data to simulate an application working with information from Tuenti servers.
Java
381
star
8

Nox

Nox is an Android library created to show a custom view with some images or drawables inside which are drawn following a shape indicated by the library user.
Java
274
star
9

AndroidGameBoyEmulator

Android Game Boy Emulator written in Java
Java
233
star
10

KotlinKatas

Kotlin training repository used to learn Kotlin and Functional Programming by solving some common katas using just purely functional programming.
Kotlin
135
star
11

DeepPanel

Finding a panel inside a comic page is the hardest thing I've ever done in computer science!
Python
116
star
12

Kuronometer

Gradle plugin to measure build times. Let's measure how long developers around the world are compiling software.
Scala
74
star
13

Buzz

A portable photo booth built on top of Electron, React and Raspberry Pi.
JavaScript
41
star
14

DeepPaneliOS

Finding a panel inside a comic page is the hardest thing I've ever done in computer science!
Swift
39
star
15

DeepPanelAndroid

Finding a panel inside a comic page is the hardest thing I've ever done in computer science!
Kotlin
38
star
16

Roma

Spark project written in Scala used to perform real time sentiment analysis on top of Twitter's streaming API
Scala
34
star
17

JavaScriptKatas

JavaScript training repository used to learn JavaScript by solving some common katas.
JavaScript
32
star
18

SparkPlayground

Playground used to learn and experiment with Apache Spark
Scala
29
star
19

LedStorm

Imagination, some leds, and a RaspberryPi will take you wherever you want
Python
29
star
20

HaskellKatas

Haskell training repository used to learn Haskell and functional programming
Haskell
20
star
21

Dotto

Dotto is an Open Source morse translator for RaspberryPi developed to practice Scala.
Scala
19
star
22

KafkaPlayground

Playground used to learn and experiment with Apache Kafka 馃殌
Scala
18
star
23

TypeScriptKatas

TypeScript training repository used to learn TypeScript by solving some common katas.
TypeScript
17
star
24

UpdateRepos

Update all your git repositories with just one command. A command line tool written in Haskell.
Haskell
16
star
25

SwiftKatas

Swift training repository used to learn Swift and Functional Programming by solving some common katas using just purely functional programming.
Swift
14
star
26

ScalaKatas

Scala training repository used to learn Scala and Functional Programming by solving some common katas using just purely functional programming.
Scala
13
star
27

HWEmoji

AI handwriting recognition for emojis 馃槂
TypeScript
11
star
28

RomanNumerals-Kata

RomanNumerals kata implemented in java by Pedro Vicente G贸mez S谩nchez.
Java
8
star
29

HaveANiceDayChromeExtension

Chrome extension to generate smiles thanks to https://github.com/pedrovgs/HaveANiceDay
JavaScript
8
star
30

ScalaFirstSteps

Some samples written in Scala to evaluate some of the most important concepts of this language.
Scala
6
star
31

FizzBuzz-Kata

FizzBuzz kata implemented in java by Pedro Vicente G贸mez S谩nchez.
Java
6
star
32

Bowling-Kata

Bowling kata implemented in java by Pedro Vicente G贸mez S谩nchez.
Java
6
star
33

StringCalculator-Kata

StringCalculator kata implemented in java by Pedro Vicente G贸mez S谩nchez.
Java
6
star
34

HaveANiceDay

Smiles generator server-side code for https://github.com/delr3ves/haveanicedayandroid.
Scala
5
star
35

Sketches

Where the magic happens 鉁忥笍
5
star
36

KataStringCalculatorSwift

String Calculator Kata implemented in Swift
Swift
5
star
37

HTML5-CSS3-Playground

HTML5 & CSS3 Playground
HTML
3
star
38

ProjectEuler

Solutions for some Project Euler problems written in Scala.
Scala
3
star
39

StringCalculatorTest

Kotlin
2
star
40

RustKatas

Rust training repository used to learn Rust by solving some common katas using this programming language.
Rust
1
star