7

我希望自定义视图在滚动期间通过过渡折叠。

我有AppBarLayout一个Toolbar里面。下面有一个我想折叠的自定义视图。

在自定义视图下面有一个NestedScrollViewwith LinearLayout

工具栏为绿色,自定义布局为粉红色,线性滚动为灰色:

工具栏自定义视图和

向下滚动后:

向下滚动

<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">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_marginTop="?attr/actionBarSize"
        android:background="@drawable/background"
        android:gravity="center">
    </RelativeLayout>

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="170dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include
            layout="@layout/linear"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </ScrollView>

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

我应该使用自定义行为和CoordinatorLayout/或NestedScroll使用翻译动画进行翻译吗?

4

2 回答 2

1

使用 Observable ScrollView 库 https://github.com/ksoichiro/Android-ObservableScrollView

于 2017-07-04T09:51:07.647 回答
1

我已经设法在没有任何库的情况下做到了这一点。关键是要有两个应用栏布局——一个带有占位符和折叠工具栏布局,隐藏在自定义视图下,另一个是普通的。

然后我创建了两种行为 - 一种用于更改嵌套布局高度,第二种用于在自定义视图中操作内部视图。

这是我的布局:

<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">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:titleEnabled="false">

        <View
            android:id="@+id/vieItemDetailsPlaceholder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00000000" />

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

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

<android.support.design.widget.AppBarLayout
    android:id="@+id/second_app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/second_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/detailContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="com.example.detail.view.Det ailScrollBehavior">

    <include
        layout="@layout/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp" />

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

<!-- second layout with behavior -->
<include layout="@layout/two_circles_layout" />

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

详细滚动行为:

public class DetailScrollBehavior extends Behavior<NestedScrollView> {

    private final Context context;

    public DetailScrollBehavior(Context context, AttributeSet attrs) {
        this.context = context;
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, NestedScrollView child, View dependency) {
        return dependency.getId() != R.id.ablItemDetailsSecondToolbar && dependency instanceof AppBarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, NestedScrollView child, View dependency) {
        int minHeight = context.getResources().getDimensionPixelSize(R.dimen.some_min_height);
        int placeholderHeight = getPlaceholderHeight(dependency);
        int actionBarHeight = getActionBarHeight(context.getTheme());

        if (placeholderHeight < minHeight + actionBarHeight) {
            placeholderHeight = minHeight + actionBarHeight;
        }

        child.setPadding(0, placeholderHeight, 0, 0);

        return true;
    }
}

和一部分two_circles_layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/relDetailsContainer"
    android:layout_width="match_parent"
    android:layout_height="@dimen/team_vs_details_height"
    android:layout_marginTop="?attr/actionBarSize"
    android:background="@drawable/background"
    android:gravity="center"
    app:layout_behavior="com.example.DetailBehavior"
    tools:showIn="@layout/activity_item_detail">


    [ ... ]

</RelativeLayout>
于 2017-07-20T14:12:48.503 回答