【问题标题】:GridView under Toolbar - Hide ToolBar when Scrolling工具栏下的 GridView - 滚动时隐藏工具栏
【发布时间】:2015-03-21 11:27:56
【问题描述】:

我的 Activity 有以下布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/content_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    <include layout="@layout/toolbar" />

</FrameLayout>

content_fragment 是我替换包含 gridView 的片段的地方:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.test.android.client.fragment.BurgerGridFragment">

        <GridView
            android:id="@+id/grid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:columnWidth="150dp"
            android:horizontalSpacing="@dimen/margin_extra_small"
            android:numColumns="auto_fit"
            android:padding="@dimen/padding_extra_small"
            android:scrollbars="none"
            android:stretchMode="columnWidth"
            android:verticalSpacing="@dimen/margin_extra_small" />

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

我的想法是,当我在 GridView 中滚动时,工具栏应该会随着动画出现/消失:

if (shown) {


view.animate()
                    .translationY(0)
                    .alpha(1)
                    .setDuration(HEADER_HIDE_ANIM_DURATION)
                    .setInterpolator(new DecelerateInterpolator());
        } else {
            view.animate()
                    .translationY(-view.getBottom())
                    .alpha(0)
                    .setDuration(HEADER_HIDE_ANIM_DURATION)
                    .setInterpolator(new DecelerateInterpolator());
        }

问题是第一个网格项目被工具栏部分重叠。 你知道如何解决这个问题吗?

附录:

我已经为我的 Activity 尝试了 RelativeLayout 和 LinearLayout 而不是 FrameLayout,但这不是解决方案,我遇到了另一个问题,如 https://stackoverflow.com/questions/29163698/hiding-toolbar-when-scrolling-layout-under-toolbar-does-not-disapear 所述

【问题讨论】:

    标签: android gridview toolbar material-design


    【解决方案1】:

    尝试将 FrameLayout 更改为具有垂直方向的 LinearLayout。 然后 Gridview 将放置在工具栏下方。

    【讨论】:

      【解决方案2】:

      您可以向 GridView 添加一个空标题,该标题将隐藏在您的自定义标题下以提供所需的间距。由于标准GridView 不支持标头,因此您需要使用HeaderGridView。这个小部件我已经用过好几次了,它总是很好用。

      由于您使用的是SwipeRefreshLayout,您还需要调整其进度视图位置。根据this answer,这可以通过setProgressViewOffset() 完成。

      【讨论】:

      • 然后它最终会出现类似的问题:stackoverflow.com/questions/29184409/…,所以这不是一个真正的解决方案。
      • 抱歉,没注意到.. 您希望在工具栏上方或下方设置刷新条纹吗?
      • 当然在下面,在网格/列表的顶部,但不是第一项,因为它是标题。
      • 编辑了我的答案,希望对您有所帮助
      • 我会尽快尝试一下,+1 为您提供帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多