I am trying to change gridView column number. I call setNumColumn() and invalidateViews() to update the view.
However, the grid\'s cell width w
May be you could get help through this nice sample offered by Google http://developer.android.com/shareables/training/BitmapFun.zip The following code which locate in the ImageGridFragment.java is used to handle column number change dynamically during runtime such as screen orientation change when rotate the device:
Here is the comments:This listener is used to get the final width of the GridView and then calculate the number of columns and the width of each column. The width of each column is variable as the GridView has stretchMode=columnWidth. The column width is used to set the height of each view so we get nic square thumbnails.
mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (mAdapter.getNumColumns() == 0) {
final int numColumns = (int) Math.floor(
mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
if (numColumns > 0) {
final int columnWidth =
(mGridView.getWidth() / numColumns) - mImageThumbSpacing;
mAdapter.setNumColumns(numColumns);
mAdapter.setItemHeight(columnWidth);
if (BuildConfig.DEBUG) {
Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
}
}
}
}
});
return v;