RecyclerView滚动时顶部不能填充

问题描述: 当RecyclerView在布局里设置 android:paddingTop=“30dp” 时,滚动时顶部不能填充

官方文档: ViewGroup setClipToPadding(boolean)方法

ViewGroup_setClipToPadding
clipToPadding: 控件的绘制区域是否在padding里面, 值为true时padding那么绘制的区域就不包括padding区域;

官方文档:androidx/releases/recyclerview
androidx/releases/recyclerview关于clipToPadding的API改变
为什么在这里要单独列出androidx/releases/recyclerview?

解答如上图红色圈出来的API changes的部分内容,大致意思如下:

PagerSnapHelper 和 LinearSnapHelper 现在将 RecyclerView 的内边距考虑在内,而无论clipToPadding 的值如何

了解上面的知识点后,先看一下问题:

修改前的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:paddingTop="30dp" />

</LinearLayout>

在这里插入图片描述
从上图可以看到列表滚动时,顶部有30dp的白色空间,列表不能滚动到顶部

修改后的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:clipToPadding="false"
        android:paddingTop="30dp" />

</LinearLayout>

区别是: 增加一行代码: android:clipToPadding=“false”

clipToPadding属性默认值是true

在这里插入图片描述
从修改后的效果图可以看出列表滚动时,内容是可以滚动到顶部的了。

欢迎关注我的公众号,不定期推送优质的文章,
微信扫一扫下方二维码即可关注。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值