如何使用自定义CoordinatorLayout行为

时间:2016-01-08 16:50:30

标签: android android-coordinatorlayout

当同一片段中的View动画时,我想隐藏FragmentViewPager中使用的RecyclerView的底部对齐。

我可以将AppBarLayout.ScrollingViewBehavior Behavior应用于视图,让View以隐藏方式启动,然后在向下滚动时设置动画效果。向上滚动时,视图会隐藏。 (此行为与TabLayout工具栏与scroll | enterAlways scrollFlags集合的行为相匹配。)

我想要的是与此相反的。 View以可见的方式开始,向下滚动以在向下滚动时隐藏自身,然后在向上滚动时再次显示自己。

这可能吗?

我的活动XML:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <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.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabBackground="@color/colorPrimary"
            app:tabGravity="fill"
            app:tabMode="fixed"/>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
        <!-- This is where I set the behavior for the fragment used in the viewpager-->

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        android:src="@drawable/ic_done"
        app:borderWidth="0dp"
        app:elevation="2dp"/>

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

My Fragment XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivityFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/text_view"/>

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/holo_red_light"
        android:gravity="center"
        android:padding="10dp"
        android:text="Bottom Bar Test"
        android:textSize="30sp"/>

</RelativeLayout>

由于 扎克

更新1: 通过使用以下自定义Behavior类,我接近了所需的行为。滚动时,底栏会向右滚动!但是,视图顶部还会出现一个空白区域,其大小设置为当前隐藏的底部视图的高度。我不认为动画应该以这种方式使用,也许?

public class BottomBarScrollOffBehavior extends AppBarLayout.ScrollingViewBehavior
{
    private int appBarHeight;

    public BottomBarScrollOffBehavior()
    {
    }

    public BottomBarScrollOffBehavior(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency)
    {
        appBarHeight = dependency.getMeasuredHeight();
        return super.onDependentViewChanged(parent, child, dependency);
    }

    @Override
    public boolean setTopAndBottomOffset(int offset)
    {
        return super.setTopAndBottomOffset(appBarHeight - offset);
    }
}

0 个答案:

没有答案