Luke Allison
Luke Allison

Reputation: 3218

Dynamically resizing a gridView

Months ago I successfully wrote code to resize a gridView to fit the amount of rows required of it each time a new table of data was to be loaded. I've just noticed that this is no longer working and I have no idea why. All that has changed as far as I can tell is that I've upgraded the SDK. Below is the code that used to dynamically resize the gridView.

/**
 * create a resizable gridView by utilizing the onLayoutChanged system method
 * @param items an arrayList containing the items to be read into each cell
 */
public void createGridView(ArrayList<String> items)
{
    Log.d(TAG, "createGridView(): ");
    gridView.setAdapter(new GridAdapter(items));
    gridView.addOnLayoutChangeListener(new View.OnLayoutChangeListener(){

        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            Log.d(TAG, "onLayoutChange(): ");
            currentGridHeight = gridView.getHeight();
            if (singleGridHeight == 0){
                singleGridHeight = currentGridHeight/currentGridRows;
            }
            // fix for nestedScrollView automatically scrolling to the bottom after swipe
            nestedScrollView.scrollTo(0, 0);
        }
    });
}

/**
 * re-sizes the height of the gridView based on the amount of rows to be added
 * @param gridView
 * @param items
 * @param columns
 */
private static void resizeGridView(GridView gridView, int items, int columns) {
    Log.d(TAG, "resizeGridView(): ");
    ViewGroup.LayoutParams params = gridView.getLayoutParams();
    params.height = singleGridHeight * items;
    gridView.setLayoutParams(params);
    gridView.requestLayout();
}

// gridView adapter
private static final class GridAdapter extends BaseAdapter {
    final ArrayList<String> mItems;
    final int mCount;

    /**
     * Default constructor
     *
     * @param items to fill data to
     */
    private GridAdapter(final ArrayList<String> items) {
       // create arrayList full of data called "items"
        // ..
        // ..

        // resizing the gridView based on the number of rows
        currentGridRows = items.size();
// if this isn't the very first gridView then resize it to fit the rows
        if (currentGridHeight != 0){
            resizeGridView(gridView,items.size(),6);
        }
    }

When the gridView is first created (the very first table loaded) I get the measurement of the height of the gridView and divide it by the number of rows in order to determine the height required per row. When I load subsequent gridViews I resize them by multiplying this height by the number of rows.

This is no longer working. Subsequent gridViews are still roughly only big enough to hold one row.

Any ideas?

EDIT: It appears that it is calculating the singleRowHeight as 9dp when in fact each row requires about 64dp.

Upvotes: 0

Views: 1253

Answers (1)

Skullper
Skullper

Reputation: 775

Try to use RecyclerView with LayoutManager like this:

recyclerView.setLayoutManager(new GridLayoutManager(context, columnsCount));

Upvotes: 2

Related Questions