MyScrollView
public class MyScrollView extends ScrollView {
private OnScrollListener listener;
public void setOnScrollListener(OnScrollListener listener) {
this.listener = listener;
}
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public interface OnScrollListener{
void onScroll(int scrollY);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if(listener != null){
listener.onScroll(t);
}
}
}
在Activity中调用
/**
* 滑动折叠
*/
private void SetScroll() {
home_scroll.setOnScrollListener(new MyScrollView.OnScrollListener() {
@Override
public void onScroll(int scrollY) {
int i = dip2px(getActivity(), scrollY);
int dp = px2dip(getActivity(), i);
if (dp > 520) {
home_head_rl.setBackgroundResource(R.color.white);
black_title();
} else {
home_head_rl.setBackgroundColor(Color.parseColor("#00ffffff"));
white_title();
}
}
});
}
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
*白色字体
*/
public void white_title(){
WindowUtils.setStatusBarColor(getActivity(),R.color.transparent);
WindowUtils.setLightStatusBar(getActivity(),false,true);
}
/**
* 黑色字体
*/
public void black_title(){
WindowUtils.setStatusBarColor(getActivity(),R.color.transparent);
WindowUtils.setLightStatusBar(getActivity(),true,true);
}
状态栏字体可以看这篇文章 修改状态栏字体颜色
布局页面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".view.activity.MainActivity">
<RelativeLayout
android:id="@+id/home_head_rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="@dimen/dp_5">
</RelativeLayout>
<xxxx.xxxx.MyScrollView
android:id="@+id/home_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
</xxxx.xxxx.MyScrollView>
</RelativeLayout>