【问题标题】:Nested scrollview inside gridview place is taking only somepartgridview 地方内的嵌套滚动视图只占用了一部分
【发布时间】:2015-10-26 01:50:45
【问题描述】:

我有: 1.协调器布局 2.appbar布局(Coordinator布局的子级) 3.折叠工具栏布局(应用栏的子级) 4.NestedScrollView(协调器的孩子)

我想NestedScrollView 中放置一个网格视图,以便用户可以滚动整个屏幕空间。

我的问题是当前 gridview 占据了NestedScrollView 的一小部分而不是NestedScrollView 的全部空间并在该部分内滚动,如下图所示:

如您所见,我的 gridview 高度仅限于天蓝色突出显示的部分,我希望它占据该图像下方的整个屏幕空间(这是我的折叠工具栏)。我尝试了不同的方法,但没有任何效果. 我的 xml 文件是:

<?xml version="1.0" encoding="utf-8"?>

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

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="196dp"
        android:background="#3f51b5"
        app:contentScrim="@color/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">


        <ImageView
            android:id="@+id/imageview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/index"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways"
            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">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"

        android:id="@+id/gridView"
        android:verticalSpacing="1dp"
        android:horizontalSpacing="1dp"
        android:numColumns="2"
        android:stretchMode="columnWidth"/>

</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_gravity="right|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

【问题讨论】:

    标签: android gridview nestedscrollview


    【解决方案1】:

    GridView 已经内置了滚动功能,因此它与NestedScrollView 冲突。您应该使用带有GridLayoutManagerappbar_scrolling_view_behavior 布局行为的RecyclerView 来代替NestedScrollView

    【讨论】:

    • 谢谢先生,但我是新手,所以你能用示例代码解释一下吗。
    【解决方案2】:

    我也有同样的问题,以下对我有用。

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

    【讨论】:

    • 它只解决了一半的问题 - 现在网格适合屏幕高度(视口)+ 应用栏高度偏移。但是参数“fillViewPort”会阻止您进一步扩展网格高度。
    【解决方案3】:

    确实,GridViewNestedScrollView 冲突,正确的解决方案是使用 RecyclerView,但我需要快速解决此类设置,并遇到使用自定义类扩展的解决方案GridView: https://gist.github.com/jiahuang/2591977

    然后您只需将 GridView 替换为自定义类并设置 NestedScrollView 的参数,如 Apoorv 的回答:

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

    【讨论】:

      【解决方案4】:

      Apoorv Singh's answer 帮助了我,但并不完全。您可以在 NestedScrollView 中放置一个 GridView,但只会显示第一行。您需要在 NestedScrollView 定义中添加 android:fillViewport="true" 才能正确显示所有内容。

      但是,当我向下滚动时,它不会滚动到屏幕底部。我有一个 10 行的网格,屏幕上没有多行,但我现在无法向下滚动查看它们。

      回答: 这现在对我有用。将 RecyclerView 与 GridLayoutManager 一起使用,而不是 GridView。由于回收站视图也与折叠工具栏兼容。当你在 NestedScrollView 中使用 RecyclerView 时,它运行良好。一切大小正确,滚动正确。

      【讨论】:

      • 我正在纠正 Apoorv 的回答。我确实回答了他的问题……他的问题是,当前 gridview 占据了 NestedScrollView 的一小部分,而不是 NestedScrollView 的全部空间并在该部分内滚动。他想知道如何解决这个问题。 Apoorv 的回答只是部分正确。正如我刚刚测试的那样,我现在的答案似乎是完全正确的。它可以帮助他解决尺寸问题,并且实际上可以让您正确滚动。
      • 是的,使用 Apoorv 的回答,它没有完全滚动。使用带有 GridLayoutManager 的 RecyclerView 而不是 GridView 确实可以让您完全滚动。如果有帮助,我从 asnwer 中删除了“任何想法”部分
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 2018-02-22
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多