All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.joanzapata.android.BaseAdapterHelper Maven / Gradle / Ivy

There is a newer version: 1.1.11
Show newest version
/**
 * Copyright 2013 Joan Zapata
 *
 * 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.joanzapata.android;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.util.Linkify;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestBuilder;

/**
 * Allows an abstraction of the ViewHolder pattern.
*
*

* Usage *

*

 * return BaseAdapterHelper.get(context, convertView, parent, R.layout.item)
 *         .setText(R.id.tvName, contact.getName())
 *         .setText(R.id.tvEmails, contact.getEmails().toString())
 *         .setText(R.id.tvNumbers, contact.getNumbers().toString())
 *         .getView();
 * 
*/ public class BaseAdapterHelper { /** Views indexed with their IDs */ private final SparseArray views; private final Context context; private View convertView; private BaseAdapterHelper(Context context, ViewGroup parent, int layoutId) { this.context = context; this.views = new SparseArray(); convertView = LayoutInflater.from(context) // .inflate(layoutId, parent, false); convertView.setTag(this); } /** * This method is the only entry point to get a BaseAdapterHelper. * @param context The current context. * @param convertView the convertView arg passed to the getView() method. * @param parent the parent arg passed to the getView() method. * @return A BaseAdapterHelper instance. */ public static BaseAdapterHelper get(Context context, View convertView, ViewGroup parent, int layoutId) { if (convertView == null) { return new BaseAdapterHelper(context, parent, layoutId); } return (BaseAdapterHelper) convertView.getTag(); } /** * This method allows you to retrieve a view and perform custom * operations on it, not covered by the BaseAdapterHelper.
* If you think it's a common use case, please consider creating * a new issue at https://github.com/JoanZapata/base-adapter-helper/issues. * @param viewId The id of the view you want to retrieve. */ public T getView(int viewId) { return retrieveView(viewId); } /** * Will set the text of a TextView. * @param viewId The view id. * @param value The text to put in the text view. * @return The BaseAdapterHelper for chaining. */ public BaseAdapterHelper setText(int viewId, String value) { TextView view = retrieveView(viewId); view.setText(value); return this; } /** * Will set the image of an ImageView from a resource id. * @param viewId The view id. * @param imageResId The image resource id. * @return The BaseAdapterHelper for chaining. */ public BaseAdapterHelper setImageResource(int viewId, int imageResId) { ImageView view = retrieveView(viewId); view.setImageResource(imageResId); return this; } /** * Will set the image of an ImageView from a drawable. * @param viewId The view id. * @param drawable The image drawable. * @return The BaseAdapterHelper for chaining. */ public BaseAdapterHelper setImageDrawable(int viewId, Drawable drawable) { ImageView view = retrieveView(viewId); view.setImageDrawable(drawable); return this; } /** * Will download an image from a URL and put it in an ImageView.
* It uses Square's Picasso library to download the image asynchronously and put the result into the ImageView.
* Picasso manages recycling of views in a ListView.
* If you need more control over the Picasso settings, use {BaseAdapterHelper#setImageBuilder}. * @param viewId The view id. * @param imageUrl The image URL. * @return The BaseAdapterHelper for chaining. */ public BaseAdapterHelper setImageUrl(int viewId, String imageUrl) { ImageView view = retrieveView(viewId); Picasso.with(context).load(imageUrl).into(view); return this; } /** * Will download an image from a URL and put it in an ImageView.
* @param viewId The view id. * @param requestBuilder The Picasso request builder. (e.g. Picasso.with(context).load(imageUrl)) * @return The BaseAdapterHelper for chaining. */ public BaseAdapterHelper setImageBuilder(int viewId, RequestBuilder requestBuilder) { ImageView view = retrieveView(viewId); requestBuilder.into(view); return this; } /** Add an action to set the image of an image view. Can be called multiple times. */ public BaseAdapterHelper setImageBitmap(int viewId, Bitmap bitmap) { ImageView view = retrieveView(viewId); view.setImageBitmap(bitmap); return this; } /** * Add an action to set the alpha of a view. Can be called multiple times. * Alpha between 0-1. */ public BaseAdapterHelper setAlpha(int viewId, float value) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { retrieveView(viewId).setAlpha(value); } else { // Pre-honeycomb hack to set Alpha value AlphaAnimation alpha = new AlphaAnimation(value, value); alpha.setDuration(0); alpha.setFillAfter(true); retrieveView(viewId).startAnimation(alpha); } return this; } /** * Set a view visibility to VISIBLE (true) or GONE (false). * @param viewId The view id. * @param visible True for VISIBLE, false for GONE. * @return The BaseAdapterHelper for chaining. */ public BaseAdapterHelper setVisible(int viewId, boolean visible) { ImageView view = retrieveView(viewId); view.setVisibility(visible ? View.VISIBLE : View.GONE); return this; } /** * Add links into a TextView. * @param viewId The id of the TextView to linkify. * @return The BaseAdapterHelper for chaining. */ public BaseAdapterHelper linkify(int viewId) { TextView view = retrieveView(viewId); Linkify.addLinks(view, Linkify.ALL); return this; } /** Retrieve the convertView */ public View getView() { return convertView; } @SuppressWarnings("unchecked") private T retrieveView(int viewId) { View view = views.get(viewId); if (view == null) { view = convertView.findViewById(viewId); views.put(viewId, view); } return (T) view; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy