ScrollView折叠GridViews

时间:2018-09-28 16:25:21

标签: android xml gridview scrollview

我的布局如下:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Test">


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal">
            <TextView
                .../>
            <TextView
                .../>
        </LinearLayout>

        <GridView
            android:id="@+id/gridview_matrici"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:numColumns="3"
            android:padding="0dp"
            android:background="#ffffff"
            android:horizontalSpacing="3dp"
            android:verticalSpacing="3dp"
            android:stretchMode="columnWidth" >
        </GridView>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal">
            <TextView
                .../>
            <TextView
                .../>
        </LinearLayout>

        <GridView
            android:id="@+id/gridview_soluzioni"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:numColumns="4"
            android:padding="0dp"
            android:background="#ffffff"
            android:horizontalSpacing="3dp"
            android:verticalSpacing="3dp"
            android:stretchMode="columnWidth" >
        </GridView>
    </LinearLayout>
</ScrollView>

当我添加ScrollView时,GridView被折叠,仅显示一行。
我尝试了所有方法,但无法解决问题。
你能告诉我怎么办吗?
谢谢!



(它说我的帖子似乎主要是代码,但是我还能说什么?代码解释了所有问题!)

1 个答案:

答案 0 :(得分:0)

使用android.support.v4.widget.NestedScrollView代替ScrollView

ScrollViewLinearLayout(这是ScrollView的直接子元素)的高度更改为wrap_content

在您的项目中添加此类:

public class ExpandableHeightGridView extends GridView
{

    boolean expanded = false;

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

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

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

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // 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();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

按如下所示将其包含在您的布局中:

<com.example.ExpandableHeightGridView
    android:id="@+id/myId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:horizontalSpacing="2dp"
    android:isScrollContainer="false"
    android:numColumns="4"
    android:stretchMode="columnWidth"
    android:verticalSpacing="20dp" />

最后,您只需要让它扩展即可:

mAppsGrid = (ExpandableHeightGridView) findViewById(R.id.myId);
mAppsGrid.setExpanded(true);