今天发现一个很奇怪的问题,测试提出来页面显示不全?乍一听惊了,什么情况?最后排查原因原来是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);
}
}
}
}
最后替换掉:

结果就是这样,冲突解决,如果还有其他问题那就是布局问题了,自己慢慢调节一下。
本文解决了一个在使用AppBarLayout与RecyclerView组合时页面显示不全的问题,尤其是在添加上拉加载的情况下,LoadView会被遮挡。通过重写FixAppBarLayoutBehavior,调整NestedScroll行为,解决了滑动冲突,确保页面元素完全可见。
208

被折叠的 条评论
为什么被折叠?



