Android Open Source - GridListViewAdapters View Holder Position Tagger






From Project

Back to project page GridListViewAdapters.

License

The source code is released under:

Apache License

If you think the Android project GridListViewAdapters 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

/**
 * Copyright 2014-present Biraj Patel//from  ww w  . j a  va  2 s  . c  om
 * 
 * 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.birin.gridlistviewadapters.utils;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import android.view.View;

/**
 * <p>
 * ViewHolderPositionTagger is used to tag position Value to View on each
 * getView call, this class is responsible for scanning Views presents in
 * ViewHolder class & tagging card's absolute position value to all children
 * views which were registered for "OnClickEvent" within passed CardViewHolder
 * class.
 * </p>
 * 
 * <p>
 * Scanning is performed by using Java Reflections currently only few scanning
 * are available
 * </p>
 * 
 * <p>
 * 1) Views present directly as member variables in ViewHolder.
 * </p>
 * 
 * <p>
 * 2) Array of View/View-wrapping-POJOs present as member variables in
 * ViewHolder.
 * </p>
 * 
 * <p>
 * 3) java.util.List of View/View-wrapping-POJOs present as member variables in
 * ViewHolder.
 * </p>
 * 
 * <p>
 * Note: combinations are also supported that means java.util.List of Array of
 * TextViews are also OK.
 * 
 * Note: Avoid holding any other objects other than child Views instances.
 * </p>
 * 
 * <p>
 * For custom scanning capability override {@link handleCustomDataObject} in a
 * subclass of this {@link ViewHolderPositionTagger} class & pass into
 * GridAdapters by overriding its getPositionTagger() method.
 * </p>
 */
public class ViewHolderPositionTagger {

  /**
   * This method scans given object for a {@link View} class reference & if
   * the view is registered for OnClickEvent then position value will be
   * tagged to that view, this position will be used to retrieve data present
   * at row position when click event occurs.
   * 
   * @param holder
   *            the Object in which we need to scan for {@link View} members
   * @param position
   *            the row position which needs to be tagged.
   */
  @SuppressWarnings("unchecked")
  public void scanAndTagViewsWithPositionValue(Object holder, int position) {
    for (Field field : holder.getClass().getDeclaredFields()) {
      field.setAccessible(true);
      Class<?> thisClass = field.getType();
      if (thisClass.isPrimitive() == false) {
        Object child = null;
        try {
          child = field.get(holder);
        } catch (IllegalAccessException e) {
        } catch (IllegalArgumentException e) {
        }
        if (child != null) {
          if (isViewInstance(child) == true) {
            tagObjectIfViewInstanceOrScanObjectForEnclosedViews(
                child, position);
          } else if (child instanceof Object[]) {
            tagViewsWithinArray((Object[]) child, position);
          } else if (child instanceof List<?>) {
            tagViewsWithinList((List<Object>) child, position);
          } else {
            handleCustomDataObject(child, position);
          }
        }
      }
    }
  }

  /**
   * Tag object if its view instance or scan object for enclosed views.
   * 
   * @param object
   *            the candidate for View.
   * @param position
   *            the row position
   */
  protected void tagObjectIfViewInstanceOrScanObjectForEnclosedViews(
      Object object, int position) {
    if (isViewInstance(object)) {
      View view = (View) object;
      addPositionTag(view, position);
    } else {
      scanAndTagViewsWithPositionValue(object, position);
    }
  }

  /**
   * Iterate over an array & tag all enclosed within its objects.
   * 
   * @param childrenArray
   *            the array of objects for which View objects are needed to be
   *            scanned.
   * @param position
   *            the row position
   */
  private void tagViewsWithinArray(Object[] childrenArray, int position) {
    for (Object child3 : childrenArray) {
      tagObjectIfViewInstanceOrScanObjectForEnclosedViews(child3,
          position);
    }
  }

  /**
   * Iterate over an java.util.List & tag all enclosed within its objects.
   * 
   * @param childrenList
   *            the list of objects for which View objects are needed to be
   *            scanned.
   * @param position
   *            the row position
   */
  private void tagViewsWithinList(List<Object> childrenList, int position) {
    for (Object childObject : childrenList) {
      tagObjectIfViewInstanceOrScanObjectForEnclosedViews(childObject,
          position);
    }
  }

  /**
   * We are going to scan only few types of objects present in ViewHolder if
   * user puts any data-structure which can not be scanned then this exception
   * is thrown,for example if user puts {@link Map} of Views into ViewHolder
   * object then this PositionTagger file doesn't know how to retrieve Views
   * from it so it throws error.
   * 
   * By overriding this method you tell library that you will handle any
   * custom Object present inside ViewHolder & you are responsible for tagging
   * position to Views present inside your CustomObjects.
   * 
   * @param child
   *            the child
   */
  protected void handleCustomDataObject(Object child, int position) {
    throw new IllegalArgumentException(
        "Unable to tag position data to "
            + child.getClass().getSimpleName()
            + " in your view-holder"
            + " object currently only View, java.util.List of Custom-View-Wrappers,"
            + " Array of Custom-View-wrappers are supported within ViewHolder class"
            + " if you have custom datastructure within your ViewHolder class consider"
            + " passing subclass of ViewHolderPositionTagger with your handling in"
            + " handleCustomDataObject(), and passing that subclass to adapter by"
            + " overriding getPositionTagger() method in your adapter this class.");
  }

  protected boolean isViewInstance(Object object) {
    return object instanceof View;
  }

  /**
   * Adds the position tag to a view if its registered for clicking.
   * 
   * @param view
   *            the view instance to be tagged.
   * @param position
   *            the row position to be tagged.
   */
  protected void addPositionTag(View view, int position) {
    if (ChildViewsClickHandler.getEventIdFromViewTag(view) >= 0) {
      ChildViewsClickHandler.tagPositionValueToView(view, position);
    }
  }

}




Java Source Code List

com.birin.cursorgridadapter.base.BaseCursorGridActivity.java
com.birin.cursorgridadapter.base.BaseEmployeeCursorGridAdapter.java
com.birin.cursorgridadapter.datasetup.CursorRetainingFragment.java
com.birin.cursorgridadapter.datasetup.TestContentProviderSqlHelper.java
com.birin.cursorgridadapter.datasetup.TestContentProvider.java
com.birin.cursorgridadapter.demo1.FixedCursorItems.java
com.birin.cursorgridadapter.demo2.CardClickHandlingEmployeeCursorGridAdapter.java
com.birin.cursorgridadapter.demo2.CardClickHandlingFixedCursorItems.java
com.birin.cursorgridadapter.demo3.ChildAndCardClickHandlingEmployeeCursorGridAdapter.java
com.birin.cursorgridadapter.demo3.ChildAndCardClickHandlingFixedCursorItems.java
com.birin.cursorgridadapter.demo4.FixedCursorItemsRotationSupport.java
com.birin.cursorgridadapter.demo5.UnlimitedCursorItemsRotationSupportAutoLoadMore.java
com.birin.cursorgridadapter.demo6.UnlimitedCursorItemsRotationClickToLoadMore.java
com.birin.gridlistviewadapters.BaseGridAdapter.java
com.birin.gridlistviewadapters.Card.java
com.birin.gridlistviewadapters.CursorFilter.java
com.birin.gridlistviewadapters.CursorGridAdapter.java
com.birin.gridlistviewadapters.ListGridAdapter.java
com.birin.gridlistviewadapters.RowViewHolder.java
com.birin.gridlistviewadapters.dataholders.CardDataHolder.java
com.birin.gridlistviewadapters.dataholders.CardPositionInfo.java
com.birin.gridlistviewadapters.dataholders.RowDataHolder.java
com.birin.gridlistviewadapters.utils.ChildViewsClickHandler.java
com.birin.gridlistviewadapters.utils.GridDataStructure.java
com.birin.gridlistviewadapters.utils.MaxCardsInfo.java
com.birin.gridlistviewadapters.utils.OnLoadMoreRequestListener.java
com.birin.gridlistviewadapters.utils.PositionCalculator.java
com.birin.gridlistviewadapters.utils.ViewHolderPositionTagger.java
com.birin.gridlistviewadaptersdemo.BaseDemoMenuList.java
com.birin.gridlistviewadaptersdemo.CursorDataDemos.java
com.birin.gridlistviewadaptersdemo.JavaUtilListDataDemos.java
com.birin.gridlistviewadaptersdemo.ParentDemoMenuList.java
com.birin.gridlistviewadaptersdemo.common.CharacterDrawable.java
com.birin.gridlistviewadaptersdemo.common.Constants.java
com.birin.gridlistviewadaptersdemo.common.EmployeeCardViewHolder.java
com.birin.gridlistviewadaptersdemo.common.RandomInfoGenerator.java
com.birin.listgridadapter.base.BaseEmployeeListGridAdapter.java
com.birin.listgridadapter.base.BaseListGridActivity.java
com.birin.listgridadapter.datasetup.Employee.java
com.birin.listgridadapter.datasetup.RetainedDataFragment.java
com.birin.listgridadapter.demo1.SimplestListGridAdapterUsageDemoActivity.java
com.birin.listgridadapter.demo2.FixedListItems.java
com.birin.listgridadapter.demo3.CardClickHandlingEmployeeListGridAdapter.java
com.birin.listgridadapter.demo3.CardClickHandlingFixedListItems.java
com.birin.listgridadapter.demo4.ChildAndCardClickHandlingEmployeeListGridAdapter.java
com.birin.listgridadapter.demo4.ChildAndCardClickHandlingFixedListItems.java
com.birin.listgridadapter.demo5.FixedListItemsRotationSupport.java
com.birin.listgridadapter.demo6.UnlimitedListItemsRotationSupportAutoLoadMore.java
com.birin.listgridadapter.demo7.UnlimitedListItemsRotationSupportAutoLoadMoreMax100Items.java
com.birin.listgridadapter.demo8.UnlimitedListItemsRotationSupportClickToLoadMore.java