如何判断ScrollView是否滚动
发布网友
发布时间:2022-05-23 16:08
我来回答
共1个回答
热心网友
时间:2023-05-18 11:40
有的。但这是个protected方法,只能被其自身或者子类调用。你可以继承ScrollView,重载该方法,然后就可以在这个方法里面判断是否发生了滚动了。我曾经有类似需要,采取一种方式是自己定义一个listener,然后在方法里面去调用listener。但重载的是onOverScrolled,你可以参考如下代码自己改写一下即可:[mw_shl_code=java,true]
public class CustomScrollView extends ScrollView {
private OnOverScrolledListener mOnOverScrolledListener = null;
/**
* @param context
*/
public CustomScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
/**
* @param context
* @param attrs
*/
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
/**
* @param context
* @param attrs
* @param defStyle
*/
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see android.widget.ScrollView#arrowScroll(int)
*/
@Override
public boolean arrowScroll(int arg0) {
// TODO Auto-generated method stub
return super.arrowScroll(arg0);
}
/* (non-Javadoc)
* @see android.widget.ScrollView#computeScroll()
*/
@Override
public void computeScroll() {
// TODO Auto-generated method stub
super.computeScroll();
}
/**
* @return the mOnOverScrolledListener
*/
public OnOverScrolledListener getOnOverScrolledListener() {
return mOnOverScrolledListener;
}
/**
* @param mOnOverScrolledListener the mOnOverScrolledListener to set
*/
public void setOnOverScrolledListener(OnOverScrolledListener mOnOverScrolledListener) {
this.mOnOverScrolledListener = mOnOverScrolledListener;
}
public interface OnOverScrolledListener{
public abstract void onOverScrolled(View view, int scrollX, int scrollY, boolean clampedX, boolean clampedY);
}
/* (non-Javadoc)
* @see android.widget.ScrollView#onOverScrolled(int, int, boolean, boolean)
*/
@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
// TODO Auto-generated method stub
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
if(mOnOverScrolledListener != null) {
mOnOverScrolledListener.onOverScrolled(this, scrollX, scrollY, clampedX, clampedY);
}
Log.i("CustomScrollView", "===DBG:onOverScrolled");
}
}
[/mw_shl_code]