Skip to content

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

License

Notifications You must be signed in to change notification settings

CuteAndroid/Renderers

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Renderers Build Status Maven Central Android Arsenal

Are you bored of creating adapters again and again each time you have to implement a ListView or a RecyclerView?

Are you bored of using ViewHolders and create getView/onCreateViewHolder/onBindViewHolder methods with thousand of lines full of if/else if/else sentences?

Renderers is an Android library created to avoid all the Adapter/ListView/RecyclerView boilerplate needed to create a new adapter and all the spaghetti code that developers used to create following the ViewHolder classic implementation.

This Android library offers you two main classes to extend and create your own rendering algorithms out of your adapter implementation.

Renderers is an easy way to work with android ListView/RecyclerView and Adapter classes. With this library you only have to create your Renderer classes and declare the mapping between the object to render and the Renderer.

You can find implementation details in this talks:

Software Design Patterns on Android Video

Software Design Patterns on Android Slides

Screenshots

Demo Screenshot

Usage

To use Renderers Android library and get your ListView/RecyclerView working you only have to follow three steps:

    1. Create your Renderer or Renderers 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 abstract class VideoRenderer extends Renderer<Video> {

       private final Context context;

       private OnVideoClicked listener;


       public VideoRenderer(Context context) {
           this.context = context;
       }

       @InjectView(R.id.iv_thumbnail)
       ImageView thumbnail;
       @InjectView(R.id.tv_title)
       TextView title;
       @InjectView(R.id.iv_marker)
       ImageView marker;
       @InjectView(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.inject(this, inflatedView);
           return inflatedView;
       }


       @OnClick(R.id.iv_thumbnail)
       void onVideoClicked() {
           if (listener != null) {
               Video video = getContent();
               listener.onVideoClicked(video);
           }
       }

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

       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());
       }

       public void setListener(OnVideoClicked listener) {
           this.listener = listener;
       }

       protected TextView getLabel() {
           return label;
       }

       protected ImageView getMarker() {
           return marker;
       }

       protected Context getContext() {
           return context;
       }

       protected abstract void renderLabel();

       protected abstract void renderMarker(Video video);



}

You can use Jake Wharton's Butterknife library to avoid findViewById calls inside your Renderers if you want and Jake Wharton's [Dagger] 6 library to inject all your dependencies and keep your activities clean of the library initialization code. But use third party libraries is not mandatory.

    1. Create a RendererBuilder with a Renderer prototypes collection and declare the mapping between the content to render and the Renderer used.
public class VideoRendererBuilder extends RendererBuilder<Video> {

  @Inject
  public VideoRendererBuilder(Context context, VideoRenderer.OnVideoClicked onVideoClicked) {
    Collection<Renderer<Video>> prototypes = getPrototypes(context, onVideoClicked);
    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>> getPrototypes(Context context,
      VideoRenderer.OnVideoClicked onVideoClickedListener) {
    List<Renderer<Video>> prototypes = new LinkedList<Renderer<Video>>();
    LikeVideoRenderer likeVideoRenderer = new LikeVideoRenderer(context);
    likeVideoRenderer.setListener(onVideoClickedListener);
    prototypes.add(likeVideoRenderer);

    FavoriteVideoRenderer favoriteVideoRenderer = new FavoriteVideoRenderer(context);
    favoriteVideoRenderer.setListener(onVideoClickedListener);
    prototypes.add(favoriteVideoRenderer);

    LiveVideoRenderer liveVideoRenderer = new LiveVideoRenderer(context);
    liveVideoRenderer.setListener(onVideoClickedListener);
    prototypes.add(liveVideoRenderer);

    return prototypes;
  }
}
    1. Initialize your ListView/RecyclerView with your RendererBuilder and your AdapteeCollection inside Activities and Fragments. You can use ListAdapteeCollection or create your own implementation creating a class which implements AdapteeCollection to configure your RendererAdapter/RVRendererAdapter.
private void initListView() {
    listView.setAdapter(adapter);
}

or

private void initListView() {
    recyclerView.setAdapter(adapter);
}

The sample code is using Dagger and ButterKnife library to avoid initialize some entities and findViewById() methods, but you can use this library without third party libraries and provide that dependencies yourself.

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

If you was using Renderers v1.5 or a lower version and you want to use your Renderers with RecyclerView the only thing you only have to do is to replace your RendererAdapter extension with a RVRendererAdapter.

Usage

Download the project, compile it using mvn clean install import renderers-2.0.3.aar into your project.

Or declare it into your pom.xml

<dependency>
    <groupId>com.github.pedrovgs</groupId>
    <artifactId>renderers</artifactId>
    <version>2.0.3</version>
    <type>aar</type>
</dependency>

Or into your build.gradle

dependencies{
    compile 'com.github.pedrovgs:renderers:2.0.3'
}

Developed By

Follow me on Twitter Add me to Linkedin

Who's using it

Does your app use Renderers? If you want to be featured on this list drop me a line.

Contributors

License

Copyright 2014 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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%