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

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

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

将 View anchor 定到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>

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

解决方案:
这是瓦蒂姆建议的 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);
因此,当您将其下拉时,这将添加/删除teaserContainer的填充。

关于android - 将热门内容 anchor 定到BottomSheet,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53187969/

10-10 21:00