Android Open Source - RZAndroidBaseUtils View Holder Strategy Utils






From Project

Back to project page RZAndroidBaseUtils.

License

The source code is released under:

MIT License

If you think the Android project RZAndroidBaseUtils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.raizlabs.widget.utils;
//from   ww w  .j  a  v  a  2  s.c  om
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.raizlabs.baseutils.R;

/**
 * Class with some utility functions useful for {@link ViewHolderStrategy}.
 * @author Dylan James
 *
 */
public class ViewHolderStrategyUtils {
  /**
   * ID of the tag in views in which the view holder should be stored.
   * @see View#getTag(int, Object)
   */
  public static final int VIEWHOLDER_TAG_ID = R.id.com_raizlabs_viewholderTagID;

  /**
   * Gets and populates a view for the given item, reusing the given view if
   * it is non-null. This assumes that the convert view is a valid view for
   * this item if it is non-null. If the convert view is null, a new view
   * will be created.
   * @param strategy The strategy to use to do the population.
   * @param item The item to populate a view for.
   * @param convertView A view which may be reused to display the item or
   * null to create a new one.
   * @param container The container the view will eventually be added to,
   * though this implementation will not add it.
   * @return The populated view.
   */
  public static <Item, Holder> View createOrConvertView(
      ViewHolderStrategy<Item, Holder> strategy,
      Item item,
      View convertView,
      ViewGroup container) {
    if (convertView == null) {
      return createAndPopulateView(strategy, item, container);
    } else {
      populateView(strategy, convertView, item);
      return convertView;
    }
  }
  
  /**
   * Creates and populates a view for the given item and returns it.
   * @param strategy The strategy to use to do the creation and population.
   * @param item The item to populate a view for.
   * @param container The container the view will eventually be added to,
   * though this implementation will not add it. 
   * @return The populated view.
   */
  public static <Item, Holder> View createAndPopulateView(
      ViewHolderStrategy<Item, Holder> strategy,
      Item item,
      ViewGroup container) {
    LayoutInflater inflater = LayoutInflater.from(container.getContext());
    return createAndPopulateView(strategy, item, inflater, container);
  }
  
  /**
   * Creates and populates a view for the given item and returns it.
   * @param strategy The strategy to use to do the creation and population.
   * @param item The item to populate a view for.
   * @param inflater An inflater to use to inflate the view.
   * @param container The container the view will eventually added to, though this
   * implementation will not add it. 
   * @return The populated view.
   */
  public static <Item, Holder> View createAndPopulateView(
      ViewHolderStrategy<Item, Holder> strategy,
      Item item,
      LayoutInflater inflater,
      ViewGroup container) {
    View view = strategy.inflateView(item, inflater, container);
    populateView(strategy, view, item);
    return view;
  }
  
  /**
   * Populates the given view for the given item using the given strategy.
   * @param strategy The strategy to use to populate the view.
   * @param view The view to populate.
   * @param item The item to populate the view with.
   */
  public static <Item, Holder> void populateView(
      ViewHolderStrategy<Item, Holder> strategy, View view, Item item) {
    if (view == null) return;

    Holder holder = getOrCreateViewHolder(strategy, view, item); 

    strategy.updateView(item, holder);
  }

  /**
   * Gets the view holder associated with the given view if it is valid for
   * the given item, or creates a new one.
   * @param strategy The strategy to use.
   * @param view The view to get or create the view holder from or for.
   * @param item The item the view represents.
   * @return The existing or created view holder.
   */
  public static <Item, Holder> Holder getOrCreateViewHolder(
      ViewHolderStrategy<Item, Holder> strategy, View view, Item item) {
    Holder holder = getViewHolderIfValid(strategy, view, item);
    if (holder == null)
      holder = createAndPopulateViewHolder(strategy, view, item);
    return holder;
  }
  
  /**
   * Creates a view holder for the given view representing the given item
   * and populates its pointers.
   * @param strategy The strategy to use to create and populate the view
   * holder.
   * @param view The view to populate the pointers from.
   * @param item The item to represent.
   * @return The created view holder.
   */
  public static <Item, Holder> Holder createAndPopulateViewHolder(
      ViewHolderStrategy<Item, Holder> strategy, View view, Item item) {
    if (strategy == null || view == null) return null;
    
    Holder holder = strategy.createHolder(item);
    strategy.populateHolder(view, holder);
    setViewHolder(view, holder);
    
    return holder;
  }
  
  /**
   * Gets the view holder associated with the given view if it is valid for
   * the given item.
   * @param strategy The strategy to use to check validity.
   * @param view The view to get the view holder of.
   * @param item The item to check the view holder against.
   * @return The associated view holder if it existed and was valid, otherwise
   * null.
   */
  @SuppressWarnings("unchecked")
  public static <Item, Holder> Holder getViewHolderIfValid(
      ViewHolderStrategy<Item, Holder> strategy, View view, Item item) {
    if (strategy == null || view == null) return null;

    try {
      // Try to cast the views view holder to the correct type
      Holder holder = (Holder) getViewHolder(view);
      // If we get here, we succeeded - ask the strategy if it's valid.
      if (strategy.isValidViewHolder(item, holder)) {
        return holder;
      }
    } catch (ClassCastException ex) { }

    // If we get here, it was null, couldn't be casted, or was invalid.
    return null;
  }

  /**
   * Sets the view holder associated with the given view to the given
   * view holder.
   * @param view The view to set the view holder of.
   * @param holder The view holder to associate with the given view.
   */
  public static void setViewHolder(View view, Object holder) {
    if (view != null)
      view.setTag(VIEWHOLDER_TAG_ID, holder);
  }
  
  /**
   * Returns the view holder associated with the given view.
   * @param view The view to get the view holder of.
   * @return The associated view holder, or null if none was found.
   */
  public static Object getViewHolder(View view) {
    if (view == null) return null;
    return view.getTag(VIEWHOLDER_TAG_ID);
  }
}




Java Source Code List

com.raizlabs.baseutils.CompatibilityUtils.java
com.raizlabs.baseutils.IOUtils.java
com.raizlabs.baseutils.Logger.java
com.raizlabs.baseutils.Math.java
com.raizlabs.baseutils.StringUtils.java
com.raizlabs.baseutils.ThreadingUtils.java
com.raizlabs.baseutils.Wrapper.java
com.raizlabs.baseutils.examples.MainActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableExampleActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableListExampleActivity.java
com.raizlabs.baseutils.examples.simplegenericadapter.SimpleGenericAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewgroupadapter.ViewGroupAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewholderstrategy.SimpleViewHolderStrategyExampleActivity.java
com.raizlabs.collections.ListUtils.java
com.raizlabs.collections.MappableSet.java
com.raizlabs.collections.TransactionalHashSet.java
com.raizlabs.concurrent.BasePrioritizedRunnable.java
com.raizlabs.concurrent.ConcurrencyUtils.java
com.raizlabs.concurrent.PrioritizedRunnable.java
com.raizlabs.concurrent.Prioritized.java
com.raizlabs.content.sharing.SharingUtils.java
com.raizlabs.database.CursorIterable.java
com.raizlabs.database.CursorIterator.java
com.raizlabs.events.EventListener.java
com.raizlabs.events.Event.java
com.raizlabs.events.ProgressListener.java
com.raizlabs.events.SimpleEventListener.java
com.raizlabs.functions.Delegate.java
com.raizlabs.functions.Predicate.java
com.raizlabs.functions.Provider.java
com.raizlabs.graphics.ImageFactory.java
com.raizlabs.graphics.drawable.async.AsyncDrawableTask.java
com.raizlabs.graphics.drawable.async.AsyncDrawableUtils.java
com.raizlabs.graphics.drawable.async.AsyncDrawableWrapper.java
com.raizlabs.graphics.drawable.async.AsyncDrawable.java
com.raizlabs.graphics.drawable.async.BaseAsyncDrawableTask.java
com.raizlabs.imagecaching.ImageCache.java
com.raizlabs.imagecaching.PrefixedImageCacheAdapter.java
com.raizlabs.imagecaching.StubImageCache.java
com.raizlabs.json.JSONArrayParserDelegate.java
com.raizlabs.json.JSONHelper.java
com.raizlabs.synchronization.OneShotLock.java
com.raizlabs.tasks.RZAsyncTaskEvent.java
com.raizlabs.tasks.RZAsyncTaskListener.java
com.raizlabs.tasks.RZAsyncTask.java
com.raizlabs.util.observable.ObservableData.java
com.raizlabs.util.observable.ObservableListAdapter.java
com.raizlabs.util.observable.ObservableList.java
com.raizlabs.view.ViewCompatibility.java
com.raizlabs.view.animation.AnimationListenerWrapper.java
com.raizlabs.view.animation.RelativeLayoutParamsAnimation.java
com.raizlabs.view.animation.ResizeAnimation.java
com.raizlabs.widget.EvenLinearLayout.java
com.raizlabs.widget.ImageMixView.java
com.raizlabs.widget.SlideRevealLayout.java
com.raizlabs.widget.ViewUtils.java
com.raizlabs.widget.adapters.ListBasedAdapter.java
com.raizlabs.widget.adapters.SimpleGenericAdapter.java
com.raizlabs.widget.adapters.ViewGroupAdapter.java
com.raizlabs.widget.adapters.ViewHolderStrategyAdapter.java
com.raizlabs.widget.utils.SimpleViewHolderStrategy.java
com.raizlabs.widget.utils.ViewHolderStrategyConverter.java
com.raizlabs.widget.utils.ViewHolderStrategyUtils.java
com.raizlabs.widget.utils.ViewHolderStrategy.java