想要这样一个效果,帧布局的一透明的搜索框随着Scrollview的距离改变而改变,本来用scrollview的触摸监听来做,结果发现,scrollview的滚动距离并不是手势最终的滚动距离,搜索框总是位置不对,因为scrollview的getScrollY()返回不正确的值
在网上找到了解决方案,重写scrollview的onScrollChanged()方法获取scroll滚动的距离值
package com.okgj.shopping.util;
import com.okgj.shopping.view.ObservableScrollView;
public interface ScrollViewListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
mainsoso = (RelativeLayout)findViewById(R.id.mainsoso);
sv_whole = (ObservableScrollView)findViewById(R.id.sv_whole);
sv_whole.setScrollViewListener(this);
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
int oldx, int oldy) {
if(y > top-20){
params.topMargin = 20;
}else{
params.topMargin = top-y;
}
mainsoso.setLayoutParams(params);
}
public class ObservableScrollView extends ScrollView {
private ScrollViewListener scrollViewListener = null;
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.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);
}
}
}