Is there a way to get the row count of a GridView for Android API Level 8?
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.
setHorizontalSpacing()
. If you use horizontal spacing you may need to tweak this to account for it.