Home | History | Annotate | Download | only in sgv
      1 /*
      2  * Copyright (C) 2012 Google Inc. All Rights Reserved.
      3  */
      4 
      5 package com.android.deskclock.widget.sgv;
      6 import android.view.View;
      7 import android.view.ViewGroup;
      8 import android.widget.BaseAdapter;
      9 
     10 import com.android.deskclock.widget.sgv.ReorderUtils;
     11 
     12 /**
     13  * Adapter for use with {@link StaggeredGridView}. Copied from Keep.
     14  * TODO: Keep up to date with fixes to their code; if they move into a shared library, use that.
     15  */
     16 public abstract class GridAdapter extends BaseAdapter {
     17     /**
     18      * A tag key which stores the id of the associated object. If set, this
     19      * allows for faster creation of views.
     20      */
     21     private static final int GRID_ID_TAG = "gridIdTag".hashCode();
     22     private View mHeaderView;
     23     private View mFooterView;
     24 
     25     public GridAdapter() {
     26         super();
     27     }
     28 
     29     /**
     30      * Checks to see if the child at the specified position is draggable
     31      * @param position The position of the child to check against
     32      * @return boolean If true, the child at the specified position is draggable.
     33      */
     34     public boolean isDraggable(int position) {
     35         return false;
     36     }
     37 
     38     /**
     39      * For a view at the specified position, return the region around the view that is a possible
     40      * reordering area.
     41      * @param position The adapter position
     42      * @param isLastColumnInGrid Flag to indicate whether the view at the specified position is
     43      * laid out at the last column in the grid.  Being in the last column has some UI implications
     44      * when it comes to reordering.  As an example, if a view has reordering area set to Left, and
     45      * is laid out in the last column, the grid may also choose to enable the right reordering area
     46      * for this view as well so that the user has the convenience have dropping views on the right
     47      * edge of the grid to re-order.
     48      * @return int The re-ordering area for the view at this adapter position.
     49      * Possible return values are a combination of
     50      * {@link ReorderUtils#REORDER_AREA_NONE}, {@link ReorderUtils#REORDER_AREA_VALID}
     51      */
     52     public int getReorderingArea(int position, boolean isLastColumnInGrid) {
     53         return ReorderUtils.REORDER_AREA_NONE;
     54     }
     55 
     56     /**
     57      * The direction for drag to reorder that is allowed.  By default, the allowed direction
     58      * is free across both horizontal and vertical axes.
     59      * @return int The allowed direction for drag to reorder.  Possible return values are a
     60      * combination of {@link ReorderUtils#REORDER_DIRECTION_VERTICAL} and/or
     61      * {@link ReorderUtils#REORDER_DIRECTION_HORIZONTAL}
     62      */
     63     public int getReorderingDirection() {
     64         return ReorderUtils.REORDER_DIRECTION_VERTICAL | ReorderUtils.REORDER_DIRECTION_HORIZONTAL;
     65     }
     66 
     67     public View getHeaderView() {
     68         return mHeaderView;
     69     }
     70 
     71     public void setHeaderView(View view) {
     72         mHeaderView = view;
     73     }
     74 
     75     public View getFooterView() {
     76         return mFooterView;
     77     }
     78 
     79     public void setFooterView(View view) {
     80         mFooterView = view;
     81     }
     82 
     83     public boolean hasHeader() {
     84         return mHeaderView != null;
     85     }
     86 
     87     public boolean hasFooter() {
     88         return mFooterView != null;
     89     }
     90 
     91     /**
     92      * Views created via the GridAdapter or any subclasses should call this to
     93      * store the id of the item associated with them.
     94      */
     95     public void setItemId(View view, long id) {
     96         view.setTag(GRID_ID_TAG, id);
     97     }
     98 
     99     /**
    100      * Get the id of the item associated with this view.
    101      */
    102     public long getItemIdFromView(View view, int position) {
    103         final Object id = view.getTag(GRID_ID_TAG);
    104         if (id != null) {
    105             return (Long) id;
    106         }
    107 
    108         return getItemId(position);
    109     }
    110 
    111     /**
    112      * Get the id of the item associated with this view.  The specified Object is associated with
    113      * the view at this position, and can be used to optimize retrieval of the id by the adapter.
    114      * @param item Object associated with this view at this position.
    115      * @param position Position of the item.
    116      * @return id Id for the item at this position.
    117      */
    118     public long getItemId(Object item, int position) {
    119         // TODO: Rather than using Object, use BaseNode so that we're not confused between this
    120         // method, and the method above.
    121         return getItemId(position);
    122     }
    123 
    124     /**
    125      * Get the type of the view given the item it will display based on its
    126      * position in the cursor managed by the adapter. Previously, the adapter
    127      * would create an item based on the position and use that item to get the
    128      * view type. However, if the item already exists due to another call that
    129      * required it, it is much better to reuse the item than recreate it.
    130      *
    131      * @param item Object associated with the view at this position
    132      * @param position Position of the item we are verifying
    133      * @return int representing the type of the item at the supplied position
    134      */
    135     public int getItemViewType(Object item, int position) {
    136         return getItemViewType(position);
    137     }
    138 
    139     /**
    140      * Get the view given its associated item.
    141      *
    142      * @param item Object associated with the view at this position
    143      * @param position Position of the item we are verifying
    144      * @param scrap The old view to reuse, if possible. Note: You should check
    145      *            that this view is non-null and of an appropriate type before
    146      *            using. If it is not possible to convert this view to display
    147      *            the correct data, this method can create a new view.
    148      *            Heterogeneous lists can specify their number of view types, so
    149      *            that this View is always of the right type (see
    150      *            getViewTypeCount() and getItemViewType(int)).
    151      * @param parent The parent view this view will eventually be attached to
    152      * @param measuredWidth
    153      * @return View
    154      */
    155     public View getView(Object item, int position, View scrap, ViewGroup parent,
    156             int measuredWidth) {
    157         return getView(position, scrap, parent);
    158     }
    159 
    160     /**
    161      * Get how many columns a specific view will span in the grid.
    162      */
    163     abstract public int getItemColumnSpan(Object item, int position);
    164 
    165     /**
    166      * The first change position of all items. It will be used by
    167      * StaggeredGridView to optimize rendering, such as skip unchanged items. By
    168      * default it returns 0, so StaggeredGridView will not try to optimize.
    169      */
    170     public int getFirstChangedPosition() {
    171         return 0;
    172     }
    173 
    174     /**
    175      * StaggeredGridView only works with stable id. Any adapter which wants to
    176      * use staggered grid view is enforced to use stable ids.
    177      */
    178     @Override
    179     public boolean hasStableIds() {
    180         return true;
    181     }
    182 }
    183