有谁用过android的 HeaderGridView吗? 为什么我添加的HeaderView总是歪的?

带头部的GridView,HeaderGridView 源代码在此:
https://android.googlesource.com/platform/packages/apps/Gallery2/+/idea133/src/com/android/photos/views/HeaderGridView.java

是Google自己开发的,按说不会有错

我的代码非常简单:
先是.rl_headerview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_header_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <TextView 
    android:id="@+id/tv_main_helloworld"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="1,2,3,4,5,6,7,8,9,10,11,12"
    android:textSize="20sp"
    android:textColor="#ff000000"
    android:background="#ff00ff00"/>

</RelativeLayout>



再是MainActivity中的代码:
friendsArrayList = new ArrayList<String>();

friendsArrayList.add("关羽");
friendsArrayList.add("张飞");
friendsArrayList.add("赵云");
friendsArrayList.add("马超");
friendsArrayList.add("黄忠");
friendsArrayList.add("胃炎");


               hgv_main = (HeaderGridView) findViewById(R.id.hgv_main);

LayoutInflater inflater =  (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);

View llView = inflater.inflate(R.layout.rl_headerview, null);
ArrayAdapter<String> friendsadapter = new ArrayAdapter<String>(this,//上下文
android.R.layout.simple_list_item_1,
friendsArrayList);//数据


hgv_main.addHeaderView(llView);

hgv_main.setNumColumns(2);
hgv_main.setAdapter(friendsadapter);


但是出来的结果却是这样的,整个Header是歪的


而且我发现只有当我将每一行的NumColumns设成1时,Header部分才是正常的,NumColumns越大,歪的越厉害。

是否有人使用过这个HeaderGridView这个类?是否也发现过同样的问题?请指教!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下代码添加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; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值