Android ScrollView滑動的監聽方法為什麼會報錯

在一次android app的開發過程中,客戶的需求是需要實現介面在下滑時標題欄消失,所以其中涉及到了scrollview的滑動的監聽方法,但是在開發過程中出現了問題

依照上邊的提示,應該是我的api版本太低了,這個版本scrollview沒有滑動的監聽方法,所以開始從網路上查詢修改的方法,發現自定義一個scrollview控制元件就可以實現

以下是我新建的自定義scrollview

package com。jing。test;

import android。content。Context;

import android。util。AttributeSet;

import android。widget。ScrollView;

public class ObservableScrollView extends ScrollView {

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,在activity中寫監聽就可以了