AppBarLayout+RecyclerView+上拉加载时的适配问题?

本文解决了一个在使用AppBarLayout与RecyclerView组合时页面显示不全的问题,尤其是在添加上拉加载的情况下,LoadView会被遮挡。通过重写FixAppBarLayoutBehavior,调整NestedScroll行为,解决了滑动冲突,确保页面元素完全可见。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天发现一个很奇怪的问题,测试提出来页面显示不全?乍一听惊了,什么情况?最后排查原因原来是AppBarLayout+RecyclerView结合使用的问题。废话不多说,上方案:

BUG:症状,以上两者结合使用会导致页面下边显示不全,也就是说滑动的时候下边会有遮挡,尤其是你还添加了上拉加载的情况下,那么上拉加载的LoadView就会被遮挡住。

原因:网上找的大神说法:和 NestScroll 相关了,重写 AppBarLayout.Behavior 打印 log,发现在快速滑动到顶部和底部之后,AppBarLayout 在一段时间内还处于 Fling 状态,那么我们想办法把这段无效的 Fling 干掉就好了

解决方案:

public class FixAppBarLayoutBehavior extends AppBarLayout.Behavior {

public FixAppBarLayoutBehavior() {
    super();
}

public FixAppBarLayoutBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target,
                           int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
            dxUnconsumed, dyUnconsumed, type);
    stopNestedScrollIfNeeded(dyUnconsumed, child, target, type);
}

@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
                              View target, int dx, int dy, int[] consumed, int type) {
    super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
    stopNestedScrollIfNeeded(dy, child, target, type);
}

private void stopNestedScrollIfNeeded(int dy, AppBarLayout child, View target, int type) {
    if (type == ViewCompat.TYPE_NON_TOUCH) {
        final int currOffset = getTopAndBottomOffset();
        if ((dy < 0 && currOffset == 0)
                || (dy > 0 && currOffset == -child.getTotalScrollRange())) {
            ViewCompat.stopNestedScroll(target, ViewCompat.TYPE_NON_TOUCH);
        }
    }
}

}

最后替换掉:
在这里插入图片描述
结果就是这样,冲突解决,如果还有其他问题那就是布局问题了,自己慢慢调节一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值