嵌套滚动视图中 gridview 的大小问题

sizing issue of gridview in nested scrollview

you can see gridview in the image which is not wrapping whole nestedscrollview 伙计们请帮帮我,我被这段代码卡住了,我需要一个折叠工具栏布局,我也成功了,但是嵌套滚动视图中的 gridview 没有包含 space 个 nestedscrollview,我尝试了很多方法但都失败了。

这是 xml 帮助我折叠工具栏和我遇到 gridview 问题的地方,

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.patelsanket.myalbum.activities.AlbumImageDisplayActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/headerImage1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@string/shows_header_image"
                android:fitsSystemWindows="true"
                android:scaleType="fitXY"
                android:src="@drawable/cod" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<GridView

        android:id="@+id/album_image_grid_view"
        android:layout_width="match_parent"
        android:layout_height="550dip"
        android:choiceMode="multipleChoice"
        android:horizontalSpacing="0.5dp"
        android:numColumns="2"
        android:paddingBottom="20dip" />

</RelativeLayout>

</android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add"
        app:backgroundTint="#009688"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end|right" />

</android.support.design.widget.CoordinatorLayout>

添加此代码 如果 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { album_image_grid_view.setNestedScrollingEnabled(真); } 或者你可以使用 RecyclerView 而不是 GridView

查了很多,发现用可扩展的gridview就可以解决。我使用了以下 class 并且工作正常。

这是原生 GridView 的自定义实现, 在这里,我们覆盖了与 GridView 的高度相关的功能。 此实现取消了固定高度的 GridView ,而是 GridView 的高度根据添加到其中的子元素的数量而扩展。

public class ExpandableGridView extends GridView{

    boolean expanded = false;
    public boolean isExpanded()
    {
        return expanded;
    }

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

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {

        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(   
                    Integer.MAX_VALUE >> 2, 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;
    }

} 

RecyclerViewGridLayoutManager 结合使用。由于 Recycler 视图也与 Collasping bartoolbar 兼容...... 希望对你有帮助..