简体   繁体   English

工具栏下的GridView - 滚动时隐藏工具栏

[英]GridView under Toolbar - Hide ToolBar when Scrolling

I have the following layout for my 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 is the place where I replace a fragment which includes a gridView: 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>

My idea is as soon I scroll in GridView, Toolbar should appears/disappears with animation: 我的想法是,一旦我在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());
        }

The problem is first grid items have partially overlapped by toolBar. 问题是第一个网格项与toolBar部分重叠。 Do you know how to solve the problem? 你知道如何解决这个问题吗?

Addenda: 附加物:

I have tried RelativeLayout as well as LinearLayout instead of FrameLayout for my Activity, but that will not be a solution and I get into another problem as described https://stackoverflow.com/questions/29163698/hiding-toolbar-when-scrolling-layout-under-toolbar-does-not-disapear 我已经为我的Activity尝试了RelativeLayout以及LinearLayout而不是FrameLayout,但这不是一个解决方案,我遇到了另一个问题,如https://stackoverflow.com/questions/29163698/hiding-toolbar-when-scrolling-布局下的工具栏-不-不disapear

try to change FrameLayout to LinearLayout with orientation vertical. 尝试将FrameLayout更改为LinearLayout,方向为vertical。 Then the Gridview will placed bellow the Toolbar. 然后Gridview将放置在工具栏下方。

You could add an empty header to your GridView that would stay hidden under your custom header to provide the desired spacing. 您可以向GridView添加一个空标题,该标题将隐藏在您的自定义标题下,以提供所需的间距。 Since standart GridView does not support headers, you would need to use HeaderGridView . 由于标准GridView不支持标题,因此您需要使用HeaderGridView I have used this widget several times and it always works well. 我已经多次使用过这个小部件了,它总是运行良好。

Since you are using SwipeRefreshLayout you will also need need to adjust its progress view position. 由于您使用的是SwipeRefreshLayout您还需要调整其进度视图位置。 According to this answer this can be done with setProgressViewOffset() . 根据这个答案,可以使用setProgressViewOffset()来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM