Android Open Source - RZAndroidBaseUtils View Group Adapter






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.adapters;
/*w w w  .  j  av a 2s.  co m*/
import java.util.LinkedList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;

/**
 * Class which binds a set of data to {@link View}s in a given {@link ViewGroup}.
 * @author Dylan James
 *
 * @param <T> The type of the data to bind.
 */
public abstract class ViewGroupAdapter<T> {
  ViewGroup viewGroup;
  /**
   * Gets the {@link ViewGroup} this adapter is bound to.
   * @return The {@link ViewGroup} this adapter is bound to.
   */
  public ViewGroup getViewGroup() { return viewGroup; }
  
  private List<T> items;
  /**
   * Gets the list of items currently loaded into this adapter.
   * @return The list of items.
   */
  public List<T> getCurrentItems() { return items; }
  
  private boolean createClickListeners = true;
  public void setCreateClickListeners(boolean createClickListeners) { this.createClickListeners = createClickListeners; }
  public boolean createsClickListeners() { return createClickListeners; }
  
  public interface ItemClickedListener<T> {
    public void onItemClicked(ViewGroupAdapter<T> adapter, T item, int index);
  }
  private ItemClickedListener<T> itemClickedListener;
  /**
   * Sets the listener to be called when an item is clicked.
   * @param listener The listener to call.
   */
  public void setItemClickedListener(ItemClickedListener<T> listener) {
    this.itemClickedListener = listener;
  }
  
  public interface ItemLongClickedListener<T> {
    /**
     * Called when an item in the adapter is long clicked.
     * @param adapter The adapter whose item was long clicked.
     * @param item The item which was long clicked.
     * @param index The index of the long clicked item
     * @return true if the callback consumed the long click, false otherwise.
     */
    public boolean onItemLongClicked(ViewGroupAdapter<T> adapter, T item, int index);
  }
  private ItemLongClickedListener<T> itemLongClickedListener;
  /**
   * Sets the listener to be called when an item is long clicked.
   * @param listener The listener to call.
   */
  public void setItemLongClickedListener(ItemLongClickedListener<T> listener) {
    this.itemLongClickedListener = listener;
  }
  
  private LayoutInflater getLayoutInflater() {
    return LayoutInflater.from(viewGroup.getContext());
  }

  /**
   * Creates a {@link ViewGroupAdapter} bound to the given {@link ViewGroup}.
   * @param viewGroup The {@link ViewGroup} to bind to.
   */
  public ViewGroupAdapter(ViewGroup viewGroup) {
    if (viewGroup == null)
      throw new IllegalArgumentException("ViewGroup may not be null.");
    
    this.viewGroup = viewGroup;
    items = new LinkedList<T>();
  }
  
  /**
   * Loads the given items as the data set of this adapter. This replaces all
   * existing items.
   * @param items The items to set as the data.
   */
  public void load(Iterable<T> items) {
    viewGroup.removeAllViews();
    this.items.clear();
    add(items);
  }
  
  /**
   * Adds the given item to the end of the data set of this adapter.
   * @param item The item to add.
   */
  public void add(T item) {
    addItem(item, getLayoutInflater());
  }
  
  /**
   * Adds the given items to the end of the data set of this adapter.
   * @param items The items to add.
   */
  public void add(Iterable<T> items) {
    if (items == null) return;
    final LayoutInflater inflater = getLayoutInflater();
    for (T item : items) {
      addItem(item, inflater);
    }
  }
  
  
  /**
   * Removes all items from the data set of this adapter.
   */
  public void clear() {
    items.clear();
    viewGroup.removeAllViews();
  }
  
  /**
   * Returns the index of the first occurrence of the given item.
   * @param item The item to retrieve the index of.
   * @return The first index of the given item or -1 if it was not found.
   */
  public int indexOf(T item) {
    return items.indexOf(item);
  }
  
  /**
   * Removes the first occurrence of the given item from the data set of this
   * adapter.
   * @param item The item to remove.
   * @return True if the item was removed, false if it was not.
   */
  public boolean remove(T item) {
    if (item == null) return false;
    final int index = items.indexOf(item);
    return item.equals(removeAt(index));
  }
  
  /**
   * Removes the item at the given position in this adapter.
   * @param position The index to remove from.
   * @return The item that was removed.
   */
  public T removeAt(int position) {
    T item = items.remove(position);
    View view = getViewForIndex(position);
    viewGroup.removeView(view);
    return item;
  }
  
  /**
   * Finds the view in the {@link ViewGroup} for the given position.
   * Default implementation passes this call through to {@link ViewGroup#getChildAt(int)}.
   * Override this method if your child implementation is customizing the
   * arrangement or positions of child views in this adapter.
   * @param position The position of the view you would like to retrieve
   * @return The {@link View} at the given position in the {@link ViewGroup}.
   */
  public View getViewForIndex(int position) {
    return viewGroup.getChildAt(position);
  }
  
  protected void addItem(final T item, LayoutInflater inflater) {
    items.add(item);
    View view = createView(item, inflater, viewGroup);
    viewGroup.addView(view);
    
    if (createClickListeners) {
      view.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          if (itemClickedListener != null) {
            final int index = viewGroup.indexOfChild(v);
            itemClickedListener.onItemClicked(ViewGroupAdapter.this, item, index);
          }
        }
      });
      view.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
          if (itemLongClickedListener != null) {
            final int index = viewGroup.indexOfChild(v);
            return itemLongClickedListener.onItemLongClicked(ViewGroupAdapter.this, item, index);
          }
          return false;
        }
      });
    }
  }
  
  /**
   * Called to get the {@link View} to be displayed for the given item.
   * @param item The item to get the view for.
   * @param inflater A {@link LayoutInflater} to use to create the view.
   * @param root The container the {@link View} will be added to. The
   * implementation should not add the view itself.
   * @return The {@link View} to display for the given item.
   */
  protected abstract View createView(T item, LayoutInflater inflater, ViewGroup root);
}




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