portfoliobuilder
portfoliobuilder

Reputation: 7856

Prevent GridView from scaling

How do I keep gridView from needing to scroll by auto adjusting it's height? I would like all items, no matter how many items I add to the gridView to remain on screen without scrolling. Is this possible?

Here is my UI so far.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:orientation="vertical">

        <GridView
            android:id="@+id/gv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/tv_header"
            android:fadingEdge="none"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="center"
            android:listSelector="#00000000"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth" />

</LinearLayout>

I did try adding a weightSum to the root and weight to gridView but it still requires scrolling.

Update: I also tried using a custom gridview. This did not work, but here is my attempt anyway.

public class CustomGridView extends GridView {

    public CustomGridView(Context context) {
        super(context);
    } 

    public CustomGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    } 

    public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST));
        getLayoutParams().height = getMeasuredHeight();
    } 
} 

Thanks in advance!

Upvotes: 0

Views: 100

Answers (1)

portfoliobuilder
portfoliobuilder

Reputation: 7856

I have found answer to this. You can set the height of each item in the adapter by using

view.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, resizeValue));

resizeValue is the size that you want to adjust your rows to. To get resizeValue you can pass to the adapter mResizeValue based on the calculations relative to your device screen size. Something like

resizevalue = this.getResources().getDisplayMetrics().heightPixels / (NUM_COLS);

I figured out some other ways of calculating the height of each row based on screen size and then doing something similar, however, this requires that you do these calculations after you set your adapter and then update the changes to the adapter. It seems less efficient but I will share that methodology as well.

private void resizeGridView(GridView gridView, int items, int columns) {
    ViewGroup.LayoutParams params = gridView.getLayoutParams();
    int oneRowHeight = gridView.getHeight();
    int rows = (int) (items / columns);
    params.height = oneRowHeight * rows;
    gridView.setLayoutParams(params);
} 

Then after you set your adapter use

gridView.getViewTreeObserver().addOnGlobalLayoutListener(new 

    ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (!gridViewResized) {
                        gridViewResized = true;
                        resizeGridView(gridView, numItems, numColumns);
                    }
                }
            });

Upvotes: 1

Related Questions