android scrollview 滑动 改变标题栏颜色

来源:互联网 发布:北大青鸟学java编程 编辑:程序博客网 时间:2024/06/03 19:28
/**
* 自定义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);        }    });}
@Overridepublic 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


阅读全文
0 0
原创粉丝点击