课前准备
添加刷新控件的模块依赖
- 下载添加 TwinklingRefreshLayout 源码(下载地址),这样才能查看源码,找到原因,解决问题
- 如果下载很慢,请看 解决GitHub下载慢或下载失败等问题 ,亲测有效,感谢作者
- 添加 refreshlibrary 库到工程

- 配置 setting.gradle

- 添加依赖(有点乱?好吧...)

- 运行 app ,根据提示解决一些剩下的小问题
- 最后把 TbNestedScrollView 移到 refreshlibrary 中
课堂笔记
- 大概意思:TwinklingRefreshLayout 是通过拦截事件处理加载更多的,所以我们要在拦截 ViewGroup 之前添加拦截 TbNestedScrollView 的方法
- 在 ScrollingUtil 中 isViewToBottom 方法里添加
public static boolean isViewToBottom(View view, int mTouchSlop) {
if (view instanceof AbsListView) return isAbsListViewToBottom((AbsListView) view);
if (view instanceof RecyclerView) return isRecyclerViewToBottom((RecyclerView) view);
if (view instanceof WebView) return isWebViewToBottom((WebView) view, mTouchSlop);
// 拦截 TbNestedScrollView
if (view instanceof TbNestedScrollView) return isTbNestedScrollView((TbNestedScrollView)view);
if (view instanceof ViewGroup) return isViewGroupToBottom((ViewGroup) view);
return false;
}
- 其中 isTbNestedScrollView 方法,返回的意思是 TbNestedScrollView 是否到了底部
private static boolean isTbNestedScrollView(TbNestedScrollView tbNestedScrollView) {
return tbNestedScrollView.isInBottom();
}
- 在 TbNestedScrollView 中,我们需要判断的是其子view RecyclerView能不能再往下滑动
@Override
public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
this.mRecyclerView = (RecyclerView) target;
if (originScroll < height) {
scrollBy(dx, dy);
consumed[0] = dx;
consumed[1] = dy;
}
super.onNestedPreScroll(target, dx, dy, consumed, type);
}
public boolean isInBottom() {
if (mRecyclerView != null) {
return !mRecyclerView.canScrollVertically(1);
}
return false;
}
视频链接