Android HeaderViewListAdapter, listadapter
Public class
HeaderViewListAdapter
Extends Object
Implements Filterable WrapperListAdapter
Class Overview
ListAdapter used when a ListView has header views. This ListAdapter wraps another one and
Also keeps track of the header views and their associated data objects.
This is intended as a base class; you will probably not need to use this class directly in your own code.
The main function of HeaderViewListAdapter is to encapsulate and upgrade the ListAdapter and provide it with the function of adding the list header and the list end. This class is generally not directly used. Its main purpose is to provide a base class for us to adapt the list containing the list header and the list end. The constructor is as follows:
Public Constructors |
|
HeaderViewListAdapter (ArrayList <ListView. FixedViewInfo> headerViewInfos, ArrayList <ListView. FixedViewInfo> footerViewInfos, ListAdapter adapter) Parameters HeaderViewInfos is used to provide the List Header FooterViewInfos is used to provide the end of the list Adapter is used to adapt the list body |
In addition, ListView. FixedViewInfo is actually very simple. It encapsulates a View and its information.
Fields |
Public Object |
Data |
The data backing the view. |
Public boolean |
IsSelectable |
true If the fixed view shocould be selectable in the list |
Public View |
View |
The view to add to the list |
Public Constructors |
|
ListView. FixedViewInfo () |
The source code of HeaderViewListAdapter is as follows: HeaderViewListAdapter. java file/** Copyright (C) 2006 The Android Open Source Project ** Licensed under the Apache License, Version 2.0 (the "License "); * you may not use this file before t in compliance with the License. * You may be 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 android. widget;
Import android. database. DataSetObserver; import android. view. View; import android. view. ViewGroup;
Import java. util. ArrayList;
/*** ListAdapter used when a ListView has header views. this ListAdapter * wraps another one and also keeps track of the header views and their * associated data objects. * <p> This is intended as a base class; you will probably not need to * use this class directly in your own code. */public class HeaderViewListAdapter implements WrapperListAdapter, Filterable {
Private final ListAdapter mAdapter;
// These two ArrayList are assumed to NOT be null. // They are indeed created when declared in ListView and then shared. arrayList <ListView. fixedViewInfo> mHeaderViewInfos; ArrayList <ListView. fixedViewInfo> mFooterViewInfos;
// Used as a placeholder in case the provided info views are indeed null. // Currently only used by some CTS tests, which may be removed. static final ArrayList <ListView. fixedViewInfo> EMPTY_INFO_LIST = new ArrayList <ListView. fixedViewInfo> ();
Boolean mAreAllFixedViewsSelectable;
Private final boolean mIsFilterable;
Public HeaderViewListAdapter (ArrayList <ListView. FixedViewInfo> headerViewInfos, ArrayList <ListView. FixedViewInfo> footerViewInfos, ListAdapter adapter) {mAdapter = adapter; mIsFilterable = adapter instanceof Filterable;
If (headerViewInfos = null) {mHeaderViewInfos = EMPTY_INFO_LIST;} else {mHeaderViewInfos = headerViewInfos ;}
If (footerViewInfos = null) {mFooterViewInfos = EMPTY_INFO_LIST;} else {mFooterViewInfos = footerViewInfos ;}
MAreAllFixedViewsSelectable = areAllListInfosSelectable (mHeaderViewInfos) & areAllListInfosSelectable (mFooterViewInfos );}
Public int getHeadersCount () {return mHeaderViewInfos. size ();}
Public int getFootersCount () {return mFooterViewInfos. size ();}
Public boolean isEmpty () {return mAdapter = null | mAdapter. isEmpty ();}
Private boolean areAllListInfosSelectable (ArrayList <ListView. FixedViewInfo> infos) {if (infos! = Null) {for (ListView. FixedViewInfo info: infos) {if (! Info. isSelectable) {return false ;}} return true ;}
Public boolean removeHeader (View v) {for (int I = 0; I <mHeaderViewInfos. size (); I ++) {ListView. fixedViewInfo info = mHeaderViewInfos. get (I); if (info. view = v) {mHeaderViewInfos. remove (I );
MAreAllFixedViewsSelectable = areAllListInfosSelectable (mHeaderViewInfos) & areAllListInfosSelectable (mFooterViewInfos );
Return true ;}}
Return false ;}
Public boolean removeFooter (View v) {for (int I = 0; I <mFooterViewInfos. size (); I ++) {ListView. fixedViewInfo info = mFooterViewInfos. get (I); if (info. view = v) {mFooterViewInfos. remove (I );
MAreAllFixedViewsSelectable = areAllListInfosSelectable (mHeaderViewInfos) & areAllListInfosSelectable (mFooterViewInfos );
Return true ;}}
Return false ;}
Public int getCount () {if (mAdapter! = Null) {return getFootersCount () + getHeadersCount () + mAdapter. getCount () ;}else {return getFootersCount () + getHeadersCount ();}}
Public boolean areAllItemsEnabled () {if (mAdapter! = Null) {return mAreAllFixedViewsSelectable & mAdapter. areAllItemsEnabled () ;}else {return true ;}}
Public boolean isEnabled (int position) {// Header (negative positions will throw an ArrayIndexOutOfBoundsException) int numHeaders = getHeadersCount (); if (position <numHeaders) {return mHeaderViewInfos. get (position ). isSelectable ;}
// Adapter final int adjPosition = position-numHeaders; int adapterCount = 0; if (mAdapter! = Null) {adapterCount = mAdapter. getCount (); if (adjPosition <adapterCount) {return mAdapter. isEnabled (adjPosition );}}
// Footer (off-limits positions will throw an ArrayIndexOutOfBoundsException) return mFooterViewInfos. get (adjPosition-adapterCount). isSelectable ;}
Public Object getItem (int position) {// Header (negative positions will throw an ArrayIndexOutOfBoundsException) int numHeaders = getHeadersCount (); if (position <numHeaders) {return mHeaderViewInfos. get (position ). data ;}
// Adapter final int adjPosition = position-numHeaders; int adapterCount = 0; if (mAdapter! = Null) {adapterCount = mAdapter. getCount (); if (adjPosition <adapterCount) {return mAdapter. getItem (adjPosition );}}
// Footer (off-limits positions will throw an ArrayIndexOutOfBoundsException) return mFooterViewInfos. get (adjPosition-adapterCount). data ;}
Public long getItemId (int position) {int numHeaders = getHeadersCount (); if (mAdapter! = Null & position> = numHeaders) {int adjPosition = position-numHeaders; int adapterCount = mAdapter. getCount (); if (adjPosition <adapterCount) {return mAdapter. getItemId (adjPosition) ;}} return-1 ;}
Public boolean hasStableIds () {if (mAdapter! = Null) {return mAdapter. hasStableIds ();} return false ;}
Public View getView (int position, View convertView, ViewGroup parent) {// Header (negative positions will throw an ArrayIndexOutOfBoundsException) int numHeaders = getHeadersCount (); if (position <numHeaders) {return mHeaderViewInfos. get (position ). view ;}
// Adapter final int adjPosition = position-numHeaders; int adapterCount = 0; if (mAdapter! = Null) {adapterCount = mAdapter. getCount (); if (adjPosition <adapterCount) {return mAdapter. getView (adjPosition, convertView, parent );}}
// Footer (off-limits positions will throw an ArrayIndexOutOfBoundsException) return mFooterViewInfos. get (adjPosition-adapterCount). view ;}
Public int getItemViewType (int position) {int numHeaders = getHeadersCount (); if (mAdapter! = Null & position> = numHeaders) {int adjPosition = position-numHeaders; int adapterCount = mAdapter. getCount (); if (adjPosition <adapterCount) {return mAdapter. getItemViewType (adjPosition );}}
Return AdapterView. ITEM_VIEW_TYPE_HEADER_OR_FOOTER ;}
Public int getViewTypeCount () {if (mAdapter! = Null) {return mAdapter. getViewTypeCount ();} return 1 ;}
Public void registerDataSetObserver (DataSetObserver observer) {if (mAdapter! = Null) {mAdapter. registerDataSetObserver (observer );}}
Public void unregisterDataSetObserver (DataSetObserver observer) {if (mAdapter! = Null) {mAdapter. unregisterDataSetObserver (observer );}}
Public Filter getFilter () {if (mIsFilterable) {return (Filterable) mAdapter). getFilter ();} return null;} public ListAdapter getWrappedAdapter () {return mAdapter ;}}