CollapsingToolbarLayout在Android中将NestedScrollView向下推 [英] CollapsingToolbarLayout pushes NestedScrollView down in android

查看:43
本文介绍了CollapsingToolbarLayout在Android中将NestedScrollView向下推的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在第一次启动片段时遇到了一个问题.看起来CoordinatorLayout向NestedScrollView添加了负边距,该边距等于CollapsingToolbarLayout在折叠状态下的高度(我通过更改高度值对其进行了测试).结果,位于NestedScrollView中的RecyclerView无法向上滚动以显示其底部的几个项目.

I encountered a problem on first launch of a fragment. It seems like CoordinatorLayout adds a negative margin to NestedScrollView which is equal to CollapsingToolbarLayout's height in collapsed state (I tested it by changing the height's value). As a result RecyclerView which lives in the NestedScrollView cannot scroll up to show few of its bottom items.

SO上也有类似的问题(例如

There are some similar questions on SO (like this, this and this), but they don't provide a solution that worked for me, and moreover, they don't give any explanations of what can cause the issue.

一个有趣的注意事项是,如果我旋转屏幕,或者关闭屏幕然后再打开屏幕,它将重新构建布局,并添加后记,它将正确显示.另外,如果我单击某个项目,它将触发一些操作,因此我将能够滚动那些隐藏的项目.作为一种解决方法,最好是调用一个可以正确正确重建布局的函数,但是我无法弄清楚它是什么(请参见下文尝试执行的操作)

One interesting note is that if I rotate, or turn the screen off and on it would rebuild the layout and afterwords it will show up correctly. Also, if I click on an item it will trigger something so then I would be able to scroll those hidden items. As a workaround it would be nice to call a function that rebuilds the layout correctly manually but I failed to figure out what it was (see what I tried to do below)

我试图做的事情:

  • 在NestedScrollView中添加等于工具栏高度的下边距.尽管它在首次启动时有所帮助,但是当我单击某个项目或旋转屏幕后,多余的边距将使视图从屏幕底部向上推.

  • adding bottom margin equal to toolbar's height to NestedScrollView. Although it helped on first launch but after I clicked on an item or rotate the screen the extra margin would push the view up from the bottom of the screen.

调试时我在代码中找不到任何问题.

I couldn't find any problem in my code while debugging.

调用getView.invalidate()也无济于事.

calling getView.invalidate() didn't help as well.

请问有人可以帮助我找出导致问题的原因吗?

Can somebody help me to figure out what can cause the issue, please?

fragment_player.xml

fragment_player.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="ru.orgin.glagol.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:stateListAnimator="@animator/appbar_always_elevated">

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

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

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/player_toolbar_height"
                app:layout_collapseMode="none"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

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

        <ViewAnimator
            android:id="@+id/viewAnimator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inAnimation="@android:anim/fade_in"
            android:outAnimation="@android:anim/fade_out">

            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="64dp"
                android:layout_gravity="center"
                style="?android:attr/progressBarStyle"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:nestedScrollingEnabled="false"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:gravity="center"
                android:layout_gravity="center"
                android:text="@string/empty_history_books" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:gravity="center"
                android:layout_gravity="center"
                android:text="@string/error_loading_data" />

        </ViewAnimator>

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

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

更新:仍然不知道原因,但是似乎将 minHeight 属性添加到 CollapsingToolbarLayout 即可达到目的.

Update: Still don't know the reason but it seems like adding minHeight attribute to CollapsingToolbarLayout does the trick.

推荐答案

添加 minHeight 属性可防止CollapsingToolbarLayout发生意外行为:

Adding minHeight attribute prevents CollapsingToolbarLayout from unexpected behavior:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:stateListAnimator="@animator/appbar_always_elevated">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:minHeight="@dimen/player_toolbar_height"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:titleEnabled="false">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/player_toolbar_height"
            app:layout_collapseMode="none"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

这篇关于CollapsingToolbarLayout在Android中将NestedScrollView向下推的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆