public class ObservableScrollView extends ScrollView {
public interface ScrollViewListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y,
int oldx, int oldy);
}
private ScrollViewListener scrollViewListener = null;
public ObservableScrollView(Context context) {
super(context);
}
public ObservableScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
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);
}
}
}
//ScrollView
<com.example.dell.wy_one.view.custom.ObservableScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_below="@id/tuijian"
android:layout_marginLeft="16pt"
android:id="@+id/choiceness_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="65pt"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</com.example.dell.wy_one.view.custom.ObservableScrollView>
//标题
<RelativeLayout
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="135pt">
<TextView
android:id="@+id/jingxuan"
android:layout_width="match_parent"
android:layout_height="82pt"
android:textSize="32pt"
android:gravity="center"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
@BindView(R.id.line)
RelativeLayout line;
@BindView(R.id.scrollView)
ObservableScrollView scrollView;
private int imageHeight = 500; //设置渐变高度,一般为导航图片高度,自己控制
//搜索框在布局最上面
line.bringToFront();
//滑动监听
scrollView.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
if (y <= 0) {
line.setBackgroundColor(Color.argb((int) 0, 2, 255, 37));
jingxuan.setTextColor(Color.argb((int) 0, 255, 255, 255));
jingxuan.setText("精选");
//AGB由相关工具获得,或者美工提供
} else if (y > 0 && y <= imageHeight) {
float scale = (float) y / imageHeight;
float alpha = (255 * scale);
// 只是layout背景透明
line.setBackgroundColor(Color.argb((int) alpha, 2, 255, 37));
jingxuan.setTextColor(Color.argb((int) alpha, 255, 255, 255));
} else {
line.setBackgroundColor(Color.argb((int) 255, 2, 255, 37));
jingxuan.setTextColor(Color.argb((int) 255, 255, 255, 255));
jingxuan.setText("精选");
}
}
});