Gridview show according to actual height within scroll view
Asked Answered
H

2

14
 <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/titleBarBG"
        android:layout_alignParentLeft="true" >

    <RelativeLayout
        android:id="@+id/scrollContent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         >

    <GridView
        android:id="@+id/issueList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/archiveTitle"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/customshape"
        android:numColumns="3"
        android:overScrollMode="never"
        android:scrollbars="none" >
    </GridView>

</RelativeLayout>
 </ScrollView>

I would like to create a gridview that act like a table . For example, the size of the grid will increase that will make the gridview taller. Instead of hide the extra content ,I would like the grid view show all content and expand the height when there is additional content

How to implement this? thanks

Hotfoot answered 13/12, 2013 at 7:57 Comment(4)
Why don't you want the scrolling to be provided by the GridView? What features of scrolling does the ScrollView give you that the GridView doesn't?Norvil
Thanks, the only problem is the height of gridview should be increase when the number of item in it is increasedHotfoot
You might be looking for [ExpandableHeightGridView][1] [1]: https://mcmap.net/q/162714/-gridview-height-gets-cut/…Norvil
Is it easier to dynamic set the layout parms (height) of the grid view? thanksHotfoot
P
25
public class MyGridView extends GridView {

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

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
Phylys answered 17/12, 2013 at 8:53 Comment(2)
You should explain a bit.Isomerize
Good work dude... Thanx for this code i try many this but i m not found anything at last i found this thank you againPrecursor
T
3

This is slightly cleaned up version of: Grid of images inside ScrollView

WrappedGridView.java:

/**
 * Use this class when you want a gridview that doesn't scroll and automatically
 * wraps to the height of its contents
 */
public class WrappedGridView extends GridView {
    public WrappedGridView(Context context) {
        super(context);
    }

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

Include in an XML layout like you would a GridLayout. Use an adapter to provide it views.

As far as I can tell, this is the simplest solution available now. There no other view available in the framework that handles wrapping. It would be nice if someone were to provide an elegant, automatically sizing table view. Modifying GridView.java for this purpose may not be a bad idea.

Alternatively, you may find one of the 'FlowLayout' projects acceptable. There is android-flowlayout and FlowLayout. These are a little more flexible than a simple grid and, I assume, a little less efficient. You also shouldn't need to provide them an adapter.

Ticket answered 17/12, 2013 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.