首页 > 代码库 > adapter

adapter

1、android widget基础适配器  Adapter

public interface Adapter {
    注册数据集观察者,以便监听数据变动。
    void registerDataSetObserver(DataSetObserver observer);

    注销数据集观察者
    void unregisterDataSetObserver(DataSetObserver observer);

    获取数据集里数据项的数量
    int getCount();   
    
    获取指定position位置数据项
    Object getItem(int position);
    
    获取数据项的ID
    long getItemId(int position);
    
    数据集是否固定
    boolean hasStableIds();
    
    从数据集指定位置获取一个VIEW用来显示数据,你既可以通过创建一个VIEW也可以通过XML资源文件来inflate。
    当你通过XML资源文件创建时,默认的父VIEW给这个VIEW提供默认的LAYOUT参数,当然你也可以通过
    android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)
    来指定一个父view。
    position:指定的位置
    convertView:为了重复使用VIEW,当原来的VIEW和现在要使用的VIEW类型一致时,不需要转换,只需要更改内容就可以, 
                当类型不一样时,需要重新创建一个新类型的VIEW以便显示数据。
    parent:指定的父VIEW。   
    View getView(int position, View convertView, ViewGroup parent);

    static final int IGNORE_ITEM_VIEW_TYPE = AdapterView.ITEM_VIEW_TYPE_IGNORE;

    获取指定位置数据项显示VIEW的类型
    int getItemViewType(int position);
    
    获取总共VIEW的类型数量
    int getViewTypeCount();
    
    static final int NO_SELECTION = Integer.MIN_VALUE;
 
    判断是否为空
    boolean isEmpty();
}

2、SpinnerAdapter

public interface SpinnerAdapter extends Adapter {
    /**
     * <p>Get a {@link android.view.View} that displays in the drop down popup
     * the data at the specified position in the data set.</p>
     *
     * @param position      index of the item whose view we want.
     * @param convertView   the old view to reuse, if possible. Note: You should
     *        check that this view is non-null and of an appropriate type before
     *        using. If it is not possible to convert this view to display the
     *        correct data, this method can create a new view.
     * @param parent the parent that this view will eventually be attached to
     * @return a {@link android.view.View} corresponding to the data at the
     *         specified position.
     */
    public View getDropDownView(int position, View convertView, ViewGroup parent);
}

3、ListAdapter

public interface ListAdapter extends Adapter {

    /**
     * Indicates whether all the items in this adapter are enabled. If the
     * value returned by this method changes over time, there is no guarantee
     * it will take effect.  If true, it means all items are selectable and
     * clickable (there is no separator.)
     * 
     * @return True if all items are enabled, false otherwise.
     * 
     * @see #isEnabled(int) 
     */
    public boolean areAllItemsEnabled();

    /**
     * Returns true if the item at the specified position is not a separator.
     * (A separator is a non-selectable, non-clickable item).
     * 
     * The result is unspecified if position is invalid. An {@link ArrayIndexOutOfBoundsException}
     * should be thrown in that case for fast failure.
     *
     * @param position Index of the item
     * 
     * @return True if the item is not a separator
     * 
     * @see #areAllItemsEnabled() 
     */
    boolean isEnabled(int position);
}