HeaderGridView——可以添加HeaderView的GridView,已修复HeaderView偏移的BUG

本文介绍了如何基于Google的HeaderGridView修复其在添加HeaderView后出现的偏移bug。通过分析和修改源码,特别是在FullWidthFixedViewLayout类中重写onLayout方法,成功解决了这个问题。并提供了相关参考资料链接。
摘要由CSDN通过智能技术生成
  • 基于Google提供的HeaderGridView,修复其HeaderView偏移的bug
  • Refer

一、基于Google提供的HeaderGridView,修复其HeaderView偏移的bug

最近在做一个需求用到了PullToRefreshGridView,其中的GridView也就是普通的GridView,并没有像ListView中那样的addHeaderView()或者addFooterView()方法,但是实际开发中又会碰到这样的需求。

于是在网上搜到HeaderGridView这个Widget,而且是谷歌官方提供的,集成运行后发现HeaderView会发生偏移,而代码中并没有设置Gravity。

HeaderGridView bug可查看Google的issues:

https://code.google.com/p/android/issues/detail?id=74520

最后找到android-GridViewWithHeaderAndFooter,该项目解决了HeaderView偏移的bug,于是查看其源码研究其如何解决HeaderView偏移的bug,发现其在FullWidthFixedViewLayout类中重写onLayout方法,代码如下:

    /**
     * @author zhangbiaojiang
     * @功能 解决HeaderView偏移的bug
     * @时间 2017/06/09
     */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int realLeft = HeaderGridView.this.getPaddingLeft() + getPaddingLeft();
        // Try to make where it should be, from left, full width
        if (realLeft != left) {
            offsetLeftAndRight(realLeft - left);
        }
        super.onLayout(changed, left, top, right, bottom);
    }

基于Google提供的HeaderGridView,修复其HeaderView偏移的bug。修复后的HeaderGridView源码如下:

import android.content.Context;
import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.WrapperListAdapter;

import java.util.ArrayList;

/**
 * A {@link GridView} that supports adding header rows in a
 * See {@link HeaderGridView#addHeaderView(View, Object, boolean)}
 */
public class HeaderGridView extends GridView {
   
    private static final String TAG = "HeaderGridView";
    /**
     * 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 ArrayList<FixedViewInfo> mHeaderViewInfos = new ArrayList<FixedViewInfo>();
    private void initHeaderGridView() {
        super.setClipChildren(false);
    }
    public HeaderGridView(Context context) {
        super(context);
        initHeaderGridView();
    }
    public HeaderGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initHeaderGridView();
    }
    public HeaderGridView(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(getNumColumns());
        }
    }
    @Override
可以使用以下代码添加UICollectionViewheaderView: ``` - (void)viewDidLoad { [super viewDidLoad]; // 初始化UICollectionViewFlowLayout UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = CGSizeMake(100, 100); layout.minimumLineSpacing = 10; layout.minimumInteritemSpacing = 10; layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 50); // 初始化UICollectionView UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout]; collectionView.dataSource = self; collectionView.delegate = self; [self.view addSubview:collectionView]; // 注册UICollectionViewCell [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; // 注册UICollectionReusableView [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"]; } #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 10; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor redColor]; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath]; headerView.backgroundColor = [UIColor blueColor]; return headerView; } return nil; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值