当SwipeRefreshLayout包裹了一个自定义控件的时候,下拉会先走刷新,如自定义webview,官方推荐重写SwipeRefreshLayout的canChildScrollUp方法,
以下是实例:
swipeRefreshLayout.setmCanChildScrollUpCallback(MyActivity.this);
@Override public boolean canSwipeRefreshChildScrollUp() { return mHybridWebView.mWebView.getScrollY() > 0; }
public class CustomSwipeToRefresh extends SwipeRefreshLayout {
private CanChildScrollUpCallback mCanChildScrollUpCallback;
public CustomSwipeToRefresh(Context context) {
super(context);
}
public CustomSwipeToRefresh(Context context, AttributeSet attrs) {
super(context, attrs);
}
public interface CanChildScrollUpCallback {
boolean canSwipeRefreshChildScrollUp();
}
public CanChildScrollUpCallback getmCanChildScrollUpCallback() {
return mCanChildScrollUpCallback;
}
public void setmCanChildScrollUpCallback(CanChildScrollUpCallback mCanChildScrollUpCallback) {
this.mCanChildScrollUpCallback = mCanChildScrollUpCallback;
}
@Override
public boolean canChildScrollUp() {
if(mCanChildScrollUpCallback != null){
return mCanChildScrollUpCallback.canSwipeRefreshChildScrollUp();
}
return super.canChildScrollUp();
}
}
public class CustomSwipeToRefresh extends SwipeRefreshLayout {
private CanChildScrollUpCallback mCanChildScrollUpCallback;
public CustomSwipeToRefresh(Context context) {
super(context);
}
public CustomSwipeToRefresh(Context context, AttributeSet attrs) {
super(context, attrs);
}
public interface CanChildScrollUpCallback {
boolean canSwipeRefreshChildScrollUp();
}
public CanChildScrollUpCallback getmCanChildScrollUpCallback() {
return mCanChildScrollUpCallback;
}
public void setmCanChildScrollUpCallback(CanChildScrollUpCallback mCanChildScrollUpCallback) {
this.mCanChildScrollUpCallback = mCanChildScrollUpCallback;
}
@Override
public boolean canChildScrollUp() {
if(mCanChildScrollUpCallback != null){
return mCanChildScrollUpCallback.canSwipeRefreshChildScrollUp();
}
return super.canChildScrollUp();
}
}