天天看點

android coordinatorlayout滑動卡頓,Coordinatorlayout/AppBarLayout + RecyclerView 上拉加載更多的監聽延遲(卡頓)...

記錄一下

Coordinatorlayout/AppBarLayout + RecyclerView 上拉加載更多的監聽延遲(卡頓)

頂部伸縮布局,RecyclerView上拉加載更多的監聽延遲嚴重(1-2s),非常影響體驗。

這裡需要給AppBarLayout設定一個app:layout_behavior ;

android:id="@+id/appBarLayout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@color/bg_121212"

android:elevation="0dp"

app:layout_behavior="包名.FixAppBarLayoutBehavior"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

app:elevation="0dp">

behavior如下:

class FixAppBarLayoutBehavior{

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);

}

}

}

}

完美解決!十分順暢!