/**
* 自定义ScrollView
* Created by Home-Pc on 2017/7/4.
*/
public class CustomScrollView extends ScrollView {
public CustomScrollView(Context context) {
super(context);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 重写该方法是为了处理进入页面直接显示到页面最底部,
* 重写该方法后,既可以处理完成正常加载进入页面
* @param rect
* @return
*/
@Override
protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
return 0;
}
public interface ScrollViewListener {
void onScrollChanged(CustomScrollView scrollView, int x, int y,
int oldx, int oldy);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
private ScrollViewListener scrollViewListener;
@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);
}
}
* 自定义ScrollView
* Created by Home-Pc on 2017/7/4.
*/
public class CustomScrollView extends ScrollView {
public CustomScrollView(Context context) {
super(context);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 重写该方法是为了处理进入页面直接显示到页面最底部,
* 重写该方法后,既可以处理完成正常加载进入页面
* @param rect
* @return
*/
@Override
protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
return 0;
}
public interface ScrollViewListener {
void onScrollChanged(CustomScrollView scrollView, int x, int y,
int oldx, int oldy);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
private ScrollViewListener scrollViewListener;
@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);
}
}
}
而其activity 用法
private void initScrollView() { ViewTreeObserver vto = titleViewGroup.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { titleViewGroup.getViewTreeObserver().removeGlobalOnLayoutListener( this); titleHeight = titleViewGroup.getHeight(); WheatHomeScrollView.setScrollViewListener(WheatHomeActivity.this); } }); }
@Override public void onScrollChanged(CustomScrollView scrollView, int x, int y, int oldx, int oldy) { // Log.i("TAG", "y--->" + y + " height-->" + height); if (y <= 0) { titleViewGroup.setBackgroundColor(Color.argb((int) 0, 227, 29, 26)); } else if (y > 0 && y <= titleHeight) { float scale = (float) y / titleHeight; float alpha = (255 * scale); // 只是layout背景透明(仿知乎滑动效果) titleViewGroup.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255)); } else { titleViewGroup.setBackgroundColor(Color.argb((int) 255, 255, 255, 255)); } }该类需要实现ScrollViewListener