前一陣將support庫(kù)版27.0.0發(fā)現(xiàn)了這個(gè)問(wèn)題狂窑。發(fā)現(xiàn)RecyclerView在滑動(dòng)到底部后媳板,會(huì)有急秒的停滯,之后再去加載下一頁(yè)數(shù)據(jù)泉哈。
滑動(dòng)越快蛉幸,停滯時(shí)間越長(zhǎng)。我們知道上拉加載實(shí)現(xiàn)方案基本都是監(jiān)聽(tīng)滑動(dòng)狀態(tài)丛晦,當(dāng)滑動(dòng)停止時(shí)奕纫,再去加載下一頁(yè)。代碼基本如下:
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
? ? super.onScrollStateChanged(recyclerView, newState);
? ? if (newState == RecyclerView.SCROLL_STATE_IDLE) {
? ? ? ? onLoadNextPage();
? ? }
}
具體解決方案如下:
https://juejin.im/entry/5b978fe8f265da0b001f16c0
方案一烫沙,重寫(xiě)AppBarLayout.Behavior
代碼如下
public class FixAppBarLayoutBehavior extends AppBarLayout.Behavior {
? ? public FixAppBarLayoutBehavior() {
? ? ? ? super();
? ? }
? ? public FixAppBarLayoutBehavior(Context context, AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? }
? ? @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);
? ? }
? ? @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);
? ? }
? ? 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);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
修改引用
<android.support.design.widget.AppBarLayout
? ? ? ? ? ? ...
? ? ? ? ? ? app:layout_behavior="yourPackage.FixAppBarLayoutBehavior">
方案二:直接修改support版本匹层,升級(jí)到28