Are you bored of creating adapters again and again each time you have to implement a ListView?
Are you bored of using ViewHolders and create getView methods with thousand of lines full of if/else if/else sentences?
Renderers is an Android library created to avoid all the Adapter/ListView 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 and Adapter classes. With this library you only have to create your renderers 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
To use Renderers Android library and get your ListView working you only have to follow three steps:
-
- Create your renderer or renderers extending
Renderer<T>
. Inside your renderers you will have to implement some methods to inflate the layout you want to render and implement the rendering algorithm.
- Create your renderer or renderers extending
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.
-
- Create a RendererBuilder with a renderer prototype collection and declare the mapping between the content to render and the renderer used.
public class VideoRendererBuilder extends RendererBuilder<Video> {
public VideoRendererBuilder(List<Renderer<Video>> prototypes) {
super(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;
}
}
-
- Initialize your ListView with your RendererBuilder and your AdapteeCollection inside Activities and Fragments.
/**
* Initialize ListVideo with our RendererAdapter.
*/
private void initListView() {
listView.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.
Download the project, compile it using mvn clean install
import renderers-1.0.9.jar
into your project.
Or declare it into your pom.xml
<dependency>
<groupId>com.github.pedrovgs</groupId>
<artifactId>renderers</artifactId>
<version>1.0.9</version>
</dependency>
Or into your build.gradle
dependencies{
compile 'com.github.pedrovgs:renderers:1.0.9'
}
- Pedro Vicente Gómez Sánchez - pedrovicente.gomez@gmail.com
Does your app use Renderers? If you want to be featured on this list drop me a line.
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.