随着Scrollview滚动颜色渐变设置,在Scrollview嵌套RecycleView的时候惯性滑动消失,以下就是解决此问题的自定义Scrollview
第一步:
public class ObservableScrollView extends ScrollView { private int downY; private int mTouchSlop; public interface ScrollViewListener { void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy); } private ScrollViewListener scrollViewListener = null; public ObservableScrollView(Context context) { super(context); //触发移动事件的最小距离 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } public ObservableScrollView(Context context, AttributeSet attrs) { super(context, attrs); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } public void setScrollViewListener(ScrollViewListener scrollViewListener) { this.scrollViewListener = scrollViewListener; } @Override protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) { return 0; } @Override protected void onScrollChanged(int x, int y, int oldx, int oldy) { super.onScrollChanged(x, y, oldx, oldy); if (scrollViewListener != null) { scrollViewListener.onScrollChanged(this, x, y, oldx, oldy); } }
//随手势惯性滚动 @Override public boolean onInterceptTouchEvent(MotionEvent e) { int action = e.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: downY = (int) e.getRawY(); break; case MotionEvent.ACTION_MOVE: int moveY = (int) e.getRawY(); if (Math.abs(moveY - downY) > mTouchSlop) { return true; } } return super.onInterceptTouchEvent(e); } }
第二步:当然是在xml中加载布局文件了,这里就不贴代码了
第三步:想要随着Scrollview滚动view的颜色渐变,当然还要在Activity中调用
// 获取顶部图片高度后,设置滚动监听 ViewTreeObserver vto = convenientBanner.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { convenientBanner.getViewTreeObserver().removeGlobalOnLayoutListener( this); imageHeight = convenientBanner.getHeight(); mScrollView.setScrollViewListener(new ObservableScrollView.ScrollViewListener() { @Override public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) { if (y <= 0) { mView.setBackgroundColor(Color.argb((int) 0, 227, 29, 26)); } else if (y > 0 && y <= imageHeight) { float scale = (float) y / imageHeight; float alpha = (255 * scale); mView.setBackgroundColor(Color.argb((int) alpha, 65, 179, 252)); } else { mView.setBackgroundColor(Color.argb((int) 255, 65, 179, 252)); } } }); } });