具有回收者视图的持久底部工作表

And*_*Dev 5 android android-recyclerview android-design-library android-coordinatorlayout bottom-sheet

我有一个可调整的视图,在协调器布局中有持久的底部工作表.

当显示底部工作表时,是否可以自动将底部边距调整为可滚动视图?否则底部工作表在可滚动视图中重叠某些项目.

可滚动视图中的属性layout_marginBottom并不总是适用,因为底部工作表可能处于隐藏状态.

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="Item 1"
                android:gravity="center"
                android:textColor="@android:color/black"
                android:background="#ffbb3d"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="Item 2"
                android:gravity="center"
                android:textColor="@android:color/black"
                android:background="#a4ff3d"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="Item 3"
                android:gravity="center"
                android:textColor="@android:color/black"
                android:background="#3d77ff"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <FrameLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:clipToPadding="true"
        android:background="#e6e6e6"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Bottom sheet title"
            android:padding="16dp"
            android:textSize="16sp"/>
    </FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

在此示例布局中,"项目3"始终位于底部工作表的下方.

更新

我在NestedScrollView中找到了使用自定义布局行为的解决方案.

例如,底部工作表布局应该从某些"标记类"扩展

public class BottomSheetFrameLayout extends FrameLayout { 
//... 
}
Run Code Online (Sandbox Code Playgroud)

否则,您可以通过id检测底部工作表.

在自定义行为中,我们可以检查BottomSheetFrameLayout的依赖关系实例,并在这种情况下附加一些底部边距或转换到视图:

public class BottomSheetScrollingViewBehavior extends AppBarLayout.ScrollingViewBehavior {
// ...

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return dependency instanceof BottomSheetFrameLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        // Add bottom margins or translation to view
    }
}
Run Code Online (Sandbox Code Playgroud)