自定义GridView添加头部

这个博客介绍了如何在Android中自定义GridView并添加头部视图。通过创建FixedViewInfo类和使用HeaderViewGridAdapter,可以添加和管理头部视图,同时提供了添加头部视图的方法addHeaderView。此外,还覆盖了一些关键方法如onMeasure、setClipChildren等以支持头部视图。
摘要由CSDN通过智能技术生成

自定义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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值