import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
/**
* 含有头布局和脚布局的ListViewAdapter
*/
public abstract class HeaderAndFooterListViewAdapter extends BaseAdapter {
public static final int ITEM_TYPE_HEADER = 0;
public static final int ITEM_TYPE_CONTENT = 1;
public static final int ITEM_TYPE_FOOTER = 2;
public abstract int getHeaderCount();
public abstract int getContentItemCount();
public abstract int getFooterCount();
public abstract View getHeaderView(int position, View convertView, ViewGroup parent);
public abstract View getContentItemView(int position, View convertView, ViewGroup parent);
public abstract View getFooterView(int position, View convertView, ViewGroup parent);
public Object getHeaderItem(int position) {
return null;
}
public Object getContentItem(int position) {
return null;
}
public Object getFooterItem(int position) {
return null;
}
public long getHeaderId(int position) {
return getHeaderPosition(position);
}
public long getContentItemId(int position) {
return getContentItemPosition(position);
}
public long getFooterId(int position) {
return getFooterPosition(position);
}
public boolean isHeaderView(int position) {
int headerCount = getHeaderCount();
return headerCount != 0 && position < headerCount;
}
public boolean isFooterView(int position) {
return getFooterCount() != 0 && position >= getHeaderCount() + getContentItemCount();
}
public boolean isContentItemView(int position) {
return !isHeaderView(position) && !isFooterView(position);
}
public int getHeaderPosition(int position) {
if (isHeaderView(position)) {
return position;
} else {
throw new IllegalArgumentException("position : " + position + " is not a header view!");
}
}
public int getContentItemPosition(int position) {
if (isContentItemView(position)) {
return position - getHeaderCount();
} else {
throw new IllegalArgumentException("position : " + position + " is not a content item view!");
}
}
public int getFooterPosition(int position) {
if (isFooterView(position)) {
return position - getHeaderCount() - getContentItemCount();
} else {
throw new IllegalArgumentException("position : " + position + " is not a footer view!");
}
}
@Override
public int getCount() {
return getHeaderCount() + getContentItemCount() + getFooterCount();
}
@Override
public int getItemViewType(int position) {
if (isHeaderView(position)) {
return ITEM_TYPE_HEADER;
} else if (isFooterView(position)) {
return ITEM_TYPE_FOOTER;
} else {
return ITEM_TYPE_CONTENT;
}
}
/**
* @param position
* @return
* @deprecated Use {@link #getHeaderItem(int)}, {@link #getContentItem(int)} or {@link #getFooterItem(int)} instead.
*/
@Override
public Object getItem(int position) {
if (isHeaderView(position)) {
int pos = getHeaderPosition(position);
return getHeaderItem(pos);
} else if (isFooterView(position)) {
int pos = getFooterPosition(position);
return getFooterItem(pos);
} else {
int pos = getContentItemPosition(position);
return getContentItem(pos);
}
}
/**
* @param position
* @return
* @deprecated Use {@link #getHeaderId(int)}, {@link #getContentItemId(int)} or {@link #getFooterId(int)} instead.
*/
@Override
public long getItemId(int position) {
if (isHeaderView(position)) {
return getHeaderId(position);
} else if (isFooterView(position)) {
return getFooterId(position);
} else {
return getContentItemId(position);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (isHeaderView(position)) {
int pos = getHeaderPosition(position);
return getHeaderView(pos, convertView, parent);
} else if (isFooterView(position)) {
int pos = getFooterPosition(position);
return getFooterView(pos, convertView, parent);
} else {
int pos = getContentItemPosition(position);
return getContentItemView(pos, convertView, parent);
}
}
@Override
public int getViewTypeCount() {
return 3;
}
}
HeaderAndFooterListViewAdapter
最新推荐文章于 2021-01-12 15:36:50 发布