重写ScrollView:
package com.example.ningac.mathom_a.myview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;
/**
* Created by Ning.A.C on 2017/12/17.
*/
public class MyScrollView extends ScrollView{
private ScrollViewListener scrollViewListener = null;
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
//x为当前滑动条的横坐标,y表示当前滑动条的纵坐标,oldx为前一次滑动的横坐标,oldy表示前一次滑动的纵坐标
super.onScrollChanged(x, y, oldx, oldy);
if (scrollViewListener != null) {
//在这里将方法暴露出去
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
//是否要其弹性滑动
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
int scrollY, int scrollRangeX, int scrollRangeY,
int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
// 弹性滑动关键则是maxOverScrollX, 以及maxOverScrollY,
// 一般默认值都是0,需要弹性时,更改其值即可
// 即就是,为零则不会发生弹性,不为零(>0,负数未测试)则会滑动到其值的位置
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
scrollRangeY, 0, 0, isTouchEvent);
}
//接口
public interface ScrollViewListener {
void onScrollChanged(View scrollView, int x, int y, int oldx, int oldy);
}
public void setScrollViewListener(ScrollViewListener listener)
{
scrollViewListener=listener;
}
}
设置标题栏:
scrollView.setScrollViewListener(new MyScrollView.ScrollViewListener() {
@Override
public void onScrollChanged(View scrollView, int x, int y, int oldx, int oldy) {
if (isFirst) {
isFirst = false;
}
titleAnim(oldy,y);
}
});
public void titleAnim(int oldy,int y){
Log.d("zzz",oldy+" "+y);
if(y==0){
found_title.setAlpha(0);
}else{
if (y <= 100) {
float alpha =((float) y) / 100;
found_title.setAlpha(alpha);
if (alpha==0)
{
found_title.setClickable(false);
}else
{
found_title.setClickable(true);
}
}else {
found_title.setAlpha(1);
}
}
// }else{
//
// }
// if (y < 200) {
// float alpha = 1 - ((float) y) / 200;
// found_title.setAlpha(alpha);
// if (alpha==0)
// {
// found_title.setClickable(false);
// }else
// {
// found_title.setClickable(true);
// }
// } else {
// //下滑显示标题栏
// if (oldy > y) {
// found_title.setAlpha(1);
// found_title.setClickable(true);
// } else {
// found_title.setAlpha(0);
// found_title.setClickable(false);
// }
// }
}