//重写ScrollViewpublic class NotifyingScrollView extends ScrollView { /** * @author Cyril Mottier */ public interface OnScrollChangedListener { void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt); } private OnScrollChangedListener mOnScrollChangedListener; public NotifyingScrollView(Context context) { super(context); } public NotifyingScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public NotifyingScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (mOnScrollChangedListener != null) { mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt); } } public void setOnScrollChangedListener(OnScrollChangedListener listener) { mOnScrollChangedListener = listener; }}
布局引用重写NotifyingScrollView控件
<你的项目路径.notifyingscrollview android:id="@id/sv_personal" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/layout_bg" android:scrollbars="none">你要显示的内容 你的项目路径.notifyingscrollview>
activity使用引用
NotifyingScrollView sv_personal=(NotifyingScrollView)findViewById(R.id.sv_personal);sv_personal.setOnScrollChangedListener(mOnScrollChangedListener);private NotifyingScrollView.OnScrollChangedListener mOnScrollChangedListener = new NotifyingScrollView.OnScrollChangedListener() {public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {int Height=Utility.dip2px(context, 130);float ratio =Math.max(Math.min(1, t/Height), 0);//导航控件linar_top.setAlpha(ratio* 255);}};
int Height=Utility.dip2px(context, 130); 130为要滑动的高度