android - 扩展持久性底页时调整回收站视图高度
问题描述
我有一个持久的底部工作表(基本上是一个按钮)和一个回收器视图,它们都包含在 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
解决方案
最近面临同样的问题,没有找到比删除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)
推荐阅读
- css - 高度:在 Safari 中运行时 100% 停止工作
- node.js - 将文件上传到其他用户的 OneDrive 共享时,MS Graph 抛出 401
- c# - 如何单击 C# 中的 Selenium WebDriver 链接所覆盖的复选框
- php - 重定向初始化 ci4 时无法设置登录记住我的 cookie
- java - 如何在回收站视图中添加 Startapp 横幅?
- mongodb - 使用 Mongoose 查询视图(并封装文档字段)
- firebase - Flutter Firebase 流
- python - `pytestqt.mouseMove` 到菜单栏菜单小部件移动到错误的位置
- c - gitignore 函数显示没有被忽略的文件
- javascript - 如何使用 useReducer 更新嵌套数组?