如何获得行的GridView控件的数量? [英] How to get the number of rows of a GridView?

查看:168
本文介绍了如何获得行的GridView控件的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让一个GridView的行数为Android API级别8?

Is there a way to get the row count of a GridView for Android API Level 8?

推荐答案

我必须解决这个问题,昨晚,它为我工作。它看起来一个孩子的宽度,并假设所有的细胞具有相同的宽度,通过孩子的宽度划分GridView的宽度。 ( getColumnWidth()也是较早的API不可用,所以因此解决方法)。

I had to solve this last night and it worked for me. It looks up a child's width and assuming all cells have the same width, divides the GridView's width by the child's width. (getColumnWidth() is also unavailable in earlier APIs, so hence the workaround).

private int getNumColumnsCompat() {
    if (Build.VERSION.SDK_INT >= 11) {
        return getNumColumnsCompat11();

    } else {
        int columns = 0;
        int children = getChildCount();
        if (children > 0) {
            int width = getChildAt(0).getMeasuredWidth();
            if (width > 0) {
                columns = getWidth() / width;
            }
        }
        return columns > 0 ? columns : AUTO_FIT;
    }
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private int getNumColumnsCompat11() {
    return getNumColumns();
}

不过也有需要注意的这一一些重要的限制。

However there are some important restrictions to note with this.

  • 在GridView控件的布局,只有当它实际上已经加入到它的一些看法它只会工作。
  • 在此不考虑与 setHorizo​​ntalSpacing添加任何单元格间距()。如果你使用的水平间距,你可能需要调整这个占了。
  • 在我来说,我没有任何垫或额外的宽度考虑到。如果你这样做,你将需要调整这个以确保该司结束了正确的答案。
  • It will only work after the GridView is laid out and only when it actually has some views added to it.
  • This doesn't take into account any cell spacing added with setHorizontalSpacing(). If you use horizontal spacing you may need to tweak this to account for it.
  • In my case I didn't have any paddings or additional width to account for. If you do, you will need to tweak this to ensure the division ends up with the right answer.

这篇关于如何获得行的GridView控件的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆