自定义GridView添加头部
// private int firstVisibleItem;
// private int visibleItemCount;
//
// /**
// * A class that represents a fixed view in a list, for example a header at
// * the top or a footer at the bottom.
// */
// private static class FixedViewInfo {
// /**
// * The view to add to the grid
// */
// public View view;
// public ViewGroup viewContainer;
// /**
// * The data backing the view. This is returned from
// * {@link ListAdapter#getItem(int)}.
// */
// public Object data;
// /**
// * <code>true</code> if the fixed view should be selectable in the grid
// */
// public boolean isSelectable;
// }
//
// private int mNumColumns = AUTO_FIT;
// private View mViewForMeasureRowHeight = null;
// private int mRowHeight = -1;
// private static final String LOG_TAG = "grid-view-with-header-and-footer";
//
// private ArrayList<FixedViewInfo> mHeaderViewInfos = new ArrayList<FixedViewInfo>();
// private ArrayList<FixedViewInfo> mFooterViewInfos = new ArrayList<FixedViewInfo>();
//
// private void initHeaderGridView() {
// }
//
// public CallGrideView(Context context) {
// super(context);
// initHeaderGridView();
// }
//
// public CallGrideView(Context context, AttributeSet attrs) {
// super(context, attrs);
// initHeaderGridView();
// }
//
// public CallGrideView(Context context, AttributeSet attrs, int defStyle) {
// super(context, attrs, defStyle);
// initHeaderGridView();
// }
//
// @Override
// protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// ListAdapter adapter = getAdapter();
// if (adapter != null && adapter instanceof HeaderViewGridAdapter) {
// ((HeaderViewGridAdapter) adapter).setNumColumns(getNumColumnsCompatible());
// ((HeaderViewGridAdapter) adapter).setRowHeight(getRowHeight());
// }
// }
//
// @Override
// public void setClipChildren(boolean clipChildren) {
// // Ignore, since the header rows depend on not being clipped
// }
//
// /**
// * Do not call this method unless you know how it works.
// *
// * @param clipChildren
// */
// public void setClipChildrenSupper(boolean clipChildren) {
// super.setClipChildren(false);
// }
//
// /**
// * Add a fixed view to appear at the top of the grid. If addHeaderView is
// * called more than once, the views will appear in the order they were
// * added. Views added using this call can take focus if they want.
// * <p/>
// * NOTE: Call this before calling setAdapter. This is so HeaderGridView can
// * wrap the supplied cursor with one that will also account for header
// * views.
// *
// * @param v
// * The view to add.
// */
// public void addHeaderView(View v) {
// addHeaderView(v, null, true);
// }
//
// /**
// * Add a fixed view to appear at the top of the grid. If addHeaderView is
// * called more than once, the views will appear in the order they were
// * added. Views added using this call can take focus if they want.
// * <p/>
// * NOTE: Call this before calling setAdapter. This is so HeaderGridView can
// * wrap the supplied cursor with one that will also account for header
// * views.
// *
// * @param v
// * The view to add.
// * @param data
// * Data to associate with this view
// * @param isSelectable
// * whether the item is selectable
// */
// public void addHeaderView(View v, Object data, boolean isSelectable) {
// ListAdapter adapter = getAdapter();
// if (adapter != null && !(adapter instanceof HeaderViewGridAdapter)) {
// throw new IllegalStateException("Cannot add header view to grid -- setAdapter has already been called.");
// }
//
// ViewGroup.LayoutParams lyp = v.getLayoutParams();
//
// FixedViewInfo info = new FixedViewInfo();
// FrameLayout fl = new FullWidthFixedViewLayout(getContext());
//
// if (lyp != null) {
// v.setLayoutParams(new FrameLayout.LayoutParams(lyp.width, lyp.height));
// fl.setLayoutParams(new LayoutParams(lyp.width, lyp.height));
// }
// fl.addView(v);
// info.view = v;
// info.viewContainer = fl;
// info.data = data;
// info.isSelectable = isSelectable;
// mHeaderViewInfos.add(info);
// // in the case of re-adding a header view, or adding one later on,
// // we need to notify the observer
// if (adapter != null) {
// ((HeaderViewGridAdapter) adapter).notifyDataSetChanged();
// }
// }
//
// public void addFooterView(View v) {
// addFooterView(v, null, true);
// }
//
// public void addFooterView(View v, Object data, boolean isSelectable) {
// ListAdapter mAdapter = getAdapter();
// if (mAdapter != null && !(mAdapter instanceof HeaderViewGridAdapter)) {
// throw new IllegalStateException("Cannot add header view to grid -- setAdapter has already been called.");
// }
//
// ViewGroup.LayoutParams lyp = v.getLayoutParams();
//
// FixedViewInfo info = new FixedViewInfo();
// FrameLayout fl = new FullWidthFixedViewLayout(getContext());
//
// if (lyp != null) {
// v.setLayoutParams(new FrameLayout.LayoutParams(lyp.width, lyp.height));
// fl.setLayoutParams(new LayoutParams(lyp.width, lyp.height));
// }
// fl.addView(v);
// info.view = v;
// info.viewContainer = fl;
// info.data = data;
// info.isSelectable = isSelectable;
// mFooterViewInfos.add(info);
//
// if (mAdapter != null) {
// ((HeaderViewGridAdapter) mAdapter).notifyDataSetChanged();
// }
// }
//
// public int getHeaderViewCount() {
// return mHeaderViewInfos.size();
// }
//
// public int getFooterViewCount() {
// return mFooterViewInfos.size();
// }
//
// /**
// * Removes a previously-added header view.
自定义GridView添加头部
最新推荐文章于 2021-05-28 12:42:46 发布
这个博客介绍了如何在Android中自定义GridView并添加头部视图。通过创建FixedViewInfo类和使用HeaderViewGridAdapter,可以添加和管理头部视图,同时提供了添加头部视图的方法addHeaderView。此外,还覆盖了一些关键方法如onMeasure、setClipChildren等以支持头部视图。
摘要由CSDN通过智能技术生成