首页 > 解决方案 > 扩展持久性底页时调整回收站视图高度

问题描述

我有一个持久的底部工作表(基本上是一个按钮)和一个回收器视图,它们都包含在 CoordinatorLayout 中。

当底部表展开时,我不希望它遮挡回收站视图。我可以通过分别app:layout_insetEdge="bottom"在底部工作表和app:layout_dodgeInsetEdges="bottom"回收站视图中进行设置来实现这一点。

但是,由于回收站视图的高度设置为android:layout_height="match_parent",因此当底部工作表展开时,其顶部会部分移出屏幕。

相反,我希望回收站视图根据底部工作表的高度动态调整其高度,使其不再移出屏幕。我怎样才能做到这一点?

这是完整的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    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="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:nestedScrollingEnabled="false"
    app:layout_dodgeInsetEdges="bottom" />

<Button
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/update_all"
    android:foreground="?attr/selectableItemBackground"
    android:background="@drawable/angled_button"
    app:behavior_hideable="false"
    app:behavior_peekHeight="0dp"
    app:layout_insetEdge="bottom"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior" />

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

编辑:添加截图

没有底板,一切看起来都很好。 在此处输入图像描述

随着底部工作表的展开,回收站视图不再完全可见。 在此处输入图像描述

编辑 2:添加了 GIF

在此处输入图像描述

标签: androiduser-interfacematerial-designandroid-coordinatorlayoutbottom-sheet

解决方案


最近面临同样的问题,没有找到比删除app:layout_dodgeInsetEdges="bottom"和使用填充更好的方法。这是如何实现的:

科特林:

val rv = findViewById<RecyclerView>(R.id.recycler_view))
val behavior = BottomSheetBehavior.from(findViewById<Button>(R.id.bottom_sheet))
behavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback(){
    override fun onSlide(bottomSheet: View, offset: Float) {
        rv.setPadding(0, 0, 0, (bottomSheet.height * offset).toInt())
    }

    override fun onStateChanged(bottomSheet: View, newState: Int){}
})

爪哇:

RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view); 
BottomSheetBehavior behavior = BottomSheetBehavior.from(findViewById(R.id.bottom_sheet));
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        rv.setPadding(0, 0, 0, (int)(slidingView.getHeight() * offset));
    }
});

优点:

  • 屏幕上没有重叠,无论是使用工具栏还是使用 BottomSheet 按钮;
  • 不会破坏 FAB 的上移功能;
  • RecyclerView scrolling can be done along with sliding up the BottomSheet;

Cons:

  • Not pure XML, coding required;
  • Additional changes are required to keep original padding (but still possible and relatively easy to do)

推荐阅读