在嵌套的scrollview中调整gridview的问题

时间:2015-10-20 10:16:17

标签: nestedscrollview

you can see gridview in the image which is not wrapping whole nestedscrollview 伙计们请帮助我,我被困在这段代码中,我需要一个折叠的工具栏布局,我也成功了但是嵌套的scrollview里面的gridview没有包装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>

3 个答案:

答案 0 :(得分:3)

RecyclerView GridLayoutManager 一起使用。由于Recycler视图也适用于Collasping bartoolbar ... 我希望它有所帮助..

答案 1 :(得分:2)

经过大量搜索,我发现使用可扩展的gridview可以解决它。我使用了下面的类,它工作正常。

这是本机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;
    }

} 

答案 2 :(得分:0)

添加此代码         if(Build.VERSION.SDK_INT&gt; = Build.VERSION_CODES.LOLLIPOP){             album_image_grid_view.setNestedScrollingEnabled(真);         } 或者您可以使用RecyclerView而不是GridView