package com.zjtd.bzcommunity.lib;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
public class myScrollView extends ScrollView {
public myScrollView(Context context) {
super(context);
}
public myScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
// public myScrollView(Context context, AttributeSet attrs, int defStyle) {
// super(context, attrs, defStyle);
// }
/**
* 公共接口:ScrollView滚动监听
*/
public interface OnScrollChangedListener {
void onScrollChanged(ScrollView who, int x, int y, int oldx, int oldy);
}
private OnScrollChangedListener mOnScrollChangedListener;
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if (mOnScrollChangedListener != null) {
//使用公共接口触发滚动信息的onScrollChanged方法,将滚动位置信息暴露给外部
mOnScrollChangedListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
/**
* 暴露给外部的方法:设置滚动监听
* @param listener
*/
public void setOnScrollChangedListener(OnScrollChangedListener listener) {
mOnScrollChangedListener = listener;
}
}
布局:
<com.zjtd.bzcommunity.lib.myScrollView
android:id="@+id/mymyscrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:orientation="vertical" >
android:id="@+id/mymyscrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:orientation="vertical" >
</LinearLayout>
</com.zjtd.bzcommunity.lib.myScrollView>
Activity:
private RelativeLayout layout_top_search; // 顶部渐变布局
private myScrollView mScrollView; // 整体ScrollView
private static final int START_ALPHA = 0;
private static final int END_ALPHA = 255;
private int fadingHeight = 600; // 当ScrollView滑动到什么位置时渐变消失(根据需要进行调整)
private Drawable drawable; // 顶部渐变布局需设置的Drawable
private myScrollView mScrollView; // 整体ScrollView
private static final int START_ALPHA = 0;
private static final int END_ALPHA = 255;
private int fadingHeight = 600; // 当ScrollView滑动到什么位置时渐变消失(根据需要进行调整)
private Drawable drawable; // 顶部渐变布局需设置的Drawable
//-----------------------init--------------
drawable = getResources().getDrawable(R.color.red);
drawable.setAlpha(START_ALPHA);
layout_top_search.setBackgroundDrawable(drawable);
mScrollView.setOnScrollChangedListener(scrollChangedListener);
drawable.setAlpha(START_ALPHA);
layout_top_search.setBackgroundDrawable(drawable);
mScrollView.setOnScrollChangedListener(scrollChangedListener);
/**
* 公共接口:ScrollView的滚动监听 p l
*/
private OnScrollChangedListener scrollChangedListener = new OnScrollChangedListener() {
@Override
public void onScrollChanged(ScrollView who, int x, int y, int oldx,
int oldy) {
if (y > fadingHeight) {
y = fadingHeight; // 当滑动到指定位置之后设置颜色为纯色,之前的话要渐变---实现下面的公式即可
relativela_id.setBackgroundColor(Color.WHITE);
} else if (y < 0) {
y = 0;
} else {
relativela_id.setBackgroundColor(0x99FFFFFF);
}
drawable.setAlpha(y * (END_ALPHA - START_ALPHA) / fadingHeight
+ START_ALPHA);
}
};
* 公共接口:ScrollView的滚动监听 p l
*/
private OnScrollChangedListener scrollChangedListener = new OnScrollChangedListener() {
@Override
public void onScrollChanged(ScrollView who, int x, int y, int oldx,
int oldy) {
if (y > fadingHeight) {
y = fadingHeight; // 当滑动到指定位置之后设置颜色为纯色,之前的话要渐变---实现下面的公式即可
relativela_id.setBackgroundColor(Color.WHITE);
} else if (y < 0) {
y = 0;
} else {
relativela_id.setBackgroundColor(0x99FFFFFF);
}
drawable.setAlpha(y * (END_ALPHA - START_ALPHA) / fadingHeight
+ START_ALPHA);
}
};