将热门内容锚定到BottomSheet

时间:2018-11-07 10:49:40

标签: android bottom-sheet

我有一个FrameLayout(teaserContainer)和BottomSheet(invitesContainer)。 FrameLayout在BottomSheet的上方(和上方)。我希望FrameLayout缩小并跟随BottomSheet,所以FremeLayout随着BottomSheet的展开而折叠。

现在发生的是,由于android:layout_height="match_parent"导致FrameLayout占据了整个页面,但是如果我将其设置为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>

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

解决方案: 这是瓦迪姆建议的科特林版本

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)
        )
    }
})

1 个答案:

答案 0 :(得分:3)

我的例子是:

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);

因此,当您拉下它时,它将添加/删除您的teaserContainer的填充。