HeaderViewListAdapter

publicclass
HeaderViewListAdapter
extendsObject
implementsFilterableWrapperListAdapter
ClassOverview
ListAdapterusedwhenaListViewhasheaderviews.ThisListAdapterwrapsanotheroneand
alsokeepstrackoftheheaderviewsandtheirassociateddataobjects.
Thisisintendedasabaseclass;youwillprobablynotneedtousethisclassdirectlyinyourowncode.
HeaderViewListAdapter 的主要作用就是在 ListAdapter 基础上封装和升级,为其提供了添加 列表头 列表尾 的功能。
该类一般不直接使用,它的主要目的是为我们提供一个对包含列表头和列表尾的列表进行适配的一个基类。
构造函数如下:
Public Constructors
<nobr style="line-height:21px"></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/widget/HeaderViewListAdapter.html#HeaderViewListAdapter(java.util.ArrayList&lt;android.widget.ListView.FixedViewInfo&gt;,%20java.util.ArrayList&lt;android.widget.ListView.FixedViewInfo&gt;,%20android.widget.ListAdapter)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">HeaderViewListAdapter</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/util/ArrayList.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ArrayList</a>&lt;<a rel="nofollow" href="http://developer.android.com/reference/android/widget/ListView.FixedViewInfo.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ListView.FixedViewInfo</a>&gt; headerViewInfos,<a rel="nofollow" href="http://developer.android.com/reference/java/util/ArrayList.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ArrayList</a>&lt;<a rel="nofollow" href="http://developer.android.com/reference/android/widget/ListView.FixedViewInfo.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ListView.FixedViewInfo</a>&gt; footerViewInfos,<a rel="nofollow" href="http://developer.android.com/reference/android/widget/ListAdapter.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ListAdapter</a>adapter)<br style="line-height:21px"><span style="line-height:21px">参数</span><br style="line-height:21px"><span style="color:#993300; line-height:21px">headerViewInfos</span>用于提供列表头<br style="line-height:21px"><span style="color:#993300; line-height:21px">footerViewInfos</span> 用于提供列表尾<br style="line-height:21px"><span style="color:#993300; line-height:21px">adapter</span> 用于为列表正文进行适配</nobr>
另外, ListView.FixedViewInfo 其实很简单,它就是对一个View及其信息的封装。
Fields
<nobr style="line-height:21px">public<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Object.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Object</a></nobr> data The data backing the view.
<nobr style="line-height:21px">public boolean</nobr> isSelectable trueif the fixed view should be selectable in the list
<nobr style="line-height:21px">public<a rel="nofollow" href="http://developer.android.com/reference/android/view/View.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">View</a></nobr> view The view to add to the list
Public Constructors
<nobr style="line-height:21px"></nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/widget/ListView.FixedViewInfo.html#ListView.FixedViewInfo()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ListView.FixedViewInfo</a></span>()</nobr>
HeaderViewListAdapter 源码如下
HeaderViewListAdapter.java文件
/*
* Copyright (C) 2006 The Android Open Source Project
*
* 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 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 HeaderViewListAdapterimplements WrapperListAdapter, Filterable {

private finalListAdapter 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 finalArrayList<ListView.FixedViewInfo> EMPTY_INFO_LIST =
new ArrayList<ListView.FixedViewInfo>();

booleanmAreAllFixedViewsSelectable;

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);
}

publicint getHeadersCount() {
return mHeaderViewInfos.size();
}

public int getFootersCount() {
returnmFooterViewInfos.size();
}

public booleanisEmpty() {
returnmAdapter == null || mAdapter.isEmpty();
}

private booleanareAllListInfosSelectable(ArrayList<ListView.FixedViewInfo> infos) {
if (infos != null) {
for (ListView.FixedViewInfo info : infos) {
if (!info.isSelectable) {
return false;
}
}
}
returntrue;
}

public booleanremoveHeader(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 booleanremoveFooter(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 intgetCount() {
if (mAdapter != null) {
return getFootersCount() + getHeadersCount() + mAdapter.getCount();
} else {
return getFootersCount() + getHeadersCount();
}
}

public booleanareAllItemsEnabled() {
if (mAdapter != null) {
return mAreAllFixedViewsSelectable && mAdapter.areAllItemsEnabled();
} else {
return true;
}
}

public booleanisEnabled(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)
returnmFooterViewInfos.get(adjPosition - adapterCount).isSelectable;
}

public ObjectgetItem(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)
returnmFooterViewInfos.get(adjPosition - adapterCount).data;
}

public longgetItemId(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 ViewgetView(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 intgetItemViewType(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 intgetViewTypeCount() {
if (mAdapter != null) {
return mAdapter.getViewTypeCount();
}
return1;
}

public voidregisterDataSetObserver(DataSetObserver observer) {
if (mAdapter != null) {
mAdapter.registerDataSetObserver(observer);
}
}

public voidunregisterDataSetObserver(DataSetObserver observer) {
if (mAdapter != null) {
mAdapter.unregisterDataSetObserver(observer);
}
}

publicFilter getFilter() {
if (mIsFilterable) {
return ((Filterable) mAdapter).getFilter();
}
return null;
}
publicListAdapter getWrappedAdapter() {
returnmAdapter;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值