- 自定义监听接口
package com.hh.beauter.idbe; import com.hh.beauter.my_ui.MyScrollVIew; /** * Created by Hh on 2017/1/9. */ public interface ScrollViewListener { void onScrollChanged(MyScrollVIew scrollView, int x, int y, int oldx, int oldy); }
- 重写ScrollView
package com.hh.beauter.my_ui; import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; import com.hh.beauter.idbe.ScrollViewListener; /** * Created by Hh on 2017/1/9. */ public class MyScrollVIew extends ScrollView{ private ScrollViewListener scrollViewListener = null; public MyScrollVIew(Context context) { super(context); } public MyScrollVIew(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MyScrollVIew(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); } } }
- 调用接口 设置监听
private ScrollViewListener onScrollChangeListener = new ScrollViewListener() { @Override public void onScrollChanged(MyScrollVIew scrollView, int x, int y, int oldx, int oldy) { float a = (float) y*2/(float) scrollView.getHeight(); Log.e(TAG, "onScrollChanged: "+a ); topBg.setAlpha(a); } };