更改AppBarLayout高度使列表低于"跳跃"大幅

时间:2017-07-12 15:08:31

标签: java android android-coordinatorlayout android-appbarlayout

我切换到CoordinatorLayout / AppBarLayout主要是为了利用app:layout_scrollFlags="scroll|enterAlways"(向上滚动时查看从顶部偷看)。我使用RecyclerView没有NestedScrollLayout作为第二个阻止视图回收,加载大量项目后它开始滞后。

"偷看" view是AppBarLayout,点击后会调整大小以显示/隐藏过滤器选项。一切都按预期工作,除了调整AppBarLayout的大小(通过设置子视图可见性)时,似乎整个布局都被渲染,仅仅RecyclerView是"跳跃&# 34;到顶部,然后回到预期的行为。它非常快,但非常烦人。当用LinearLayout替换时,它不会发生,但显然该视图并不可见。

我的布局:

<?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"
    android:id="@+id/fragment_notifications"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:animateLayoutChanges="true">

    <android.support.design.widget.AppBarLayout android:id="@+id/fragment_notifications_filter_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:animateLayoutChanges="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true"
            app:layout_scrollFlags="scroll|enterAlways">

            <!--Two example views with different heights, of which only one is visible at a time -->
            <ImageView android:id="@+id/view1"
                android:layout_width="match_parent"
                android:layout_height="68dp"
                android:src="@drawable/ic_launcher"/>

            <ImageView android:id="@+id/view2"
                android:layout_width="match_parent"
                android:layout_height="44dp"
                android:src="@drawable/ic_launcher"/>
            ...
        </RelativeLayout>

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

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

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/fragment_notifications_notifications"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scrollbars="vertical" />

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

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

Java代码非常简单;只需在AppBarLayout

中设置视图的可见性
someViewInsideAppBarLayout.setVisibility(visible ? View.VISIBLE : View.GONE);

知道怎么处理这个吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我必须在LayoutTransition.CHANGING中启用具有appbar滚动行为的ViewGroup类型的转换(在我的情况下,它是SwipeRefreshLayout)。

LayoutTransition layoutTransition = swipeRefreshLayout.getLayoutTransition();
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);

后来我遇到了额外的奇怪的动画行为,通过将消失的视图的父项的LayoutTransition.CHANGE_DISAPPEARING值更改为0(最初为300)来解决:

relativeLayout.getLayoutTransition().setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0);
appBar.getLayoutTransition().setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0);
coordinatorLayout.getLayoutTransition().setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0);