android - 如何在协调器布局中拉伸内容以填充空白空间?
问题描述
我正在尝试实现如下所示的行为(在图片上),所以我需要拉伸 AppBarLayout 下面的所有内容。我已经通过实现自定义 CoordinatorLayout Behavior 以某种方式实现了它,但是该解决方案在 RecyclerView 内部的视图回收和整体性能方面存在一些问题。有没有更简单的解决方案来实现我想要的?
public class TestBehaviour48Margin extends CoordinatorLayout.Behavior {
public TestBehaviour48Margin() {
}
public TestBehaviour48Margin(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
return dependency instanceof AppBarLayout;
}
@Override
public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
AppBarLayout appBarLayout = (AppBarLayout) dependency;
val toolbar = appBarLayout.findViewById(R.id.toolbar);
int range = appBarLayout.getTotalScrollRange();
int totalHeight = parent.getHeight();
int appBarHeight = appBarLayout.getHeight();
int toolbarHeight = toolbar.getHeight();
int initialHeight = totalHeight - appBarHeight;
int finalHeight = totalHeight - toolbarHeight;
int differenceHeight = finalHeight - initialHeight;
float factor = -appBarLayout.getY() / range;
val layoutParams = child.getLayoutParams();
int lastHeight = layoutParams.height;
layoutParams.height = (int) (initialHeight + (differenceHeight * factor)) + LayoutUtils.dpToPx(parent.getContext(), 48);
if(lastHeight != layoutParams.height){
child.setLayoutParams(layoutParams);
}
return true;
}
}
<CoordinatorLayout ... (some content)
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="48dp"
android:layout_gravity="bottom"
app:layout_behavior=".custom_views.TestBehaviour48Margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/custom_pink"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:id="@+id/rv_strength_items"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/grey_B" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/custom_blue_light" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:background="@color/custom_green"
bind:layout_anchor="@id/scroll"
bind:layout_anchorGravity="bottom" />
</LinearLayout>
</FrameLayout>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorSurfaceNight"
android:visibility="@{viewmodel.views.mainLayoutVisibility}"
app:elevation="0dp"
app:layout_behavior=".fragments.training.main_training.view_fold.BlockableAppBarLayoutBehaviour">
... rest of code
解决方案
推荐阅读
- python - 我无法使用 python 正确循环遍历 xml 文件,如何循环并以正确的顺序返回多个不同的标签?
- javascript - 如何通过删除veu js中的标签来打印html标签内容?
- mongodb - MongoDB聚合获取$group创建的组数
- java - 需要用管道替换逗号以在java中导出csv
- php - 求救!如何在局域网中的 Windows IIS 服务器上部署多个 Laravel 应用程序
- php - 在 PHP 中覆盖 CSV 文件时跳过第一行
- wso2 - 将 wso2 身份服务器从 5.7.0 迁移到 5.11.0
- asp.net - 如何使用 iTextSharp 从数据表中生成 PDF 一页一行
- spring-boot - 在 Spring Boot 中使用 Thymeleaf 显示错误消息不起作用
- jupyter-notebook - 推荐共享只读 Jupyter 笔记本的方法?