用法:
@ViewInject(R.id.sv)
PullToRefreshScrollView sv;
//y轴滑动监听
sv.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldX, int oldY) {
Log.i("====","y"+y);
}
});
//上拉下拉的y轴监听
ObservableScrollView refreshableView = sv.getRefreshableView();
refreshableView.setPullScrollViewListener(new PullToRefreshScrollView.PullScrollViewListener() {
@Override
public void onScrollChanged(PullToRefreshScrollView pullToRefreshScrollView, int x, int y, int oldX, int oldY) {
Log.i("====","y"+y);
}
});
源码稍作修改:
package com.handmark.pulltorefresh.library;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;
public class PullToRefreshScrollView extends PullToRefreshBase<ObservableScrollView> {
public PullToRefreshScrollView(Context context) {
super(context);
}
public PullToRefreshScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PullToRefreshScrollView(Context context, Mode mode) {
super(context, mode);
}
public PullToRefreshScrollView(Context context, Mode mode, AnimationStyle style) {
super(context, mode, style);
}
@Override
public final Orientation getPullToRefreshScrollDirection() {
return Orientation.VERTICAL;
}
@Override
protected ObservableScrollView createRefreshableView(Context context, AttributeSet attrs) {
ObservableScrollView scrollView = new ObservableScrollView(context, attrs);
scrollView.setId(R.id.scrollview);
return scrollView;
}
@Override
protected boolean isReadyForPullStart() {
return mRefreshableView.getScrollY() == 0;
}
@Override
protected boolean isReadyForPullEnd() {
View scrollViewChild = mRefreshableView.getChildAt(0);
if (null != scrollViewChild) {
return mRefreshableView.getScrollY() >= (scrollViewChild.getHeight() - getHeight());
}
return false;
}
public void firstReFreshing(boolean doScroll) {
if (doScroll) {
new AsyncTask() {//该处是针对PullToRefreshScrollView控件的bug进行补充的
@SuppressWarnings("ResourceType")
@Override
protected Integer doInBackground(Integer... params) {
while (true) {
if (mHeaderLayout.getHeight() > 0) {
return null;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
protected void onPostExecute(Integer result) {
Log.e("shiyan", "zhixingle");
setRefreshing(true);
}
}.execute();
} else {
setRefreshing(false);
}
}
private PullScrollViewListener pullScrollViewListener = null;
public interface PullScrollViewListener {
void onScrollChanged(PullToRefreshScrollView pullToRefreshScrollView, int x, int y, int oldX, int oldY);
}
public void setPullScrollViewListener(PullScrollViewListener pullScrollViewListener) {
this.pullScrollViewListener = pullScrollViewListener;
}
@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);
if (pullScrollViewListener != null) {
pullScrollViewListener.onScrollChanged(this, x, y, oldX, oldY);
}
}
}
package com.handmark.pulltorefresh.library;
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);
}
}
public interface ScrollViewListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldX, int oldY);
}
}