美文网首页
CoordinatorLayout + AppBarLayout

CoordinatorLayout + AppBarLayout

作者: YbSTGing | 来源:发表于2017-11-17 14:49 被阅读165次

    在项目中用到了,向上滑动的时候,顶部悬停一个View,使用了:CoordinatorLayout + AppBarLayout + NestScrollView 。但是出现了向上滑动之后卡顿的情况,找 了好多解决方案不管用,这个是最靠谱的一个。
    解决方案 (里面很详细的讲解了原理等一堆东西。)

    这里我直接把要用的代码放在下面了,如果作者看到了不同意转载,请联系我!

    public final class FlingBehavior extends AppBarLayout.Behavior {
    
        private static final String TAG = FlingBehavior.class.getName();
        private static final int TOP_CHILD_FLING_THRESHOLD = 1;
        private static final float OPTIMAL_FLING_VELOCITY = 3500;
        private static final float MIN_FLING_VELOCITY = 20;
    
        boolean shouldFling = false;
        float flingVelocityY = 0;
    
        public FlingBehavior() {
        }
    
        public FlingBehavior(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target,
                                      int velocityX, int velocityY, int[] consumed) {
    
            super.onNestedPreScroll(coordinatorLayout, child, target, velocityX, velocityY, consumed);
    
            if (velocityY > MIN_FLING_VELOCITY) {
                shouldFling = true;
                flingVelocityY = velocityY;
            } else {
                shouldFling = false;
            }
        }
    
        @Override
        public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout abl, View target) {
            super.onStopNestedScroll(coordinatorLayout, abl, target);
            if (shouldFling) {
                Log.d(TAG, "onNestedPreScroll: running nested fling, velocityY is " + flingVelocityY);
                onNestedFling(coordinatorLayout, abl, target, 0, flingVelocityY, true);
            }
        }
    
        @Override
        public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target,
                                     float velocityX, float velocityY, boolean consumed) {
    
            if (target instanceof RecyclerView && velocityY < 0) {
                Log.d(TAG, "onNestedFling: target is recyclerView");
                final RecyclerView recyclerView = (RecyclerView) target;
                final View firstChild = recyclerView.getChildAt(0);
                final int childAdapterPosition = recyclerView.getChildAdapterPosition(firstChild);
                consumed = childAdapterPosition > TOP_CHILD_FLING_THRESHOLD;
            }
    
            // prevent fling flickering when going up
            if (target instanceof NestedScrollView && velocityY > 0) {
                consumed = true;
            }
    
            if (Math.abs(velocityY) < OPTIMAL_FLING_VELOCITY) {
                velocityY = OPTIMAL_FLING_VELOCITY * (velocityY < 0 ? -1 : 1);
            }
            Log.d(TAG, "onNestedFling: velocityY - " + velocityY + ", consumed - " + consumed);
    
            return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
        }
    }
    

    使用方法:

    <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="这里填写你新建的这个类的全名.FlingBehavior"
                android:fitsSystemWindows="true"> 
    

    相关文章

      网友评论

          本文标题:CoordinatorLayout + AppBarLayout

          本文链接:https://www.haomeiwen.com/subject/gqdjvxtx.html