
com.pedrogomez.renderers.RendererAdapter Maven / Gradle / Ivy
/*
* Copyright (C) 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.
*/
package com.pedrogomez.renderers;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.pedrogomez.renderers.exception.NullRendererBuiltException;
import java.util.Collection;
/**
* BaseAdapter created to work RendererBuilders and Renderers. Other adapters have to use this one
* to create new lists.
*
* This class is the heart of this library. It's used to avoid the library users declare a new
* renderer each time they have to implement a new ListView.
*
* RendererAdapter has to be constructed with a LayoutInflater to inflate views, one
* RendererBuilder to provide Renderer to RendererAdapterdapter and one AdapteeCollection to
* provide the elements to render.
*
* @author Pedro Vicente Gómez Sánchez.
*/
public class RendererAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private RendererBuilder rendererBuilder;
private AdapteeCollection collection;
public RendererAdapter(LayoutInflater layoutInflater, RendererBuilder rendererBuilder,
AdapteeCollection collection) {
this.layoutInflater = layoutInflater;
this.rendererBuilder = rendererBuilder;
this.collection = collection;
}
@Override public int getCount() {
return collection.size();
}
@Override public T getItem(int position) {
return collection.get(position);
}
@Override public long getItemId(int position) {
return position;
}
/**
* Main method of RendererAdapter. This method has the responsibility of update renderer builder
* values and create or recycle a new rendere. Once the renderer has been obtained the
* RendereBuilder will call the render method in the renderer and will return the renderer root
* view to the ListView.
*
* If rendererBuilder returns a null renderer this method will throw a
* NullRendererBuiltException.
*
* @param position to render.
* @param convertView to use to recycle.
* @param parent used to inflate views.
* @return view rendered.
*/
@Override public View getView(int position, View convertView, ViewGroup parent) {
T content = getItem(position);
rendererBuilder.withContent(content);
rendererBuilder.withConvertView(convertView);
rendererBuilder.withParent(parent);
rendererBuilder.withLayoutInflater(layoutInflater);
Renderer renderer = rendererBuilder.build();
if (renderer == null) {
throw new NullRendererBuiltException("RendererBuilder have to return a not null renderer");
}
updateRendererExtraValues(content, renderer, position);
renderer.render();
return renderer.getRootView();
}
/**
* Indicate to the ListView the type of renderer used to one position using a numeric value.
*
* @param position to analyze.
* @return the id associated to the renderer used to render the content at position position.
*/
@Override public int getItemViewType(int position) {
T content = getItem(position);
return rendererBuilder.getItemViewType(content);
}
/**
* Indicate to the ListView the number of different renderers are the RendererBuilder to use.
*
* @return amount of different renderers.
*/
@Override public int getViewTypeCount() {
return rendererBuilder.getViewTypeCount();
}
/**
* Add an element to the AdapteeCollection.
*
* @param element to add.
*/
public void add(T element) {
collection.add(element);
}
/**
* Remove an element from the AdapteeCollection.
*
* @param element to remove.
*/
public void remove(T element) {
collection.remove(element);
}
/**
* Add a Collection of elements to the AdapteeCollection.
*
* @param elements to add.
*/
public void addAll(Collection elements) {
collection.addAll(elements);
}
/**
* Remove a Collection of elements to the AdapteeCollection.
*
* @param elements to remove.
*/
public void removeAll(Collection elements) {
collection.removeAll(elements);
}
/**
* Remove all elements inside the AdapteeCollection.
*/
public void clear() {
collection.clear();
}
/**
* Allows the client code to access the AdapteeCollection from subtypes of RendererAdapter.
*
* @return collection used in the adapter as the adaptee class.
*/
protected AdapteeCollection getCollection() {
return collection;
}
/**
* Empty implementation created to allow the client code to extend this class without override
* getView method.
* This method is called before render the renderer and can be used in RendererAdapter extension
* to add extra info to the renderer created.
*
* @param content to be rendered.
* @param renderer to be used to paint the content.
* @param position of the content.
*/
protected void updateRendererExtraValues(T content, Renderer renderer, int position) {
//Empty implementation
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy