Recycler下方跟随显示一个Button,显示内容超出屏幕时,按钮固定在底部显示

在开发中有这样的需求,列表下面跟随显示一个按钮,当列表的item很多时,按钮会超出屏幕显示,这时要求按钮固定在屏幕底部显示。

网上大部分的解决方案是,在用LinearLayout包裹RecyclerView 和 Button,外层LinearLayout 设置android:layout_height=“wrap_content”, 给RecyclerView设置android:layout_weight=“1” android:layout_height=“0dp”,给Button部分设置android:layout_height=“wrap_content”。但是我这样设置,在item条目比较少时,按钮部分还是显示在屏幕底部的。

我找到最佳的解决方案还是通过代码动态控制,首先是解决思路的转变,上述需求的真正目的就是当item少量的时候按钮距离列表要近一点。由此我们可以把按钮设置成一直在屏幕底部,item很少的时候,给按钮设置margin或padding让按钮离列表近一点。

然后问题就分解成:一、什么时候去设置margin或padding?二、margin或padding的值设置成多少?

如下图所示,RecyclerView底部到屏幕顶部的距离是recyclerBottomY,为h1+h2的距离,底部Button顶部到屏幕顶部的距离为layoutButtonTopY,给RecyclerView到Button留一定间隙gapHeight。那么当layoutButtonTopY减去recyclerBottomY 大于gapHeight时,就需要给button部分设置margin或者padding使按钮上移,设置的大小即layoutButtonTopY-recyclerBottomY-gapHeight

请添加图片描述

以下是具体代码:

private fun resetBottomLayout(){
    binding.recyclerView.postDelayed({
        val scrollRange = binding.recyclerView.computeVerticalScrollRange()
        val recyclerIntArray = IntArray(2)
        binding.recyclerView.getLocationOnScreen(recyclerIntArray)
        val recyclerBottomY = recyclerIntArray[1]+scrollRange
        val layoutButtonIntArray = IntArray(2)
        binding.layoutDealButton.getLocationOnScreen(layoutButtonIntArray)
        val layoutButtonTopY = layoutButtonIntArray[1]
        if (layoutButtonTopY-recyclerBottomY>SizeUtils.dp2px(30f)){
            binding.layoutBottomButton.setPadding(0,0,0,layoutButtonTopY-recyclerBottomY-SizeUtils.dp2px(30f))
        }
    }, 300)
}

布局代码为:

<?xml version="1.0" encoding="utf-8"?>

<androidx.appcompat.widget.LinearLayoutCompat 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="wrap_content"
    android:background="@color/content_color"
    android:orientation="vertical"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"/>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/layout_bottom_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/recycler_deal_stream">
        <Button
            android:layout_weight="1"
            android:id="@+id/btnStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:layout_weight="2"
            android:id="@+id/btnEnd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>

inearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值