将顶部内容锚定到 BottomSheet

Anchor top content to BottomSheet

我有一个 FrameLayout (teaserContainer) 和 BottomSheet (invitesContainer)。 FrameLayout 位于 BottomSheet 之外(和上方)。我希望 FrameLayout 收缩并跟随 BottomSheet,因此 FremeLayout 在 BottomSheet 展开时折叠。

现在发生的事情是 FrameLayout 占据了整个页面,因为它 android:layout_height="match_parent" 但是如果我将它设置为 android:layout_height="wrap_content" 它显示在 BottomSheet 后面并且像 FAB 一样垂直居中。

我希望当 BottomSheet (invitesContainer) 完全展开时,FrameLayout (teaserContainer) 应该占据屏幕的其余部分,直到工具栏。

所有将视图锚定到 BottomSheet 的示例都涉及 FAB,因此这里对我没有任何帮助。

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".social.friends.FriendsListFragment">

    <FrameLayout
        android:id="@+id/teaserContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:fitsSystemWindows="true"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone"
        app:layout_anchor="@+id/invitesContainer"
        app:layout_anchorGravity="top">

        <com.myapp.android.common.social.friends.views.FriendsTeaser
            android:id="@+id/teaser"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            app:friendsTeaserState="EMPTY" />
    </FrameLayout>

    <LinearLayout
        android:id="@+id/invitesContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical"
        app:behavior_hideable="true"
        app:behavior_peekHeight="0dp"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:alpha="0.1"
            android:background="@color/body" />

        <com.myapp.android.common.social.friends.views.FriendsConnectItem
            android:id="@+id/connectFacebook"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:friendsConnectType="facebook" />

        <com.myapp.android.common.social.friends.views.FriendsConnectItem
            android:id="@+id/connectContacts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:friendsConnectType="contacts" />

        <com.myapp.android.common.social.friends.views.FriendsConnectItem
            android:id="@+id/share"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:friendsConnectType="invite" />
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

---------------------------------------- ---------------------------------------------- ------------------

解法: 这是 Vadim 建议的 Kotlin 版本

BottomSheetBehavior.from(invitesContainer).setBottomSheetCallback(object :
    BottomSheetBehavior.BottomSheetCallback() {
    override fun onStateChanged(bottomSheet: View, newState: Int) {
    }

    override fun onSlide(bottomSheet: View, slideOffset: Float) {
        val currentHeight = teaserContainer.height - bottomSheet.height
        val bottomSheetShiftDown = currentHeight - bottomSheet.top
        teaserContainer.setPadding(
            0,
            0,
            0,
            (bottomSheet.height + bottomSheetShiftDown)
        )
    }
})

我的例子是这样的:

public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        int currentHeight = teaserContainer.getHeight() - bottomSheet.getHeight();
        int bottomSheetShiftDown = currentHeight - bottomSheet.getTop();
        rootContainer.setPadding(
                0,
                0,
                0,
                (mBottomSheet.getPeekHeight() + bottomSheetShiftDown));
            }

mBottomSheet - BottomSheetBehavior mBottomSheet = BottomSheetBehavior.from(invitesContainer);

所以这将 add/remove 当你把它拉下来时你的 teaserContainer 的填充。